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

Test files run through node --test seem to use a shared ESM cache (which is not expected and also not very practical) #44595

Closed
timmolendijk opened this issue Sep 11, 2022 · 3 comments
Labels
test_runner Issues and PRs related to the test runner subsystem.

Comments

@timmolendijk
Copy link

timmolendijk commented Sep 11, 2022

Version

v18.8.0

Platform

Darwin Tims-MacBook-Pro.local 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000 arm64

Subsystem

test runner

What steps will reproduce the bug?

As referenced in #44583, node --test appears to run each test file against a single shared ESM cache. This is not only impractical, but also unexpected. The Node docs explicitly state that “each matching test file is executed in a separate child process”, which suggests that each of them runs in a fresh environment.

This does not seem to be the case, as evidenced by the following test case:

mkdir repro && cd repro;
echo 'export const imports = []; imports.push(process.pid);' > shared-dep.mjs;
echo 'import { imports } from "./shared-dep.mjs"; if (imports.length > 1) throw new Error("shared-dep imported more than once:" + imports);' > a.test.mjs;
echo 'import { imports } from "./shared-dep.mjs"; if (imports.length > 1) throw new Error("shared-dep imported more than once:" + imports);' > b.test.mjs;
node --test;
cd .. && rm -r repro;

This will run two test files, each of them importing the same dependency. If they would use separate ESM caches, the imports array would be filled with two entries instead of one, in which case one of the test files would throw an error. As you can witness from running this, no error is being thrown which means that the shared dependency is imported only once.

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

Test run should fail if separate ESM caches are used.

What do you see instead?

Test run does not fail.

Additional information

No response

@MoLow MoLow added the test_runner Issues and PRs related to the test runner subsystem. label Sep 11, 2022
@MoLow
Copy link
Member

MoLow commented Sep 11, 2022

@timmolendijk this reproduction does not show that the two test files share ESM cache:
shared-dep.mjs runs twice in two separate processes and it does not share memory across those processes, so imports will always have a length of 1.

@MoLow
Copy link
Member

MoLow commented Sep 11, 2022

@timmolendijk I suggest converting shared-dep.mjs to:

import { appendFile } from 'node:fs/promises';
await appendFile('tmp.txt', process.pid + '\n');

then inside tmp.txt you can count how many processes imported shared-dep.mjs

@timmolendijk
Copy link
Author

Right! That actually makes a lot of sense. I guess I was confused. Thanks for clearing this up for me.

@MoLow MoLow closed this as completed Sep 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants