loading...

Jest: shared async code between test blocks

dimaip profile image Dmitri Pisarev 🇷🇺 ・1 min read

I want to remember this day as the first day I asked something at StackOverflow and receive a brilliant and non-trivial answer!

Check it out, a way to provision a group of tests with some test data asynchronously while getting some data from the provisioning process:

const setupTestContext = testContext => beforeAll(async () => {
  Object.assign(testContext, await setup());
});

...

describe('Some group of tests', async () => {
    const someData = {};

    setupTestContext(someData);

    test('Test1', async () => {
      // context is filled with data at this point
      const actual = myFunc(someData.x)
      ...
    }
    ...
})

https://stackoverflow.com/questions/62019169/jest-shared-async-code-between-test-blocks/62027162#62027162

Discussion

markdown guide