-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
runInBand + dynamic imports break in Jest 27.0.0-next.11 #11438
Comments
I tried this code on my Mac
but i don't see the issue. How do you run into it? |
After some further debugging this seems to be what causes the flaky behavior: const module = await import(path).then(r => r.default) Omitting either the EDIT: I may have been too hasty in this conclusion. Doing some further testing. |
@ahnpnl could you try this snippet in watch mode. If it doesn't fail on first test, try hit enter to trigger another test. test('test1', async () => {
const res= await import('some/file.js') //not package.json, but a .js file
expect(res).toBeTruthy()
}) |
hmm i still don't see the issue, what if you clear Jest cache and try again? |
Could you put together a minimal repository showing the error? |
https://github.com/easy-demo/ts-jest-issue/tree/await-import
|
It's not about async/await, it will fail to test once we use |
seems related to #10944 i'm running into the same issue with ESM and --runInBand and think i'm running into a similar issue as seen in #10944 (comment)
|
@azban is there a workaround for this? I have it either with |
i haven't found a workaround, but also am not seeing this issue with |
Could this be related to #11601? Short summary is in LinqLover/downstream-repository-mining#65 (comment) or here:
That is, I don't get actual error messages from the dynamic imports, but by adding them to my project I create a small likeliness that jest will fail without executing all the tests. |
Hi there. I'm also experiencing this issue, and made a stable reproduction repository here As you can see, I've stripped down all my code to the minimum: a module (index.js) dynamically importing another one (internals.js). As long as I had a single test file importing I've lost considerable time digging it, without finding any workaround nor root cause, but my gut feeling is that the jest-node-environment is tear down, then reused. Its VMcontext is gone, preventing further dynamic imports to work. |
Going one step further: by placing console logs within jest-runtime Basically, each test file has its own Node environment: it is configured before running tests within the file, and tore down at the end.
I've tried to import all my modules dynamically, to use |
@SimenB would you be able to take a look at @feugy 's reproductible, #11438 (comment). |
This is almost certainly nodejs/node#36351 or nodejs/node#33439 (either way it's further upstream: https://bugs.chromium.org/p/v8/issues/detail?id=10284) |
I have the same problem. |
Again, this is a bug in V8, there is nothing we can do except wait for Google to fix and release it, then Node to update their version of V8 and backport it to v12, v14 and v16 |
@SimenB but it works without |
cache is probably only shared within a worker/process, so using multiple workers doesn't have the same issue |
It's rather strange that I have different results in my local environment and Github Actions runner. The same OS (Ubuntu), nodejs version and run command. |
Seeing as we're probably going to be stuck with this bug for a while, does anyone have a workaround? |
@jakobrosenberg, i think this is related to the issue I have with esbuild compiling jest. |
A workaround I'm using is to run each test file separately, rather than telling Jest to run everything in a dir tree. It's kludgy, but until V8 fixes this, so that Node can fix it, so that Jest can work with it, not a lot of choice if you don't want to run into caching issues: "scripts": {
"test": "run-s test:jest:*",
"test:jest": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --verbose=false",
"test:jest:models": "npm run test:jest -- test/models/model.test.js",
"test:jest:migration": "npm run test:jest -- test/migration/migration.test.js",
"test:jest:...": "...",
"...": "...",
},
"devDependencies": {
"cross-env": "^7.0.3",
"jest": "^27.2.2",
"npm-run-all": "^4.1.5",
"...": "...",
} With testing code that relies on dynamic imports: import { Models } from "../../index.js";
import { User } from "./user.model.js";
describe(`Testing User model`, () => {
beforeAll(async () => {
await Models.useDefaultStore(`./data-store`); // this falls through to a dynamic import
Models.register(User);
});
...
}); with the operative dynamic import in Models: static async useDefaultStore(path) {
const { FileSystemStore } = await import("./store/filesystem-store.js");
Models.setStore(new FileSystemStore(path));
} But even then, this workaround probably still has pitfalls that I've simply not run into (yet). |
I'm also facing the same issue with Workaround is to disable
@SimenB it seems like that for ESM we have to instantiate new worker per each test file to workaround this issue at least for thoose who use Node.js < 16.11.0 |
Migrated test from Tap to Jest. File Path: packages/cactus-test-cmd-api-server/src/test/typescript/integration/runtime-plugin-imports.test.ts ======================================================================= This is a flaky test case that can't be reliably reproduce. I've ran the test individually 40 times and all 40 times it has passed successfully. This error, however, seems to pop up only when the `yarn jest` command is run (when I ran the test individually). Based on the linked ["hint"](https://stackoverflow.com/a/50793993) from the issue, it seems like using `jest.useFakeTimers()` a good solution. However, using the `jest.useFakeTimers()` with`async/await` was not recommended. However, I found a [different source] (https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4#file-test-js-L58) that showed examples of how to use the `jest.useFakeTimers()`. Our original problem, however, is probably because the after the test environment is torn down, the second test file is trying to import files from the first environment, triggering the error ([explained here](jestjs/jest#11438 (comment))). So I've come to the solution of installing another node package: [node-cleanup](https://www.npmjs.com/package/node-cleanup) and running that within the afterAll at the very end of the test. My laptop can't take running the `yarn jest` script too much so I haven't seen it reproduce the error yet. (WOT ruhrow) _______________________edit below _________________________ So it turns out that this flaky test due to libraries trying to third libraries trying to run after the test runs is a simple fix that was documented [here](https://testing-library.com/docs/using-fake-timers/) where we just add the `useRealTimer` in the `afterEach`. Lol so sorry!! ======================================================================= This is a PARTIAL resolution to issue hyperledger-cacti#238 Fixes hyperledger-cacti#1667 Signed-off-by: awadhana <[email protected]> Signed-off-by: Youngone Lee <[email protected]> Signed-off-by: Peter Somogyvari <[email protected]>
Migrated test from Tap to Jest. File Path: packages/cactus-test-cmd-api-server/src/test/typescript/integration/runtime-plugin-imports.test.ts ======================================================================= This is a flaky test case that can't be reliably reproduce. I've ran the test individually 40 times and all 40 times it has passed successfully. This error, however, seems to pop up only when the `yarn jest` command is run (when I ran the test individually). Based on the linked ["hint"](https://stackoverflow.com/a/50793993) from the issue, it seems like using `jest.useFakeTimers()` a good solution. However, using the `jest.useFakeTimers()` with`async/await` was not recommended. However, I found a [different source] (https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4#file-test-js-L58) that showed examples of how to use the `jest.useFakeTimers()`. Our original problem, however, is probably because the after the test environment is torn down, the second test file is trying to import files from the first environment, triggering the error ([explained here](jestjs/jest#11438 (comment))). So I've come to the solution of installing another node package: [node-cleanup](https://www.npmjs.com/package/node-cleanup) and running that within the afterAll at the very end of the test. My laptop can't take running the `yarn jest` script too much so I haven't seen it reproduce the error yet. (WOT ruhrow) _______________________edit below _________________________ So it turns out that this flaky test due to libraries trying to third libraries trying to run after the test runs is a simple fix that was documented [here](https://testing-library.com/docs/using-fake-timers/) where we just add the `useRealTimer` in the `afterEach`. Lol so sorry!! ======================================================================= This is a PARTIAL resolution to issue #238 Fixes #1667 Signed-off-by: awadhana <[email protected]> Signed-off-by: Youngone Lee <[email protected]> Signed-off-by: Peter Somogyvari <[email protected]>
Migrated test from Tap to Jest. File Path: packages/cactus-test-cmd-api-server/src/test/typescript/integration/runtime-plugin-imports.test.ts ======================================================================= This is a flaky test case that can't be reliably reproduce. I've ran the test individually 40 times and all 40 times it has passed successfully. This error, however, seems to pop up only when the `yarn jest` command is run (when I ran the test individually). Based on the linked ["hint"](https://stackoverflow.com/a/50793993) from the issue, it seems like using `jest.useFakeTimers()` a good solution. However, using the `jest.useFakeTimers()` with`async/await` was not recommended. However, I found a [different source] (https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4#file-test-js-L58) that showed examples of how to use the `jest.useFakeTimers()`. Our original problem, however, is probably because the after the test environment is torn down, the second test file is trying to import files from the first environment, triggering the error ([explained here](jestjs/jest#11438 (comment))). So I've come to the solution of installing another node package: [node-cleanup](https://www.npmjs.com/package/node-cleanup) and running that within the afterAll at the very end of the test. My laptop can't take running the `yarn jest` script too much so I haven't seen it reproduce the error yet. (WOT ruhrow) _______________________edit below _________________________ So it turns out that this flaky test due to libraries trying to third libraries trying to run after the test runs is a simple fix that was documented [here](https://testing-library.com/docs/using-fake-timers/) where we just add the `useRealTimer` in the `afterEach`. Lol so sorry!! ======================================================================= This is a PARTIAL resolution to issue hyperledger-cacti#238 Fixes hyperledger-cacti#1667 Signed-off-by: awadhana <[email protected]> Signed-off-by: Youngone Lee <[email protected]> Signed-off-by: Peter Somogyvari <[email protected]>
this also happens without |
This problem still exists. |
For jest users: Try running jest --clearCache before running your actual tests (equals to disable jest's transform cache). This may help in some situations. If this works for you, you can turn off jest's cache ability in jest.config with cache: false. |
I cannot reproduce this from any of the provided reproductions. If anybody still encounters this issue, please try with a current Node nightly (https://nodejs.org/download/nightly/) or with Node 21 when it comes out in October. |
Consistently running into a segfault coming from Node as a result of cosmiconfig attempting to run a dynamic import on the listed .js config file caused by: nodejs/node#35889 jestjs/jest#11438 Using cosmiconfigSync to workaround this which will fall back to using a synchronous require() call.
Fixes #118 Introduces `cosmiconfig` as requested in #118 in order to support loading the config from alternative file formats like: ``` .unimportedrc.js .unimportedrc.yml package.json > "unimported" key ``` I'm using `cosmiconfigSync` utilities instead of the async version, despite them being available since the `getConfig` is an async method. When we use the async equivalent, we hit a segfault in Node as a result of `cosmiconfig` trying to call a dynamic import on the file within a Jest context that doesn't allow it to. Patched in a recent version of Node, and once the test infra can require the latest version it should be fine to switch to the async version. See nodejs/node#35889 and jestjs/jest#11438 I haven't made any efforts to change the `update` function to write updates to the loaded files, as this would be difficult/impossible to update something like a .js or .yml (if using features like anchors) in a meaningful way. A few notes on the other changes included: 1. `cosmiconfig` pulls in a newer version of TypeScript which was incompatible with the version of `@types/node` we used, updated it 2. One of the tests produced invalid JSON to a config file, which failed silently before and passed the test, but now fails loudly when `cosmiconfig` tries to read and parse the JSON. Updated it to be valid to fulfill the spirit of the test. --------- Co-authored-by: Stephan Meijer <[email protected]>
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. |
🐛 Bug Report
Running jest in parallel works fine, but with
runInBand
enabled, all tests with dynamic imports fail with the errorTest environment has been torn down
Often, if I run a single test, the test will work the first time and then error on subsequent tests. Changing the imported file will fix the test on the next run, but then fail on the subsequent ones.
To reproduce
Run the above test with
--runInBand --watch
. Once finished, click enter to trigger another run.Expected behavior
Test should pass on subsequent runs, but instead only the first test passes and subsequent runs break.
envinfo
Verified on:
Windows, Mac, Ubuntu (Github action), WSL
Node: 14 & 15
NPM: 6 & 7
The text was updated successfully, but these errors were encountered: