Run "node test" as part of Visual Studio Team Services build task with results in "tests" tab - node.js

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:
"test": "mocha ./**/*.spec.js --reporter dot --require jsdom-global/register"
I have currently got a simple task set up in Visual Studio Team Services, which just runs the npm test command, this runs Mocha within a console and continues/fails the build depending on whether the tests pass.
What I'd like to be able to do is have the results of my tests populate the "tests" tab in the build definition after it's run. In the same way that I can get this tab populated if I'm running tests on C# code.
I've tried using Chutzpah for this, but it's overly complicated and seems to require that I jump through all sorts of hoops that mean changing my tests and writing long config files. I already have loads of tests written, so really don't want to have to do that. When it did finally discover any of my tests, it complained about require and other things related to Node modules.
Is what I'm asking for actually possible? Is there a simple way of achieving this that's compatible with running my tests in Node?

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.

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!

Related

Reporting Jest Test run result in a file

I am new to Jest. I am able to set it up and write my Jest tests and execute from the package.json by specifying another target like this in a Node-16.x environment.
"test": "jest -config=jest.config.json"
"test-cov": "jest -config=jest.config.json --coverage"
Now what I want is that: I want to save the run reports in a file -- which I will later on share over email to notify the report.
In effect, what I am looking for are:
In case I am running without --coverage - how can I save the PASS/FAIL report in a file in a specified location (like report_dir/report_service.)
In case I am running with --coverage - how can I save the PASS/FAIL report in a file in a specified location (like report_dir/report__coverage_service.)
The jest configuration one can specify the reports. Even including custom reports also.
The options can be checked Jest Reporting and Jest Coverage Reporting
All the Jest config options can be found here: Configuring Jest

Generate report for mocha with NodeJs

I am using mocha with NodeS but can't understand how i can generate report and show to the team what test cases i have created and which are not runnig also how many test cases should i write how to decide this ?
Mocha reporter documentation here
By default, the reporter is the console output. This output can be modified (as shown in the documentation).
I use mochawesome reporter to see results in a browser.
Quoting the documentation
Mochawesome is a great alternative to the default HTML reporter.
The usage is quite simple: npm i mochawesome and you can execute your test with the parameter --reporter mochawesome.
I use the following line into package.json.
"test": "cross-env NODE_ENV=testing mocha tests --timeout 4000 --reporter mochawesome --exit",
When you execute your test, results will be in a folder named mochawesome-report where will be an .html file.
And that's all.
Exists more third party reporters, even you can create your own reporter.

How to setup mocha to run unit tests first and then start a server and run integration tests

