What is the purpose of `beforeEach` global in Jest?

前端 未结 2 2070
别那么骄傲
别那么骄傲 2021-02-20 01:19

I always find Jest-Enzyme test cases starting with a beforeEach global that resembles like the following:

describe(\'TEST BLOCK\' () => {
  let w         


        
2条回答
  •  耶瑟儿~
    2021-02-20 01:39

    Jest documentation recommends beforeEach for tests that consume a particular global state for each test, for example, resetting test data in a database before each test is run.

    Note the following case:

    If beforeEach is inside a describe block, it runs for each test in the describe block.

    Using the same example where we have a database with test data, if your tests do not need the test data reset for each test, you can use beforeAll to run some code once, before any tests run.

提交回复
热议问题