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
You can do this through the following script and tasks:
- run
ng test - publish test results with
PublishTestResultstask - publish code coverage results with
PublishCodeCoverageResultstask
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
4 Comments
Igor
Running a script similar to above, after a successful build, gives me "command not found: ng" - what gives?
uminder
@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
Igor
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?
NJS
@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
@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.
- 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
I would also add a cobertura in coverageReporter in to karma.config.js
coverageReporter: { .... reporters: [ ... { type: 'cobertura' } // TO BE ADDED ]}