Kick off mocha tests in Visual Studio Team Services Build

2.4k views Asked by At

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.

3

There are 3 answers

0
tonhio On

After quite a bit of fiddling around i got it to work by adding a "Command line task" to my build definition, i used the following parameters:

  • Set Tool to node
  • Set Arguments to $(Build.SourcesDirectory)\node_modules\jasmine-node\bin\jasmine-node --verbose test\

My tests are under a "test" folder, also make sure you have jasmine-node as a dev dependency enter image description here

0
Compulim On

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.

2
jessehouwing On

If you've configured you package.json to be able to run tests, adding a npm step that executes npm run test should do it. If you want to publish the test results you need to make sure that Mocha is writing its results to a format understood by Visual Studio Team Services. JUnit format would be a safe bet. Then follow up with a Publish test Results step that uploads the test results.

You can also use the Visual Studio Test Runner, combined with Chutzpah to run your tests, but I suppose that's going to be a lot of additional work to setup and isn't going to add much.