Search code examples
node.jsjunitazure-devopsmocha.js

How to Publish Test Results in Azure DevOps


I have created the below YAML file with the PublishTestResults task to display the mocha test results in Azure DevOps portal

# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

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

- script: |
    npm install
    mocha test --reporter mocha-junit-reporter
    npm test
  displayName: 'npm install and test'

- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testRunner: JUnit
    testResultsFiles: '**/TEST-RESULTS.xml'

- task: ArchiveFiles@2
  displayName: 'Archive $(System.DefaultWorkingDirectory)'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/node.zip'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'

but whenever I run the build I am getting the following warning

"No test result files matching **/TEST-RESULTS.xml were found."

The main motive is to display the mocha test results separately in a dashboard or a test tab. So that we don't have to check the test task in build process to see the test results.


Solution

  • I encountered same issue, After trying bunch of things could solve it as below.

    Step 1. Update package.json

    1. Include in scripts section:

      "test": "cross-env CI=true react-scripts test --env=jsdom
      --testPathIgnorePatterns=assets --reporters=default --reporters=jest-junit"
      
    2. Include below in jest config or jest section in package.json

      "jest-junit": {
          "suiteNameTemplate": "{filepath}",
          "outputDirectory": ".",
          "outputName": "junit.xml"
      }
      

      Run npm run test locally and see if junit.xml is created.

    Step 2. In Azure Pipeline YAML file

    1. Include an Npm task with custom command as below

      - task: Npm@1
        displayName: 'Run Tests'
        inputs:
          command: 'custom'
          workingDir: "folder where your package.json exists"
          customCommand: 'test'
      
    2. Task to publish Results

      - task: PublishTestResults@2
        displayName: 'Publish Unit Test Results'
        condition: succeededOrFailed()
        inputs:
            testResultsFormat: 'JUnit'
            testResultsFiles: '**/junit.xml'
            mergeTestResults: true
            failTaskOnFailedTests: true
            testRunTitle: 'React App Test'
      

    Publish Test Results