I am new to Mocha so this might probably be a trivial question but couldn't yet find an answer:
I have a simple NodeJS project with the below package.json
{
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "davide talesco",
"license": "ISC",
"devDependencies": {
"chai": "^4.0.2",
"mocha": "^3.4.2"
}
}
and the following 2 tests files under test folder:
test1.js
process.env.NODE_ENV = 'test';
var chai = require('chai');
var should = chai.should();
describe('Test setProp', function(){
it('env variable should be test', function(done){
process.env.NODE_ENV.should.be.equal('test');
return done();
});
});
test2.js
process.env.NODE_ENV = 'prod';
var chai = require('chai');
var should = chai.should();
describe('Test setProp', function(){
it('env variable should be prod', function(done){
process.env.NODE_ENV.should.be.equal('prod');
return done();
});
});
when I run npm test the first test complete succesfully whilst the second fails as per below
ie-macp-davidt:crap davide_talesco$ npm test
> [email protected] test /Users/davide_talesco/development/crap
> mocha
Test setProp
1) env variable should be test
Test setProp
✓ env variable should be prod
1 passing (16ms)
1 failing
1) Test setProp env variable should be test:
AssertionError: expected 'prod' to equal 'test'
+ expected - actual
-prod
+test
at Context.<anonymous> (test/test1.js:11:36)
npm ERR! Test failed. See above for more details.
its pretty clear that the tests are running under the same process... my question is : how can I make them run under completely separate processes so each one can set its own environment?
Thanks,
Davide
Mocha will run the tests in the order the describe calls execute. Save this answer.
Learn how to run only one test with Mocha To run a single test (as defined in it() ), you can simply call the only() method on it in the following way: it. only('...', function () { // only this test will run... });
Jest is also faster than Mocha. It has built-in support for snapshot testing, which means tests are run automatically on each change to the code. This makes it easy to keep your tests up to date as you work. Mocha has more features out of the box since it is a more mature tool with a larger community of contributors.
One of the most simple ways is to use Unix find
command:
find ./test -name '*.js' -exec mocha \{} \;
I'd recommend to use local mocha
binaries to avoid troubles in case it isn't installed globally:
find ./test -name '*.js' -exec ./node_modules/.bin/mocha \{} \;
If you want to add that to package.json, please note that backslashes should be escaped:
...
"scripts": {
"test": "find ./test -name '*.js' -exec ./node_modules/.bin/mocha \\{} \\;"
},
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With