node.js - Best way to reuse code at unit testing in javascript -
i'm using buster.js test runner. basic test like:
// test globals var var1='foo1', var2='foo2'; // test run describe('description', function(){ beforeeach(){ console.log('var2'); } it('should ....', function(){ expect(var1).toequal('foo1'); } });
now, imagine have test needs use same beforeeach, plus else, , same it, plus else.
what best approach reuse code in javascript? specially in buster.js or mocha?.
its sort of context need create , encapsulate class .
class testcontext { this.var1 = undefined this.var2 = undefined buildup(next) { // whatever init need next && next() } teardown(next) { //clean stuff next && next() } sharedteststeps() { return [ { text: "it should something", fn: next => { //...do testing } } ] } }
the test looks that:
describe("...", () => { var c = new textcontext() before(next => c.buildup(next)) after( () => c.teardown()) it("should work", () => { //work c.var1 }) c.sharedsteps.foreach({text, fn} => it(text, fn)) })
Comments
Post a Comment