6

Given the build has an Angular app as part of it, there are Jasmine tests in there. What do I have to do to get those test results published as part of the build and better yet, gate the build result on successful execution of all Jasmine tests?

2 Answers 2

14

You can do this through the following script and tasks:

  1. run ng test
  2. publish test results with PublishTestResults task
  3. publish code coverage results with PublishCodeCoverageResults task

In the Azure Pipelines YAML file, this could look as follows:

# perform unit-tests and publish test and code coverage results
- script: |
    npx ng test --watch=false --karmaConfig karma.conf.ci.js --code-coverage
  displayName: 'perform unit tests'    

- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '**/TESTS-*.xml'
  displayName: 'publish unit test results'

- task: PublishCodeCoverageResults@1
  displayName: 'publish code coverage report'
  condition: succeededOrFailed()
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(Build.SourcesDirectory)/coverage/cobertura-coverage.xml'
    failIfCoverageEmpty: true     
Sign up to request clarification or add additional context in comments.

4 Comments

Running a script similar to above, after a successful build, gives me "command not found: ng" - what gives?
@Igor: You previously need to install Node.js and then the @angular/cli package. Please take a look at github.com/centeractive/koia/blob/master/azure-pipelines.yml
I do in a previous stage. I have a Build stage that installs Node.js and angular and successfully builds my Angular front-end and .NET back-end. Testing happens in a subsequent stage. My assumption was that installs executed in a previous stage are still available in a subsequent one - am I wrong?
@Igor: Have you tried running the tests using the Azure Npm task instead of a script task? This example helped me a lot: olivercoding.com/2020-01-02-angular-azure-devops
8

@uminder's Azure configuration is correct.

I would add two things so the answer is complete. This is needed in order to create junit reports and coverage files - so you can later refer them in azure pipeline.

  1. junit and coverage (if not present) reporter to karma.config.js
 config.set({
      plugins: [
        ...
        require('karma-coverage'),
        require('karma-junit-reporter')
      ]

Of course you need to install it

npm install -D karma-junit-reporter

  1. I would also add a cobertura in coverageReporter in to karma.config.js

     coverageReporter:  { 
     ....
     reporters: [
         ...
         { type: 'cobertura' } // TO BE ADDED        
     ]
    

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.