node.js - Azure Devops 上的 React App 的 Cobertura 格式代码覆盖率报告

标签 node.js reactjs azure-devops code-coverage

我有一个使用 npx create-react-app 命令创建的 React 应用程序,并为 CI/CD 配置了基于 yaml 的 azure devops 管道。然后我添加了一些简单的单元测试。

我在 yaml 管道中添加了一个脚本任务来生成覆盖率结果,如下所示。

- script: |
    cd reactonazure
    npm run test -- --coverage --ci --reporters=default --reporters=jest-junit --coverageReporters=cobertura
  displayName: 'test code coverage'

但是此任务显示以下输出,然后任务继续运行并且永远不会结束。我必须取消管道运行才能最终停止它。不确定发生了什么。有什么想法吗?

No tests found related to files changed since last commit.

但是当我使用 CI=true 前缀来执行如下命令时,该命令工作正常并且我得到了测试结果,但没有找到覆盖率报告(cobertura-coverage.xml)。

CI=true npm test -- --reporters=jest-junit --reporters=default --coverageReporters=cobertura 

Published test results in Azure DevOps

我添加了一个如下所示的简单脚本任务来列出文件和文件夹,以便我搜索 cobertura-coverage.xml。但我没有在哪里找到该文件。请注意,正在创建 jest.xml 文件。这就是为什么我能够看到上面显示的测试结果的原因。问题出在测试覆盖率报告上。

- script: |
    pwd
    cd ..
    pwd
    ls -Rla
  displayName: 'list out the files and folders for inspection'
 

此外,当我在计算机 (Windows 10) 上的 VS Code 命令提示符下运行以下命令时,我确实在覆盖率文件夹中看到了一个名为 cobertura-coverage.xml 的文件。

npm run test -- --coverage --ci --reporters=default --reporters=jest-junit --coverageReporters=cobertura

cobertura-coverage.xml generation

完整的管道 yaml 粘贴在下面。

trigger:
- master

variables:

  azureSubscription: 'AzureServiceConnection'

  # Web app name
  webAppName: BasicReactApp

  # Environment name
  environmentName: Dev

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - task: NodeTool@0
      inputs:
        versionSpec: '16.x'
      displayName: 'Install Node.js'

    - script: |
        cd reactonazure
        npm install
        npm run build --if-present
      displayName: 'npm install, build '

    - script: |
        cd reactonazure
        CI=true npm test -- --reporters=jest-junit --reporters=default --coverageReporters=cobertura 
      displayName: 'test code coverage'


    - task: PublishTestResults@2
      displayName: "Publish Test Results"
      inputs:
        testResultsFormat: 'JUnit'
        testResultsFiles: 'reactonazure/junit.xml'
        failTaskOnFailedTests: true
        mergeTestResults: true
      condition: succeededOrFailed()

    # The following, two are not functioning as no coverage.cobertura.xml file is being created.
    - task: PublishCodeCoverageResults@1
      displayName: "Publish code coverage"
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: "reactonazure/$(System.DefaultWorkingDirectory)/coverage/cobertura-coverage.xml"
        reportDirectory: "reactonazure/$(System.DefaultWorkingDirectory)/coverage"
        failIfCoverageEmpty: false

    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage report'
      inputs:
        codeCoverageTool: 'Cobertura'
        summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
        failIfCoverageEmpty: true

还有其他方法可以生成管道报告吗?

最佳答案

您提供的覆盖文件路径不正确。它应该是$(System.DefaultWorkingDirectory)/reactonazure/coverage/cobertura-coverage.xml

任务看起来像这样:

- task: PublishCodeCoverageResults@1
  displayName: "Publish code coverage"
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: "$(System.DefaultWorkingDirectory)/reactonazure/coverage/cobertura-coverage.xml"
    reportDirectory: "$(System.DefaultWorkingDirectory)/reactonazure/coverage"
    failIfCoverageEmpty: false

关于node.js - Azure Devops 上的 React App 的 Cobertura 格式代码覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69469877/

相关文章:

javascript - Meteor.call 并将对象传递给渲染组件

javascript - 如何在 React 中显示进度值

azure - 如何将二进制文件上传到Azure管道并进行签名?

node.js - 在 Node 配置中使用 gradle-node-plugin 或 force in 时如何设置 "--max-old-space-size"?

node.js - 使用 Postman 将数据发送到 Google Cloud Function

javascript - JavaScript .on() 方法是如何定义的?

javascript - Codemirror lint 功能在 React/Redux/Typescript 应用程序中不起作用 react-codemirror

javascript - 无法使用 sudo n stable 技巧更新 Nodejs,无法统计错误(Ubuntu 14.04)

c# - 根据工作日安排 Azure Functions

nuget - 将私有(private) Nuget 包源添加到 Visual Studio Online