describe('some test', function(){ // Could put here a shared variable it('should pass a value', function(done){ done(null, 1); }); it('and then double it', function(value, done){ console.log(value * 2); done(); }); });
The above currently would not work in mocha.
A solution would be to have a variable shared between the tests, as shown above.
With async.waterfall()
this is very possible and I really like it. Is there any way to make it happen in mocha?
Thanks!
According to it, tests are run synchronously. This only shows that ordered code is run in order. That doesn't happen by accident.
Retrying Mocha testsMocha provides a this. retries() function that allows you specify the number of times a failed test can be retried. For each retry, Mocha reruns the beforeEach() and afterEach() Hooks but not the before() and after() Hooks.
This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.
Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.
It is much preferable to keep the tests isolated so that one test does not depend on a computation performed in another. Let's call the test that should pass a value test A and the test that should get it test B. Some question to consider:
Are test A and test B really two different tests? If not, they could be combined.
Is test A meant to provide test B with a fixture to test against? If so, test A should become the callback for a before
or beforeEach
call. You basically pass the data around by assigning it to variables in the closure of describe
.
describe('some test', function(){ var fixture; before(function(done){ fixture = ...; done(); }); it('do something', function(done){ fixture.blah(...); done(); }); });
I've read Mocha's code, and provided I'm not forgetting something, there is no way to call describe
, it
, or the done
callback to pass values around. So the method above is it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With