‘s project is full of mocha
test/ ├── a.coffee └── b.coffee
console.log 'Executing A test!' global.foo = 'FOO'
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?
Problem courtesy of: abernier
Mocha’s author answered this question here: https://github.com/visionmedia/mocha/issues/365#issuecomment-4997480
Solution courtesy of: abernier
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.
Discussion courtesy of: Nick Perkins
Global state in general makes testing difficult. The Clean Code Talks by googletechtalks are worth watching for anyone interested in writing testable software
Discussion courtesy of: Noah
This recipe can be found in it’s original form on Stack Over Flow