Jest: shared async code between test blocks

dimaip

Dmitri Pisarev 🇷🇺

Posted on May 26, 2020

Jest: shared async code between test blocks

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

💖 💪 🙅 🚩
dimaip
Dmitri Pisarev 🇷🇺

Posted on May 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Testes de Integração
testing Testes de Integração

May 13, 2024

React TDD: First Step
webdev React TDD: First Step

April 24, 2024

Melhorando seus testes com Jest
tdd Melhorando seus testes com Jest

January 10, 2022