Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Dmytro-Komaryst opened this issue Aug 16, 2017 · 10 comments · Fixed by #12748
Closed

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

Dmytro-Komaryst opened this issue Aug 16, 2017 · 10 comments · Fixed by #12748

Comments

@Dmytro-Komaryst
Copy link

Dmytro-Komaryst commented Aug 16, 2017

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

@aaronabramov
Copy link
Contributor

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

@jeffijoe
Copy link
Contributor

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

@aaronabramov
Copy link
Contributor

@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

@hemanta8841
Copy link

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

@yxliang01
Copy link

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

@techieshark
Copy link

@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.

@yarulan
Copy link

yarulan commented Jun 12, 2019

Why is this closed if it's not fixed?

@gselsidi

This comment has been minimized.

@tobiasfeil
Copy link

Issue persists, please reopen

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants