Run the "node test" as part of the Team Team Visual Studio build task with the results in the tests tab

I have a project containing tests that I run using Mocha from the command line. I installed a test script in my packages.json that looks like this:

"test": "mocha ./**/*.spec.js --reporter dot --require jsdom-global/register"

I have a simple task configured in Visual Studio Team Services that simply runs the npm test command, it runs Mocha in the console and continues / does not build depending on whether the tests pass.

What I would like to do is get the results of my tests by filling out the "tests" tab in the assembly definition after it starts. Just like I can get this tab if I run tests in C # code.

I tried using Chutzpah for this, but it is too complex and seems to require me to jump over all kinds of hoops, which meant changing my tests and writing long configuration files. I already have a ton of tests, and I really don't want to do this. When he finally discovered any of my tests, he complained about require and other things related to Node modules.

What I ask is really possible? Is there an easy way to achieve this compatible with running my tests in Node?

+6
source share
2 answers

I found a good way to do this that does not require a third-party adapter (e.g. Chutzpah). This includes getting Mocha to output his report in XML format and setting up Visual Studio Team Services to publish the results in an additional step to define the assembly.

I installed mocha-junit-reporter ( https://www.npmjs.com/package/mocha-junit-reporter ) and changed my test script to the following:

"test": "mocha ./temp/**/*.spec.js --reporter mocha-junit-reporter --require jsdom-global/register"

Then I created a new step in defining my assembly using the Publish Test Results task. I set the result format to "JUnit" and added the correct path for the output file test-results.xml created by the reporter.

It is worth noting that although Mocha comes with the β€œXUnit” reporter, this format does not seem to work correctly with VSTS, although it is listed as an option.

npm test results npm test now displayed on the "tests" tab along with any other tests from MSTest, etc.

+7
source

I use karma and get this to work the way @ dylan-parry suggested. Some of them are below if they help others:

package.json

  "scripts": { "test": "cross-env NODE_ENV=test karma start" } 

karma.conf.js

 const webpackCfg = require('./webpack.config')('test'); module.exports = function karmaConfig(config) { config.set({ reporters: ['mocha', 'coverage', 'junit'], junitReporter: { outputDir: 'coverage', outputFile: 'junit-result.xml', useBrowserName: false } }) ... 

Tfs enter image description here

You can also add that I use branch policies in git branches to prevent PR merging if tests fail, the information on this link:

https://www.visualstudio.com/en-us/docs/git/branch-policies

Here is the output in TFS: enter image description here

The next step is also to get coverage!

+3
source

All Articles