Set up and tear down

Just like other frameworks, Jasmine also provides a way to set up and tear down. Jasmine provides global functions such as beforeEach—called before each spec, afterEach—called after each spec, beforeAll—called only once before all specs are run, and afterAll—called only once after all specs are done. Look at the following code to understand this:

describe("Setup and Teardown", function() { var count = 0; var velocity = 0; beforeEach(function() { velocity = 100; count++; console.log("Count is "+ count); }); afterEach(function() { velocity = 0; console.log("Some spec just finished and this function is called"); }); beforeAll(function(){ console.log("This is called only once, specs are about to run."); }); afterAll(function(){ ...

Get Test-Driven JavaScript Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.