Separation of unit tests and integration tests - node.js

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

Related

Mocha executes irrelevant file before test file

In my express app, I'm trying unit testing using Mocha for an independent util class. When kicking off the mocha tests, something strange happened.
Mocha somehow launches the express app before tests.
Since the file that initiates the app is not referenced from any test suite, I think it's weird that Mocha reaches out to it.
Here's my folder structure:
/dist
/index.ts <-- server launching script
/util
/ util.ts <-- independent util class
/ util.test.ts <-- tests for util.ts
/package.json
In package.json I define something like:
"test:unit": "mocha --require source-map-support/register 'dist/**/*.test.js'"
And when I run it, the log output is something like:
> my-project#1.0.0 test:unit
> mocha --require source-map-support/register 'dist/**/*.test.js'
info: server started
Util class
✔ Test case 1 (64ms)
✔ Test case 2 (62ms)
✔ Test case 3 (122ms)
3 passing (258ms)
info: server listening on port 3333
(And Mocha never exits!!!!)
Currently, the --exit flag makes it possible to forcefully quit the tests after finishing. But it's still a mystery why Mocha behaves like this. Does anyone know the reason? Any answers would be appreciated.

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

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

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!

Resources