32

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.

skyboyer
  • 15,149
  • 4
  • 41
  • 56
Vishnu
  • 1,881
  • 1
  • 24
  • 45

3 Answers3

60

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
crimbo
  • 8,295
  • 6
  • 42
  • 54
  • Thanks this helped a lot. Working well overall just two issues. One is the "Test File" filter shows each jest test as a seperate file, vs each dll/exe for the "vstest" assemblies, but it's not too bad as at least each test shows individually. The other issue is that a build seems limited to one "coverage" result and I build a webpack bundle which goes into a ASP.NET web app. The web app vstest coverage overwrites the jest coverage, but I still have the artifact with the full report so sort of have both. – Rory Nov 20 '18 at 15:40
  • 1
    RE the first issue - have you checked the junit.xml file? Does it look right? You can tweak how the jest-junit reporter emits properties via config. Second issue sounds like a defect in Azure DevOps (not supporting more than 1 coverage report per build plan) - would be great to report it. I don't have that problem, b/c as of right now our UI code is built in a separate build plan from our server code. – crimbo Nov 20 '18 at 16:12
  • I had a look and it looked "ok" but wasn't really sure how that gets mapped to what azure DevOps expects. Will play around a bit and see if config can be tweaked to get better results. – Rory Nov 20 '18 at 20:10
  • @crimbo I've setup my Azure pipeline like this, but I just get a zip file of the coverage html/xml. Do you know how to get Azure to show coverage in the pipeline page? – Nick Jan 08 '20 at 16:02
  • @Nick - Try adding the publish code coverage task, step 4 in my answer. We're using Cobertura-formatted results from step 2. Take a look at your `cobertura-coverage.xml` file and see what is says. – crimbo Jan 15 '20 at 23:30
  • @crimbo Yeah I figured it out. It's still there as an artifact, but eventually our pipeline summary page loads an almost useless summary (single overall percentage of coverage). Azure DevOps 2019 IIRC. – Nick Jan 17 '20 at 21:28
  • 2
    Very useful answer. You might also want to consider `continueOnError: true` (task level) for the `yarn test` task along with `failTaskOnFailedTests: true` (inputs level) on the `PublishTestResults` task so that test failures still cause the result to be uploaded, but the build still fails. – oatsoda Mar 24 '20 at 10:48
  • 1
    @OffHeGoes - great suggestion. I'll update my answer so others don't have figure that out. – crimbo Mar 25 '20 at 20:11
  • @crimbo, the solution works perfectly for me. Thanks. Do you know if there is options to add screenshot attachment to the report, and when you publish the report it will give you a link so you can view the screenshot of fail test. – Phuong Duyen Huynh Ngoc May 12 '21 at 09:16
11

I've gone throw some jest npm packages like tap-xunit and jest-json-to-tap but couldn't figure out it to work. Following steps worked for me to get the results to review under Test of VSTS build.

  1. Install jest-trx-results-processor

    # NPM
    npm install jest-trx-results-processor --save-dev
    
    # Yarn
    yarn add -D jest-trx-results-processor
    
  2. Create jestTrxProcessor.js file with following content:

    var builder = require('jest-trx-results-processor');     
    var processor = builder({
        outputFile: 'jestTestresults.trx' 
    });
    module.exports = processor;
    
  3. Updated package.json file should look like:

    "devDependencies": { 
        "jest": "^23.4.1",
        "jest-trx-results-processor": "0.0.7",
        "jsdom": "^11.12.0",
        ...
    },
    "scripts": {
        "test": "jest"
    },
    "jest": {
        ...,
        "testResultsProcessor": "./jestTrxProcessor.js"
    }
    
  4. Add npm task to VSTS build for npm test. This will run jest tests and publish results to jestTestresults.trx

  5. Add Publish Test Results task of VSTS to add jestTestresults.trx results in VSTS test.

You will be able to see JEST tests along with other tests.

Evaldas Buinauskas
  • 12,532
  • 10
  • 45
  • 88
Vishnu
  • 1,881
  • 1
  • 24
  • 45
6

Evaldas' solution is obsolete, so I'm going to add a few modifications.

The more modern solution is a combination between Evaldas' here, as well as the one from the maintainer: https://www.npmjs.com/package/jest-trx-results-processor

I'll describe it as such below.

  1. Install jest-trx-results-processor

    # NPM
    npm install jest-trx-results-processor --save-dev
    
    # Yarn
    yarn add -D jest-trx-results-processor
    
  2. Updated package.json file should look like:

    "devDependencies": { 
        "jest": "^24.9.0",
        "jest-trx-results-processor": "^1.0.2"
        ...
    },
    "scripts": {
        "test": "jest"
    },
    "jest": {
        ...,
        "reporters": [
    "default",
    [
      "jest-trx-results-processor",
      {
        "outputFile": "./src/jestTestresults.trx",
        "defaultUserName": "user name to use if automatic detection fails"
      }
    ]]}
    
  3. Add npm task to VSTS build for npm test in the build pipeline. It should look like this: The test setup.

  4. Add Publish Test Results task of VSTS to add jestTestresults.trx results in VSTS test. To do this, in the build pipeline, click on the 'add sign'. Look for "Publish Test Results". It'll bring up a menu like this. Since it's a .trx file, you'll need to use VSTest instead of JTest. Configuration of Publish Test Results
  5. Finally, the build pipeline for your frontend project will look like this:The finished pipeline.
Ryan Battistone
  • 571
  • 1
  • 8
  • 20