vendredi 27 mars 2015

How do I factor out common before() calls with mocha?

I have a lot of duplication in my mocha tests:



describe("Some test", function() {
before(helpers.testPrepare);
beforeEach(helpers.freshDB);

it("should ...", function(done) {
//...
done();
});

after(helpers.teardownServer);
});

describe("Another test", function() {
before(helpers.testPrepare);
beforeEach(helpers.freshDB);

it("should ...", function(done) {
//...
done();
});

after(helpers.teardownServer);
});


This could cause problems if I forget to call beforeEach and it's a pain to refactor. I'd like to define a describe that ensures I always call my setup and teardown, so I can just write:


I would like to write this as:



var describe = require('./helpers').describe;

describe("Some test", function() {
it("should ...", function(done) {
//...
done();
});
});


How can I do this, since mocha is defining describe as a global? How do I ensure everything is executed in the right order?


Aucun commentaire:

Enregistrer un commentaire