I have unit tests and integration tests that need to be run with one 'npm run mocha' command. The suite before my integration tests was set up and working properly with mocha.opts including the before step like so:
test/beforeTests.js (sets up some important variables)
test/*.x.js (bulk of the tests, all the files ending in .x.js are run)
When including my integrations tests, a unit test fails because the running server clashes with some stubbed functions. Therefore I wanted to run unit tests first, then star the server, run integration tests and close the server. Which I have tried by changing mocha.opts to:
test/beforeTests.js (sets up some important variables)
test/*.x.js (bulk of the tests, all the files ending in .x.js are run)
test/startServer.js (start the server)
test/integration/*.x.js (run integration tests)
test/afterTests.js (close the server)
beforeTests.js:
before(function(done) {
// do something
});
startServer.js:
before(function() {
return runServer()
});
both beforeTests, and startServer work however they are both executed in the beginning of the test instead of
beforeTest > do unit tests > start server > do integration tests > stop server
I cannot merge the integration tests in one file, or the unit tests as there are too many. Is there a way to set up mocha.opts to do what I want. I've looked around and nothing really fits what I want to do.
One of technique that I use before for this is to have two npm commands for unit and integration test then combine them in one npm command if I want to run them in single execution.
"test": "npm run test:unit && npm run test:integration"
"test:unit": "mocha test/unit",
"test:integration": "mocha test/integration"
You can start server as part of integration test.
I suggest to use test for npm test instead of mocha for npm run mocha because it is more standard test command name in node project.
Hope it helps

Is there a good report for jest test case?

I am using jest to test my reactjs application. It works fine but jest doesn't give me a good report about test case results. The output of jest shows each test suite followed by its output. I have more than 50 test suites and it is hard for me to scroll up to check each failed test cases. Is there a brief jest report which printing a brief summary about the whole test cases?
below is my jest.conf file:
{
"testRegex": "/tests/.*\\.test\\.jsx?$",
"testEnvironment": "node",
"roots": ["./src"],
"coverageReporters": ["text-summary", "html"]
}
There is also a JEST HTML Reporter module which you might find useful.
It's configurable to specify what level of detail you want to show in the test report for failures
https://www.npmjs.com/package/jest-html-reporter
You can run jest with the --coverage flag.
If you want something different than the default reporters, you have to set them in your jest config file.
jest.json
{
"coverageReporters": ["text-summary", "html"]
}
text-summary gives you a short summary beneath all tests that tells you how many suites/tests are successful/failed.
html gives you a some html pages that you can browse through to see exactly what got tested.
CLI
$ ./node_modules/.bin/jest --config ./path/to/jest.json --coverage
You might want to adjust which files are covered etc.
See all coverage options in the jest docs.
https://github.com/jest-community/awesome-jest#reporters contains a list of other reporters. jest-stare will let you filter off passing tests so you can just see the ones that failed.
Use Istanbul to generate Reports
In the Script section of the Package.json just add :
"scripts": {
"test": "jest",
"test-coverage": "jest --coverage",
}
Save the project and run below command:
Yarn test : For Terminal Coverage
Yarn test-coverage for Report Generation.Find HTML report in Coverage > Icov-report >index.html
If you can use a web-based reporter try Tesults - a reporter for Jest is available to help with integration: https://www.npmjs.com/package/jest-tesults-reporter.
You main issue, with respect to navigating test suites is handled well. I should add a disclaimer that I work at Tesults, but on the other hand if this is for a very small team or it's open source it is free to use, just contact support.

Separation of unit tests and integration tests

I'm interested in creating full mocked unit tests, as well as integration tests that check if some async operation has returned correctly. I'd like one command for the unit tests and one for the integration tests that way I can run them separately in my CI tools. What's the best way to do this? Tools like mocha, and jest only seem to focus on one way of doing things.
The only option I see is using mocha and having two folders in a directory.
Something like:
__unit__
__integration__
Then I'd need some way of telling mocha to run all the __unit__ tests in the src directory, and another to tell it to run all the __integration__ tests.
Thoughts?
Mocha supports directories, file globbing and test name grepping which can be used to create "groups" of tests.
Directories
test/unit/whatever_spec.js
test/int/whatever_spec.js
Then run tests against all js files in a directory with
mocha test/unit
mocha test/int
mocha test/unit test/int
File Prefix
test/unit_whatever_spec.js
test/int_whatever_spec.js
Then run mocha against specific files with
mocha test/unit_*_spec.js
mocha test/int_*_spec.js
mocha
Test Names
Create outer blocks in mocha that describe the test type and class/subject.
describe('Unit::Whatever', function(){})
describe('Integration::Whatever', function(){})
Then run the named blocks with mochas "grep" argument --grep/-g
mocha -g ^Unit::
mocha -g ^Integration::
mocha
It is still useful to keep the file or directory separation when using test names so you can easily differentiate the source file of a failing test.
package.json
Store each test command in your package.json scripts section so it's easy to run with something like yarn test:int or npm run test:int.
{
scripts: {
"test": "mocha test/unit test/int",
"test:unit": "mocha test/unit",
"test:int": "mocha test/int"
}
}
mocha does not support label or category. You understand correctly.
You must create two folders, unit and integration, and call mocha like this
mocha unit
mocha integration

Resources