Awesome
rocha (aka "ROKKA" the Random Mocha)
Runs Mocha unit tests but randomizes their order
E2E tests
Install and use
Should be just like Mocha for most cases
npm install -g rocha
rocha src/*-spec.js
Open an issue if things do not work as expected.
Because I used some pieces of ES6, and Ubuntu does not play nicely with --harmony
flag (which allows using some ES6 today)
this package requires Node >= 4.
Demo screencast
In this demo I am showing how Rocha can find ordering problems in the tests (see the example below).
Example
The tests in spec/tricky-spec.js always pass in Mocha, but only because their execution order is 1 - 2 - 3.
describe('example', function () {
var foo
it('runs test 1', function () {
foo = 42
console.log('polluted the environment')
})
it('runs test 2', function () {})
it('runs test 3', function () {
console.assert(foo === 42, 'foo is 42', foo)
})
})
This tests pass under Mocha but this is very unreliable: a tiny code change can break the tests for no obvious reason. A pain to find the problem too.
Running tests using Mocha
> mocha spec/tricky-spec.js
example
polluted the environment
✓ runs test 1
✓ runs test 2
✓ runs test 3
3 passing (8ms)
Running tests using Rocha
> rocha spec/tricky-spec.js
shuffling 3 unit tests in "example"
example
1) runs test 3
polluted the environment
✓ runs test 1
✓ runs test 2
2 passing (10ms)
1 failing
1) example runs test 3:
AssertionError: foo is 42 undefined
Rocha takes each suite and shuffles its list of unit tests. Given enough test runs this should make visible the problems due to shared data, or polluted environment, or even poor understanding of JavaScript concurrency.
Notes
Not every random order will be
- so random that it is different from sequential
- enough to flush out every problem
Recreating the failed order
If the unit tests fail, the executed order is saved in JSON file .rocha.json
.
For the included example rocha spec/*-spec.js
it will be something like this
[{
"title": "fixed example",
"tests": [
"runs test 1",
"runs test 2",
"runs test 3"
]
}, {
"title": "tricky example",
"tests": [
"runs test 1",
"runs test 3",
"runs test 2"
]
}]
When you start rocha
again, it will find this file and will reorder the tests
in the same order, recreating the failure again.
If the tests pass, the .rocha.json
file is deleted, thus the next run will be random again.
How should we test?
Each unit test should NOT depend on the order the other tests are running. In the above case, refactor the test to reset the variable before each unit test, see spec/fixed.spec.js
describe('fixed example', function () {
var foo
beforeEach(function () {
foo = undefined
})
...
});
Now each unit test starts from the same values (at least in this example).
Options
verbose log - to see diagnostic messages as Rocha runs, set environment variable DEBUG=rocha
when running.
DEBUG=rocha rocha <spec>
During end to end tests, verbose logging is enabled using DEBUG=rocha:e2e
Small print
Author: Gleb Bahmutov © 2015
License: MIT - do anything with the code, but don't blame me if it does not work.
Spread the word: tweet, star on github, etc.
Support: if you find any problems with this module, email / tweet / open issue on Github
MIT License
Copyright (c) 2015 Gleb Bahmutov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.