Davide Talesco
Davide Talesco

Reputation: 212

mocha : how to run multiple JS test files under separate process environment

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

Upvotes: 6

Views: 3758

Answers (3)

Gabriel Furstenheim
Gabriel Furstenheim

Reputation: 3432

Alternatively you can use mocha-parallel-tests.

To install: https://www.npmjs.com/package/mocha-parallel-tests

To use:

  "scripts": {
    "test": "mocha-parallel-tests"
  },

Nice thing is that it is a proper mocha runner so you can configure reports and pass standard mocha configuration like bail.

Upvotes: 0

Will
Will

Reputation: 958

If you want to abort testing as soon as a test file fails, you can do it like this:

find ./test -type f -name "*.js" -exec sh -c 'for n; do ./node_modules/.bin/mocha "$n" || exit 1; done' sh {} +

Upvotes: 0

Phil Filippak
Phil Filippak

Reputation: 673

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 \\{} \\;"
},
...

Upvotes: 5

Related Questions