Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeforeAll and AfterAll in javascript test cases

I want do something before all tests, then after? What is the best way to organize my code? For example: backup some variables -> clear them -> test something -> restore backups. 'beforeEach' and 'afterEach' are too expencive. Thanks!

like image 856
user2996905 Avatar asked Feb 12 '14 11:02

user2996905


People also ask

What is beforeAll in JavaScript?

beforeAll(functionopt, timeoutopt)Run some shared setup once before all of the specs in the describe are run.

What is the difference between beforeAll and BeforeEach?

Given the articles I found that BeforeAll is called once and only once. While the BeforeEach is called before each individual test.

What is afterAll in Jasmine?

afterAll(functionopt, timeoutopt)Run some shared teardown once after all of the specs in the describe are run. Note: Be careful, sharing the teardown from a afterAll makes it easy to accidentally leak state between your specs so that they erroneously pass or fail.

What is setUp and teardown?

setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.


1 Answers

A pretty simple solution:

describe("all o' my tests", function() {

  it("setup for all tests", function() {
    setItUp();
  });

  describe("actual test suite", function() {

  });

  it("tear down for all tests", function() {
    cleanItUp();
  });

});

This has the advantage that you can really put your setup/teardown anywhere (eg. at the begining/end of a nested suite).

like image 149
edan Avatar answered Sep 19 '22 14:09

edan