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

Use of Jest CLI in Node scripts #3737

Closed
eldarshamukhamedov opened this issue Jun 5, 2017 · 9 comments
Closed

Use of Jest CLI in Node scripts #3737

eldarshamukhamedov opened this issue Jun 5, 2017 · 9 comments

Comments

@eldarshamukhamedov
Copy link

eldarshamukhamedov commented Jun 5, 2017

Hey all, hopefully someone can help me out with a less common use case for Jest. I am writing a set of build/deploy scripts that start by running Jest, before continuing to compile and dockerize an application. Unfortunately, there does not appear to be a public API to match Jest CLI functionality. How can I run Jest from a Node script?

I did find an example in Create React App's test script. The problem with this approach is that jest.run() spawns a new process. This process will exit cleanly if all tests pass, and errors out if some tests fail. That's good enough for a CLI command, but jest.run() does not return a promise, or event stream, so there's no way to detect when and how the Jest process exits from my code.

The workaround I'm currently using is to spawn a new process that runs Jest. Since I control how the process is spawned, I can wrap it in a promise that rejects/resolves based on the Jest process exit code. This feels a bit clunky, since I am spawning a process for my build script that immediately spawns another process for Jest. Is there a better way? Is there documentation that I missed?

@cpojer
Copy link
Member

cpojer commented Jun 5, 2017

There is an onComplete event that isn't expose to run. Would you mind sending a PR to add that?

@eldarshamukhamedov
Copy link
Author

That would work, and yeah, I can put up a PR. I'm thinking, if you pass jest.run() a callback as the final argument, it could call that with the result, instead of exiting the process.

@rogeliog
Copy link
Contributor

rogeliog commented Jun 8, 2017

@eldarshamukhamedov This is the onComplete callback that runCLI is already using.... I think we just need to pass it all the way down when running jest.run

@eldarshamukhamedov
Copy link
Author

@rogeliog Yep, I got eldarshamukhamedov@931927f, just need to write the tests.

@rogeliog
Copy link
Contributor

rogeliog commented Jun 8, 2017

Awesome, I left some comments

@fijijavis
Copy link

Did this get implemented?

@SimenB
Copy link
Member

SimenB commented Aug 14, 2018

No. Feel free to send a PR 🙂

@SaurabhKhare86
Copy link

I'm also facing this issue. I'm using runCLI function of jest-cli and tests are executing, but unable to know when exactly test run gets finished. I'm resolving the promise with a then function, but my then function code executes first and then the test run completes.

I checked out the source code and looks like there is no event / callback as of now which notifies the complete of test execution.

If this is a open issue - Is there a workaround? It's said above that we can spawn a new process, but I'm new to Node and need some help in figuring out how to achieve that.

To fix this issue, is the approach taken here still correct ?

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

6 participants