DEV Community

Dmitri Pisarev πŸ‡·πŸ‡Ί
Dmitri Pisarev πŸ‡·πŸ‡Ί

Posted on

3 1

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

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay