Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish Jest Unit Test Results in VSTS tests?

I've found some questions on SO specific to version for jest unit test to publish its result in VSTS build Test Results tab. But no proper solution is found.

like image 789
Vishnu Avatar asked Aug 07 '18 06:08

Vishnu


People also ask

How do I create a test report in Azure DevOps?

Open the report from the web portal by going to Test Plans>Progress Report. The report shows you the status of the test plan you last accessed. However, using the filter bar, you can select one or more test plans in the project.

How do I query test results Azure DevOps?

Select the Execute tab in a test suite and then select a test case. Select More options or right-click to open the context menu. Select View test result. Select the test case within a test suite and then choose to view the test details pane.

Where do I put unit tests Jest?

Where to put test files. Unit tests run against specific lines of code. So it makes sense to place them right next to that code. Integration tests run against many lines of code in many files.

Are Jest tests unit tests?

Jest is an open source JavaScript unit testing framework, used by Facebook to test all JavaScript code including React applications. Jest is built on top of Jasmine.


1 Answers

I've used a different approach, b/c after some research I found that the Jest testResultsProcessor property is deprecated. I'm using the jest-junit package for test reports (which has been worked on more recently than the jest-trx-results-processor, fwiw):

  1. Add jest-junit to package.json

    Eg yarn add -D jest-junit or npm add --save-dev jest-junit

  2. Add a VSTS task to run Jest using the jest-junit results reporter

    I used the Yarn task, but you can alternately use the npm task. I used these task arguments:

    jest --ci --reporters=jest-junit --reporters=default --coverage --coverageReporters=cobertura --coverageReporters=html 

    because I also wanted code coverage. To skip code coverage reporting, use these (npm or yarn) task arguments:

    jest --ci --reporters=jest-junit --reporters=default 

    Note that --reporters=default is there b/c I wanted the default stdout in my build log.

  3. Add a Publish Test Results task

    Since we're using the default path, the test results file will be written to ~/junit.xml

Publish Test Results task

  1. (Optional) Add a publish code coverage task, too

    If you're running code coverage, you might as well add a task for publishing the code coverage results, too:

Publish Code Coverage Results task

If you're using a YAML pipeline, here's equivalent YAML (note that we're using the yarn task instead of npm tasks, but that can be changed):

 - task: geeklearningio.gl-vsts-tasks-yarn.yarn-task.Yarn@2     displayName: 'Install dependencies'     inputs:       Arguments: install --no-progress    - task: geeklearningio.gl-vsts-tasks-yarn.yarn-task.Yarn@2     displayName: 'Unit tests'     inputs:       Arguments: 'test --ci --reporters=jest-junit --reporters=default --coverage --coverageReporters=cobertura'     continueOnError: true # Test failures should be published before failing the build    - task: PublishTestResults@2     displayName: 'Publish Jest Unit Test Results'     inputs:       testResultsFiles: junit.xml       mergeTestResults: true       testRunTitle: 'Jest Unit Tests'       failTaskOnFailedTests: true    - task: PublishCodeCoverageResults@1     displayName: 'Publish code coverage from Jest tests'     inputs:       codeCoverageTool: Cobertura       summaryFileLocation: '$(System.DefaultWorkingDirectory)/coverage/cobertura-coverage.xml'       # reportDirectory: '$(System.DefaultWorkingDirectory)/coverage'       failIfCoverageEmpty: true 
like image 186
crimbo Avatar answered Sep 24 '22 01:09

crimbo