How to publish Jest Unit Test Results in VSTS tests?

Vishnu picture Vishnu · Aug 7, 2018 · Viewed 14.1k times · Source

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.

Answer

crimbo picture crimbo · Sep 16, 2018

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