Jest: Allow setupFiles and setupTestFrameworkScriptFile to be asynchronous

Created on 21 Nov 2016  路  6Comments  路  Source: facebook/jest

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.

Most helpful comment

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

All 6 comments

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

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.

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);

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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pfftdammitchris picture pfftdammitchris  路  76Comments

snapwich picture snapwich  路  77Comments

kirlat picture kirlat  路  81Comments

TYRONEMICHAEL picture TYRONEMICHAEL  路  80Comments

SimenB picture SimenB  路  131Comments