安装和移除

    如果你有一些要为多次测试重复设置的工作,你可以使用 和 afterEach

    例如,我们考虑一些与城市信息数据库进行交互的测试。 你必须在每个测试之前调用方法 initializeCityDatabase() ,同时必须在每个测试后,调用方法 clearCityDatabase()。 你可以这样做:

    beforeEachafterEach 能够通过与 相同的方式处理异步代码 — — 他们可以采取 done 参数或返回一个 promise。 例如,如果 initializeCityDatabase() 返回解决数据库初始化时的 promise ,我们会想返回这一 promise︰

    1. beforeEach(() => {
    2. return initializeCityDatabase();
    3. });

    在某些情况下,你只需要在文件的开头做一次设置。 如果这个通用设置是异步的,就比较麻烦,因为没办法每个用例都设置一遍,这样性能还很差。 Jest provides beforeAll and afterAll hooks to handle this situation.

    1. beforeAll(() => {
    2. return initializeCityDatabase();
    3. });
    4. afterAll(() => {
    5. return clearCityDatabase();
    6. });
    7. test('city database has Vienna', () => {
    8. expect(isCity('Vienna')).toBeTruthy();
    9. });
    10. test('city database has San Juan', () => {
    11. expect(isCity('San Juan')).toBeTruthy();
    12. });

    顶层的 before*after* hook 函数会应用于文件中的每一条测试 在 describe 块中声明的 hook 函数,只会作用于 describe 中的测试

    比如说,我们不仅有一个城市的数据库,还有一个食品数据库。 我们可以为不同的测试做不同的设置︰

    注意顶级的beforeEach 会比 describe 中的beforeEach 执行的更早。 下面的代码也许能帮助你理解所有 hook 的执行顺序。

    1. beforeAll(() => console.log('1 - beforeAll'));
    2. afterAll(() => console.log('1 - afterAll'));
    3. beforeEach(() => console.log('1 - beforeEach'));
    4. afterEach(() => console.log('1 - afterEach'));
    5. describe('Scoped / Nested block', () => {
    6. beforeAll(() => console.log('2 - beforeAll'));
    7. beforeEach(() => console.log('2 - beforeEach'));
    8. afterEach(() => console.log('2 - afterEach'));
    9. test('', () => console.log('2 - test'));
    10. });
    11. // 1 - beforeAll
    12. // 1 - beforeEach
    13. // 1 - test
    14. // 1 - afterEach
    15. // 2 - beforeAll
    16. // 1 - beforeEach
    17. // 2 - beforeEach
    18. // 2 - test
    19. // 2 - afterEach
    20. // 1 - afterEach
    21. // 2 - afterAll
    22. // 1 - afterAll

    Jest 会在所有真正的测试开始之前执行测试文件里所有的 describe 处理程序(handlers)。 This is another reason to do setup and teardown inside before* and after* handlers rather than inside the describe blocks. Once the describe blocks are complete, by default Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.

    1. describe('describe outer', () => {
    2. console.log('describe outer-a');
    3. describe('describe inner 1', () => {
    4. console.log('describe inner 1');
    5. test('test 1', () => console.log('test 1'));
    6. });
    7. console.log('describe outer-b');
    8. test('test 2', () => console.log('test 2'));
    9. describe('describe inner 2', () => {
    10. test('test 3', () => console.log('test 3'));
    11. });
    12. console.log('describe outer-c');
    13. });
    14. // describe outer-a
    15. // describe inner 1
    16. // describe outer-b
    17. // describe inner 2
    18. // describe outer-c
    19. // test 1
    20. // test 2
    21. // test 3

    Just like the describe and test blocks Jest calls the before* and after* hooks in the order of declaration. Note that the after* hooks of the enclosing scope are called first. For example, here is how you can set up and tear down resources which depend on each other:

    note

    If you are using jasmine2 test runner, take into account that it calls the after* hooks in the reverse order of declaration. To have identical output, the above example should be altered like this:

    1. beforeEach(() => console.log('connection setup'));
    2. + afterEach(() => console.log('connection teardown'));
    3. beforeEach(() => console.log('database setup'));
    4. + afterEach(() => console.log('database teardown'));
    5. - afterEach(() => console.log('database teardown'));
    6. - afterEach(() => console.log('connection teardown'));
    7. // ...

    如果测试失败,第一件要检查的事就是,当仅运行这条测试时,它是否仍然失败。 To run only one test with Jest, temporarily change that test command to a test.only:

    1. test.only('this will be the only test that runs', () => {
    2. expect(true).toBe(false);
    3. });
    4. test('this test will not run', () => {
    5. expect('A').toBe('A');
    6. });