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

Support for spawned processes/threads #20

Open
2 tasks
kunal-kushwaha opened this issue Jul 2, 2020 · 2 comments
Open
2 tasks

Support for spawned processes/threads #20

kunal-kushwaha opened this issue Jul 2, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects

Comments

@kunal-kushwaha
Copy link
Member

kunal-kushwaha commented Jul 2, 2020

Summary

Jest implements a sandbox for every test allowing you to run tests in isolation, and within that sandbox allows you to modify module resolution, globals and so on without worrying how it will impact other tests. But if people spawn subprocesses or worker threads, the sandbox is broken out of, and you lose support for Jest’s features like mocks and fake timers. In addition, we’re unable to collect code coverage for spawned processes. This project would explore ways for Jest to intercept subprocesses or worker threads so it can provide the same features it does in the main process. One avenue to explore is if V8 coverage allows Jest to collect coverage.

Deliverables

  • Support for code coverage in subprocesses/worker threads
  • Injecting Jest’s module mocking and resolution into the spawned children

Expected behavior

Jest should correctly output coverage for forked processes.

Observed behavior

Jest outputs no coverage for forked processes.

Repo that reproduces the issue

https://github.com/mnmkng/nyc-jest-issue

References

jestjs#5274

Test Plan

TBD

@kunal-kushwaha kunal-kushwaha added enhancement New feature or request help wanted Extra attention is needed labels Jul 2, 2020
@SimenB
Copy link
Collaborator

SimenB commented Jul 2, 2020

As I think I mentioned it the call we had earlier - this is the area I've put the least research into, and I believe it to be the toughest/most involved project provided to you. I'll try to string some thoughts together, though!

The way Jest currently runs a test is to set up a single jest-runtime, which loads the test file with module resolution, loading etc.. For subprocess/thread to work, we'd essentially have to create a secondary jest-runtime just for the spawned child. Then somehow report back code coverage to the parent process afterwards. If we can make a second runtime work, I assume reporting back some JSON is relatively trivial.

I'm not sure how to best hook into the spawned process, tho. nyc uses node-preload - looking into that implementation might make sense, although it mentions in the README it doesn't support worker threads.
If we can support just subprocesses and not threads for now, some support is way better than no support though. And it would still allow us to land the other parts (like reporting back, merging results etc.).

However, I think the approach we need to make is mock out child_process and worker_threads (similar to what we do with module) and replace the spawning with our own custom one, which starts up a new runtime in a thread and then loads the JS file. We need to avoid spawns for non-none programs etc.. I don't know how feasible/scalable that is though.

One open question is whether we should inject expect and the other globals - I'm thinking "no", but I haven't put much thought into it.

@kunal-kushwaha kunal-kushwaha added this to To do in MLH x Jest Jul 6, 2020
@rbuckton
Copy link

Would the goals for this project include allowing preprocessors like ts-jest the opportunity to perform preprocessing/transformation as well? I currently have to work around this limitation when using jest+ts-jest by injecting ts-node/register into a worker_thread.Worker that needs to import TypeScript code in the same project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
MLH x Jest
  
To do
Development

No branches or pull requests

4 participants