Run mocha tests individually

My test/'s project is full of mocha tests:

test/
├── a.coffee
└── b.coffee

Say, a.coffee is

console.log 'Executing A test!'
global.foo = 'FOO'

and b.coffee is

console.log 'Executing B test!'
console.log 'Expecting foo not defined:', assert.equal(foo, undefined)

When executing mocha:

$ mocha --compilers coffee:coffee-script test/*
Executing A test!
Executing B test!
Expecting foo not defined: false

It looks like tests share the same global object (which I would like to avoid)...


Is there a way to execute each test individually?

Thank you.

Mocha's author answered this question here: https://github.com/visionmedia/mocha/issues/365#issuecomment-4997480

If the code you are testing behaves differently depending on some global state, then you have to take control of that as part of the tests. You can code some "beforeEach" function that sets whatever global state you require.

You might also want to think about re-factoring your code so that it does not depend on global state. If you can pass parameters or something ( explicit vs implicit ), then you can test with more confidence.

Global state in general makes testing difficult. The Clean Code Talks by googletechtalks are worth watching for anyone interested in writing testable software

  1. Unit Tests
  2. Global State and Singletons
  3. Don't Look For Things