Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kick off mocha tests in Visual Studio Team Services Build

I can't for the life of me find documentation or a tutorial for kicking off mocha unit tests in Visual Studio Online builds.

I have node.js app that is building in VSO and being deployed to Azure. That all works wonderfully. I can't seem to figure out how to kick off the spec files through the build process.

How is this done? Is there documentation available somewhere that I'm missing.

like image 569
Mike M Avatar asked Jun 11 '15 12:06

Mike M


1 Answers

Assume you have setup Mocha tests with your package.json, i.e. you run tests with npm test. For more information, refer to https://docs.npmjs.com/cli/test.

In your Visual Studio Online build/release:

  • Add a "npm" task to install JUnit reporter
    • Run custom command install mocha-junit-reporter
  • Add a "npm" task
    • Run custom command test -- --reporter mocha-junit-reporter
    • Tips: You may want to increase timeout by adding --timeout 30000 because the build agent maybe running slower than your dev box
  • Then, add a "Publish Test Results" task
    • Set "Test result format" to "JUnit"
    • Check the box on "Continue on error"
    • Under "Control Options" > "Run this task", set it to "Even if a previous task has failed, unless the build was canceled"

Queue a build, you should see Mocha test results in your VSO build.

BONUS! You can also add code coverage to your Mocha run with nyc (formerly known as Istanbul)

On top of the steps above:

  • Install Istanbul locally to your package.json
    • Run npm install nyc--save-dev
  • Modify your scripts in package.json
    • Update { "scripts": { "test": "nyc --repoter=cobertura mocha" } }
  • Modify the "npm test" task
    • Run custom command test -- --reporter mocha-junit-reporter
  • Add a "Publish Code Coverage Results" task
    • Set "Code Coverage Tool" to "Cobertura"
    • Set "Summary File" to $(System.DefaultWorkingDirectory)/coverage/cobertura-coverage.xml
    • Set "Report Directory" to $(System.DefaultWorkingDirectory)/coverage/
    • Check the box on "Continue on error"
    • Under "Control Options" > "Run this task", set it to "Even if a previous task has failed, unless the build was canceled"
  • Add a new build variable NPM_CONFIG_COVERAGE and set it to true

Now you got both unit tests and code coverage results in your build report.

like image 75
Compulim Avatar answered Nov 10 '22 18:11

Compulim