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

Test environment async setup with transformations #5386

Closed
Vanuan opened this issue Jan 24, 2018 · 4 comments
Closed

Test environment async setup with transformations #5386

Vanuan opened this issue Jan 24, 2018 · 4 comments

Comments

@Vanuan
Copy link
Contributor

Vanuan commented Jan 24, 2018

Do you want to request a feature or report a bug?

A feature: asynchronous test setup should apply transformations.

What is the current behavior?

Currently there are the following options to introduce setup code:

  • globalSetup
  • setupFiles
  • setupTestFrameworkScriptFile

Two of those (globalSetup and setupTestFrameworkScriptFile) enable asynchronous setup:

// jest.config.js
module.exports = {
  globalSetup: "<rootDir>/jest.setupGlobal.js",
  setupTestFrameworkScriptFile: "<rootDir>/jest.setupJasmine.js"
};
// jest.setupGlobal.js
module.exports = async function(){
  return app.init();
}

// jest.setupJasmine.js
jasmine.getEnv().topSuite().beforeAll({
        fn: function(done) {
               app.init().then(() => done());
        }
});

Unfortunately, neither of these methods satisfy requirements.

The first method doesn't allow babel transformations. It looks like async code executed in the context of main process, not in the context of worker process.

In the second method it is executed in the context of process, but it's already too late as beforeAll is executed after the test is imported.

What is the expected behavior?

I need a way to execute async setup code before the test module is imported, so that the code in test module is run after my app is initialized. Current globalSetup option doesn't apply transformations.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

Jest 22
Node 8
Yarn 1

@Vanuan
Copy link
Contributor Author

Vanuan commented Jan 24, 2018

It looks like the only workaround is to mock/refactor my init function to be synchronous :(
Alternatively, I could enable --harmony_modules so that babel transformations are not needed. But still, it's not clear whether state in globalSetup would somehow impact state inside test.

@Vanuan
Copy link
Contributor Author

Vanuan commented Jan 25, 2018

Hm, looks similar to #5164

@thymikee
Copy link
Collaborator

Yup, looks like a dupe of #5164. Wanna send a PR with a fix? :)

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

2 participants