如何在 VSTS 测试中发布 Jest 单元测试结果?

How to publish Jest Unit Test Results in VSTS tests?

我发现了一些 questions on SO 特定于开玩笑单元测试的版本,以在 VSTS 构建测试结果选项卡中发布其结果。但是没有找到合适的解决方法。

我扔了一些像 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.

这样的 npm 包
  1. 安装jest-trx-results-processor

    # NPM
    npm install jest-trx-results-processor --save-dev
    
    # Yarn
    yarn add -D jest-trx-results-processor
    
  2. 创建 jestTrxProcessor.js 文件,内容如下:

    var builder = require('jest-trx-results-processor');     
    var processor = builder({
        outputFile: 'jestTestresults.trx' 
    });
    module.exports = processor;
    
  3. 更新后的 package.json 文件应如下所示:

    "devDependencies": { 
        "jest": "^23.4.1",
        "jest-trx-results-processor": "0.0.7",
        "jsdom": "^11.12.0",
        ...
    },
    "scripts": {
        "test": "jest"
    },
    "jest": {
        ...,
        "testResultsProcessor": "./jestTrxProcessor.js"
    }
    
  4. npm test 添加 npm task to VSTS build。这将 运行 开玩笑测试并将结果发布到 jestTestresults.trx

  5. 添加 Publish Test Results task of VSTS 以在 VSTS 测试中添加 jestTestresults.trx 结果。

您将能够看到 JEST 测试以及其他测试。

我使用了不同的方法,b/c 经过一些研究我发现 Jest testResultsProcessor property is deprecated。我正在使用 jest-junit 包来获取测试报告(最近它的工作比 jest-trx-results-processor,fwiw):

  1. 将 jest-junit 添加到 package.json

    例如yarn add -D jest-junitnpm add --save-dev jest-junit

  2. 使用 jest-junit 结果报告程序将 VSTS 任务添加到 运行 Jest

    我用了the Yarn task, but you can alternately use the npm task。我使用了这些任务参数:

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

    因为我也想要代码覆盖率。要跳过代码覆盖率报告,请使用这些(npm 或 yarn)任务参数:

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

    请注意 --reporters=default 在那里 b/c 我想要我的构建日志中的默认标准输出。

  3. 添加一个Publish Test Results task

    由于我们使用的是默认路径,因此测试结果文件将写入 ~/junit.xml

  1. (可选)加一个publish code coverage task,也

    如果您运行宁代码覆盖率,您不妨也添加一个发布代码覆盖率结果的任务:

如果您使用的是 YAML 管道,这里是等效的 YAML(请注意,我们使用的是 yarn 任务而不是 npm 任务,但这可以更改):

 - 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

Evaldas 的解决方案已过时,因此我将添加一些修改。

更现代的解决方案是 Evaldas 和维护者的组合:https://www.npmjs.com/package/jest-trx-results-processor

下面我会这样描述。

  1. 安装 jest-trx-results-processor

    # NPM
    npm install jest-trx-results-processor --save-dev
    
    # Yarn
    yarn add -D jest-trx-results-processor
    
  2. 更新后的 package.json 文件应如下所示:

    "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. npm 任务添加到构建管道中 npm test 的 VSTS 构建。它应该是这样的:

  4. 添加 Publish Test Results VSTS 任务以在 VSTS 测试中添加 jestTestresults.trx 结果。为此,在构建管道中,单击 'add sign'。寻找 "Publish Test Results"。它会调出一个这样的菜单。因为它是一个 .trx 文件,所以您需要使用 VSTest 而不是 JTest。
  5. 最后,前端项目的构建管道将如下所示: