How to publish Jest Unit Test Results in VSTS tests?

前端 未结 3 1637
半阙折子戏
半阙折子戏 2021-01-31 03:49

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.

3条回答
  •  孤独总比滥情好
    2021-01-31 04:07

    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

    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:

    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
    

提交回复
热议问题