jestjs/jest

The "BeforeAll" works later than "it.concurrent", but must work before

Dmytro-Komaryst opened this issue ยท 10 comments

I'd like to run my Jest tests concurrently, but I'm having issues with one scenario:

I'm testing the results on an endpoint, and I want to test multiple things about it. So in my beforeAll function, I make the request and store the response, and then I test the response in multiple tests. This works fine synchronously, but when I make the tests concurrent, it no longer lets you pass a variable into the test, so it's a no go. Alternatively, I can put the request in the test itself and then expect many things about the response, but then I don't have the granularity to see what went wrong if something fails.

Is there any solution for this scenario?

//This works:

let data;
beforeAll(async () => {
    data = await getDataFromRequest();
}
it('value1 should be truthy', () => {
    expect(data.value1).toBeTruthy();
}
it('value2 should be truthy', () => {
    expect(data.value2).toBeTruthy();
}

//This also works:

it.concurrent('data should have correct values', async () => {
    const data = await getDataFromRequest();
    expect(data.value1).toBeTruthy();
    expect(data.value2).toBeTruthy();
}

//But what I want is:

let data;
beforeAll(async () => {
    data = await getDataFromRequest();
}
it.concurrent('value1 should be truthy', () => {
    expect(data.value1).toBeTruthy();
}
it.concurrent('value2 should be truthy', () => {
    expect(data.value2).toBeTruthy();
}

Additional check (the BeforeAll worked after test.concurrent)

describe('', () => {

    beforeAll(() => {
        console.log('start beforeAll') 
    })

    it.concurrent('', async () => {
        console.log('start it.concurrent')
        expect(1).toBe(1)
    })
})

Result:
$ jest
PASS ..\1.test.js
โˆš

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.938s
Ran all test suites.
console.log ..\1.test.js:8
start it.concurrent

console.log ..\1.test.js:4
start beforeAll

that's not possible right now. test.concurrent is very complex and there's tons of things that won't work with it.
for your specific case you can do something like this:

const dataPromise = getSomeDataPromise();

test.concurrent('one', async () => {
  const data = await dataPromise;
});

test.concurrent('two', async () => {
  const data = await dataPromise;
});

this way they'll wait on the same promise that will be executed before everything

@aaronabramov are there any plans on supporting it though? How about afterAll, will it execute in an unexpected order as well?

@jeffijoe not in the nearest future.
afterAll should work as expected though, because it'll wait for all tests to be completely done before it runs

@aaronabramov Is this issue added to backlog ? appreciate your response

I am wondering why test.concurrent is not shown in the documentation at https://jestjs.io/docs/en/api . Is it no longer available?

@yxliang01 I believe it simply hasn't been documented there. It is mentioned here, however: https://jestjs.io/docs/en/cli#maxconcurrency-num

--maxConcurrency=
Prevents Jest from executing more than the specified amount of tests at the same time. Only affects tests that use test.concurrent.

Why is this closed if it's not fixed?

obviously cause facebook, jest literally feels like it's dead.

Issue persists, please reopen

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.