Race condition on snapshots when a test timeouts
See original GitHub issue🐛 Bug Report
When I have two tests that use snapshots, if the first timeouts, the content of the first test will be in the snapshot of the second test
To Reproduce
I have made a repository that reproduces the issue :
- Repository : https://github.com/onigoetz/jest-race-condition-repro
- Github Actions failed jest run : https://github.com/onigoetz/jest-race-condition-repro/runs/218697180
The code
/**
* The Dummy code to test
* Just a function that returns the first argument after 600ms
* @param {*} content
*/
function longRunningFunction(content) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(content);
}, 600);
});
}
// This is only needed in the test repro
// If the snapshots don't exist, leave the time for them to be created
// If they exist, fail the test on purpose
const fs = require("fs");
const path = require("path");
const snapshotFile = `${path.basename(__filename)}.snap`;
const snapshotPath = path.dirname(process.cwd(), "__snapshots__", snapshotFile);
if (fs.existsSync(snapshotPath)) {
jest.setTimeout(400);
}
// The tests
it("Runs a first test", async () => {
expect(
await longRunningFunction("Content of the first test")
).toMatchSnapshot();
});
it("Runs a second test", async () => {
expect(
await longRunningFunction("Content of the second test")
).toMatchSnapshot();
});
Run this code twice to see the error
Output
● Runs a second test
expect(received).toMatchSnapshot()
Snapshot name: `Runs a second test 1`
Snapshot: "Content of the second test"
Received: "Content of the first test"
27 | expect(
28 | await longRunningFunction("Content of the first test")
> 29 | ).toMatchSnapshot();
You see that it’s the call to snapshot from the first test that ends in the second test.
Expected behavior
The content of the first test should be in the second snapshot The content of the second test should be in the second snapshot
Link to repro
- Repository : https://github.com/onigoetz/jest-race-condition-repro
- Github Actions failed jest run : https://github.com/onigoetz/jest-race-condition-repro/runs/218697180
envinfo
System:
OS: Linux 4.15 Ubuntu 18.04.3 LTS (Bionic Beaver)
CPU: (8) x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
Binaries:
Node: 8.16.0 - ~/.nvm/versions/node/v8.16.0/bin/node
Yarn: 1.17.3 - /usr/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v8.16.0/bin/npm
npmPackages:
jest: ^24.9.0 => 24.9.0
It’s also the same on GitHub actions on node 8, 10 and 12
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:10
Top Results From Across the Web
How to trace possible async race conditions in my test code?
In the "end-to-end test with one successful query" test, it inserts a document and then passes some assertions.
Read more >Fix race condition for image test · 5b36c36896 - tempest - OpenDev ...
To handle the test scope and this race condition, we need to assert on second ... This can be done via capturing the...
Read more >RaceTestUtils (reactor-test 3.5.1-SNAPSHOT)
Synchronizes the execution of several Runnable s as much as possible to test race conditions. The method blocks until all have run to...
Read more >Fix race conditions in OffCanvasTestBase [#2946294] - Drupal
If we timeout and the link is not clickable after "enough" waiting, we throw the "this ain't clickable" exception and the test will...
Read more >Multi Threaded Tests - Documentation - NCrunch
TestCoverageEventListener - This is caused by a race condition created in the NCrunch runtime code by the background thread trying to record coverage...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Stale maybe, but the bug is still present in Jest 27 : https://github.com/onigoetz/jest-race-condition-repro
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.