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

Allow setupFiles and setupTestFrameworkScriptFile to be asynchronous #2147

Closed
niieani opened this issue Nov 21, 2016 · 7 comments
Closed

Allow setupFiles and setupTestFrameworkScriptFile to be asynchronous #2147

niieani opened this issue Nov 21, 2016 · 7 comments

Comments

@niieani
Copy link
Contributor

niieani commented Nov 21, 2016

Feature request

What is the current behavior?

Currently, setupFiles and setupTestFrameworkScriptFile can only setup environment synchronously.

What is the expected behavior?

It would be great if it Jest would run the test after awaiting any Promise that is the default export of any setupFile or setupTestFrameworkScriptFile the before running each test.

@thymikee
Copy link
Collaborator

thymikee commented Dec 9, 2016

Is there anything that prevents you to await this code in beforeEach() hook?

@niieani
Copy link
Contributor Author

niieani commented Dec 10, 2016

Mostly cleanliness and DRY code. Nothing prevents me from doing a beforeEach() as that's what I'm doing now. However, I need to copy and paste the beforeEach() setup to each unit-test file. The point of setupFiles and setupTestFrameworkScriptFile is, as far as I understand, that common environment setup to all unit tests can be done in a centralized place.

@cpojer
Copy link
Member

cpojer commented Dec 12, 2016

We'll not be adding this for now as the complexity isn't really worth it. The recommended solution is to create a test utils file and use it like so:

const TestUtils = require('TestUtils');
beforeEach(TestUtils.setup);

@cpojer cpojer closed this as completed Dec 12, 2016
@niieani
Copy link
Contributor Author

niieani commented Dec 12, 2016

OK, I understand your position. Thanks for having a look at it. 👍

@tamlyn
Copy link
Contributor

tamlyn commented Apr 12, 2018

Note that it's possible to put a beforeAll hook in your setupTestFrameworkScriptFile which avoids needing to put one in each test suite.

@kirillgroshkov
Copy link

Oh wow, @tamlyn, it really worked for me! My god, for years I didn't know about this solution!

@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 11, 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

No branches or pull requests

5 participants