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

标签 jestjs azure-pipelines-build-task

我已经找到了一些特定于版本的questions on SO,用于 Jest 单元测试,并将其结果发布在VSTS构建测试结果标签中。但是找不到适当的解决方案。

最佳答案

经过一些研究,我发现Jest testResultsProcessor property is deprecated是一种不同的方法,b / c。我正在使用 jest-junit 软件包来生成测试报告(该软件包比jest-trx-results-processor fwiw更为新近开发):

  • 将jest-junit添加到package.json
    例如yarn add -D jest-junitnpm add --save-dev jest-junit
  • 使用jest-junit结果报告器添加VSTS任务以运行Jest

    我使用了the Yarn task,但是您也可以替代使用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是否存在,我想在构建日志中使用默认的stdout。
  • 添加一个Publish Test Results task

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

  • Publish Test Results task
  • (可选)也添加publish code coverage task

    如果您正在运行代码覆盖率,则最好也添加一个任务来发布代码覆盖率结果:

  • Publish Code Coverage Results 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
    

    关于jestjs - 如何在VSTS测试中发布Jest单元测试结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51720050/

    相关文章:

    unit-testing - 类型错误 : Utils is not a constructor

    react-router - component.find ('a).prop(' href')返回undefined而不是href值

    javascript - jest 函数必须是模拟或 spy

    c# - 使用错误版本的 MSBuild 的自托管代理(适用于 Azure 管道)

    tfs - VSTS : Execute conditional tasks on builds

    javascript - Jest 单元测试调用返回 promise 的函数的函数

    javascript - 如何整合 Flow 和 Jest?

    powershell - WinRM 访问被拒绝(Visual Studio Team Services 的生成定义中的 Azure 文件复制任务)

    azure - 如何使用 YAML 配置文件在 VSTS 中的 VS2017 代理中启动 Azure 存储模拟器

    azure - VSTS 构建 + Azure 数据库 + Redgate ReadyRoll