NockBack lockdown mode breaks test isolation in Nock 14 beta
Please avoid duplicates
- I checked all open bugs and none of them matched my problem.
Reproducible test case
https://github.com/tamlyn/nock-beta-bug-repro
Nock Version
v14.0.0-beta.15 & v14.0.0-beta.19
Node Version
22.10.0
TypeScript Version
5.6
What happened?
I'm using Nock Back to record external HTTP requests in tests and replay them. I use nock.back.setMode("lockdown") by default to prevent any unexpected outbound requests (see test/helpers.ts in repro repo for details).
In Nock 13 this works fine. But in 14 beta, I get strange behaviours. Each test file runs fine on its own. But when I run the whole project, the first file passes but the second and subsequent tests fail with a NetConnectNotAllowedError. It's not 100% reproducible with just two test file but it happens most of the time. In my real project with hundreds of test file, I always get some failures but the number differs between runs.
I've tracked down the issue to the call to nock.back.setMode in test/helpers.ts. Removing this line fixes the issue. However I wouldn't expect test suites to influence each other like this. Jest goes to great lengths to run each test file in an isolated environment to prevent such race conditions. And as I mentioned, it works in Nock v13.
Thanks for reading.
Would you be interested in contributing a fix?
- yes
Source: nock/nock