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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -