-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Async setupFiles #11038
Comments
If you use ESM you can use top level await. So if you name your setup file Not to say we shouldn't support exported functions from setup files, but I think it's cleaner with TLA. Thoughts? |
ESM & TLA sounds like a decent option, didn't think about that 👍 Does Jest wait for it to finish before continuing? |
Yes, all ESM module resolution and loading is async and Jest awaits it. |
It seems that .mjs would be quite hacky solution and it does not have all the necessary variables like |
I would want to second the feature proposal. In my view it is quite common that you have async operations for setting up your tests. Those could be as already mentioned DB connection handling or reading credentials from an external secret store, ... |
This sounds exactly what I need and cannot currently find an alternative solution. I am testing a lambda function that sets up a database connection on load, e.g. // src/handlers/graphql.ts
const loadingConnection = setupDatabase();
export const handler = async (
event: APIGatewayProxyEvent,
context: Context,
callback: Callback
): Promise<APIGatewayProxyResult> => {
const connection = await loadingConnection;
... Then because my test is importing the file, e.g. // src/handlers/graphql.spec.ts
import { handler } from './graphql';
describe('handler', () => {
it('handles graphql queries', async () => { If I had the database connection set up in the handler, I am able to create the database in a setupFilesAfterEnv file, e.g. // src/test/setupFilesAfterEnv/database.setup.ts
beforeAll(async () => {
const dbname = ['graphql', process.env.NODE_ENV, process.env.JEST_WORKER_ID].filter(Boolean).join('-');
const connectionOptions = await getConnectionOptions();
await createDatabase({}, {...connectionOptions, dbname});
}); However this doesn't work as the beforeAll runs after the handler file is imported, e.g.
If anyone has any suggestions, even hacky temporary ones it would be much appreciated! |
Trivial to support calling a function exported from the module if it exists and await the result, so I'll happily merge a PR doing so 🙂 |
@owenbendavies Not sure this is your problem or if you found a solution but my
|
@clintonmedbery thank you for the suggestion, unfortunately it's an order issue rather than a time issue so this doesn't help. |
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. |
🚀 Feature Proposal
setupFiles should support
module.exports = async () => {/* async setup here */}
kind of functionality.Motivation
Async setupFiles would enable setting up new database for each test file and store the database name to config.js file.
Example
config.DATABASE_NAME
is then used in the actual tests to establish the actual connection.Pitch
Why does this feature belong in the Jest core platform?
There is currently no way to achieve this behaviour with synchronous setupFiles nor with custom env, but this would make working with parallel tests using database much easier.
I think this would be the second best option after this: #11039
The text was updated successfully, but these errors were encountered: