Skip to content

Instantly share code, notes, and snippets.

@billiegoose
Created May 6, 2016 21:46
Show Gist options
  • Save billiegoose/c4b3c3d2cdb0b4f9e60ee5c6bbaa5795 to your computer and use it in GitHub Desktop.
Save billiegoose/c4b3c3d2cdb0b4f9e60ee5c6bbaa5795 to your computer and use it in GitHub Desktop.
Mocha users need to watch out for this surprising behavior

Mocha does not reload modules when running multiple tests. This can lead to some curious errors that are difficult to understand at first, but have to do with modules that have an internal state of their own. If you have a folder with two files, test1.js and test2.js (shown in gist below), you will find you get different test results even though they are identical except in name. The first one fails, but the second succeeds.

So some state from previous tests can "pollute" later tests. This makes it difficult to write unit tests in isolation with Mocha, because suddenly when you put both tests together in the same directoy, mocha's behavior can break the tests.

$ mocha test1.js test2.js

  my module
    1) get
    √ set

  my module
    √ get
    √ set

  3 passing (19ms)
  1 failing

This also occurs if you are using the --watch functionality. Here I just added some whitespace and hit save - suddenly the test passes.

$ mocha --watch test1.js    

  my module                                             
    1) get                                              
    √ set                         
  1 passing (16ms)                                      
  1 failing                                             
                                                        
  1) my module get:                                     
     AssertionError: 1 == undefined                
                                                        
  my module                                             
    √ get                                               
    √ set           
                                                        
  2 passing (3ms)                                       
                                                        

The upside of all this is mocha runs tests MUCH FASTER when you run all N tests together than if you ran mocha N times, because it doesn't have to reload modules that are already loaded. The downside is you need to be careful and make sure your tests really work individually, together, and in any order. Otherwise, you might find that modifying one test breaks a different test in your test suite, or even renaming a test might break the test suite by changing their alphabetical order (or the order mocha runs them).

Now, armed with this knowledge, stop procrastinating and go write some unit tests!

assert = require('assert')
describe('my module', function() {
it('get', function() {
assert.equal(1, assert.hidden_state)
})
it('set', function() {
assert.hidden_state = 1
})
})
assert = require('assert')
describe('my module', function() {
it('get', function() {
assert.equal(1, assert.hidden_state)
})
it('set', function() {
assert.hidden_state = 1
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment