Jest run async function ONCE before all tests

后端 未结 4 1140
名媛妹妹
名媛妹妹 2021-02-05 02:45

I want to use jest for my server unit testing (instead of mocha+chai). Is there a way I can run async function before all tests start (init purposes) only once and not for every

4条回答
  •  情歌与酒
    2021-02-05 03:10

    Jest provides beforeAll and afterAll. As with test/it it will wait for a promise to resolve, if the function returns a promise.

    beforeAll(() => {
      return new Promise(resolve => {
        // Asynchronous task
        // ...
        resolve();
      });
    });
    

    It also supports callback style, if you have some existing test code that uses callbacks, although it's recommended to use promises.

    beforeAll(done => {
      // Asynchronous task
      // ...
      done();
    });
    

提交回复
热议问题