Run “node test” as part of Visual Studio Team Services build task with results in “tests” tab

后端 未结 2 357
生来不讨喜
生来不讨喜 2021-02-03 14:54

I have a project that contains tests that I am running with Mocha from the command line. I have set up a test script in my packages.json, which looks as follows:

相关标签:
2条回答
  • 2021-02-03 15:20

    I'm using karma and got this to work in the same way as @dylan-parry suggested. Some excepts below in case it helps 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

    It may also be worth adding I'm using branch policies on my git branch to prevent PR's being merged if the tests fail, info via this link:

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

    Here's the output in TFS:

    Next step is to get the coverage working too!

    0 讨论(0)
  • 2021-02-03 15:24

    I've found a good way of doing it that requires no third-party adapter (eg. Chutzpah). It involves getting Mocha to output its report in an XML format, and setting up Visual Studio Team Services to publish the results in an extra step of the build definition.

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

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

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

    It is worth noting that although Mocha comes with an "XUnit" reporter, this format appears to not work correctly with VSTS even though it's listed as an option.

    The results of npm test now show up in the "tests" tab alongside any other tests from MSTest etc.

    0 讨论(0)
提交回复
热议问题