question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run each test case in a separate process / better isolated context?

See original GitHub issue

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

Request a feature.

What is the current behavior?

Currently, tests in a single test source file are run one after the other in the same process / context. One can use resetModules and resetMocks (disabled by default!?) to reduce the chances of one test to affect another test.

One of the problems with resetModules and resetMocks is that this doesn’t jive very well with having ES6’ top-level import declarations in your test source file. They create references to the initial module / mock instances that resetModules and resetMocks won’t be able to “reset” to new instances. Someone had already filed a ticket for that in the past: https://github.com/facebook/jest/issues/3236

Instead of trying to reset the state after each test case (which is hard!), I think it’s better to run each test case in “true isolation” in a separate node.js process (or perhaps a lighter way of isolating, I’m not sure what it would be though).

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

Using:

  • Jest v22.2.2
  • Node v8.9.1
  • Yarn v1.3.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
sseletskyy-apixiocommented, Apr 23, 2019

jest has a built in flat to run all tests sequentially

jest --runInBand

alias -i

I used that for running just test against storybook+puppeteer environment (e.g. npm lib spdt)

2reactions
hugollmcommented, Mar 28, 2019

I actually needed this for some tests with puppeteer. I’m probably better off refactoring the setup or test logic, but meanwhile I got isolation with:

jest --listTests | xargs -L 1 -P 0 jest

This runs each file as a separate jest command, so each of my test files spawn a new puppeteer context, solving my problem for now. Figured I would post this here in case it helps someone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running tests in separate processes - wp-browser
Running tests in a separate process provide isolation at the cost of speed, only tests that require isolation should run in a separate...
Read more >
Is it possible to run each testcase in a separate process?
Here is a hack that you can use: force each test to die at the end. Say you want to test a function...
Read more >
What information/context are shared between Apex tests ...
Salesforce is supposed to ensure that each test's environment is isolated from all others. However, there are definitely cases where there ...
Read more >
Setup and Teardown - Jest
Jest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do...
Read more >
ExUnit.Callbacks — ExUnit v1.15.0-dev - HexDocs
Functions used to define a test setup must accept the context as single argument. ... Finally, setup_all callbacks run in a separate process...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found