You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Importing from @jest/globals elsewhere breaks unstable_mockModule #15772
Mocking ../makeId.js from src/unit/__tests__/makeId.test.js works, because src/unit/makeId.ts exists.
Actual behavior
Error message:
FAIL src/unit/__tests__/makeId.test.js
● Test suite failed to run
Cannot find module '../makeId.js' from 'src/setupTests.js'
2 | import { makeId } from '../makeId.js';
3 |
> 4 | jest.unstable_mockModule('../makeId.js', () => ({
| ^
5 | makeId: jest.fn(() => 'aaaa'),
6 | }));
7 |
at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/index.js:863:11)
at unstable_mockModule (src/unit/__tests__/makeId.test.js:4:6)
Additional context
This problem is caused if the @jest/globals module is referenced elsewhere (for example, in a setupTestsAfterEnv file) before it's imported in a test file. The first time @jest/globals is imported, it's synthesized with the path of that file that imported it and cached. All subsequent imports are then bound to the path of whatever first imported it and use that for path resolution, instead of properly resolving paths as relative to their own locations.
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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
Activity
Per-file @jest/globals
Per-file @jest/globals
collinmurd commentedon Aug 6, 2025
I found a workaround to be adding
jest.resetModules()to the end of yoursetupTestsAfterEnvfile (or wherever that first reference is).github-actions commentedon Sep 27, 2025
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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.