-
-
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
[Bug]: Mocking Partials (in with ES6 modules) doesn't work as per documentation #15100
Comments
This works: import { jest } from '@jest/globals';
jest.unstable_mockModule('../foo-bar-baz', () => {
return {
bar: () => 'mocked bar',
};
});
const { bar } = await import('../foo-bar-baz');
test('should do a partial mock', () => {
expect(bar()).toBe('mocked bar');
}); Docs for Trying to make the implementation exactly the same as in the original example, I think this is the correct code: import { jest } from '@jest/globals';
jest.unstable_mockModule('../foo-bar-baz', async () => {
const originalModule = await import('../foo-bar-baz');
return {
...originalModule,
default: jest.fn(() => 'mocked baz'),
foo: 'mocked foo',
};
});
const { default: defaultExport, bar, foo } = await import('../foo-bar-baz');
test('should do a partial mock', () => {
const defaultExportResult = defaultExport();
expect(defaultExportResult).toBe('mocked baz');
expect(defaultExport).toHaveBeenCalled();
expect(foo).toBe('mocked foo');
expect(bar()).toBe('bar');
}); However, the test never finishes. Seems this issue was reported a while ago: #13851 |
As you see CJS has |
It seems that #10025 is tracking a potential fix for this issue (since 2020) Since Should I make a PR? |
Similar code works in Vitest using a special method called Vitest supports partial mocks but has a caveat that's well explained in the docs:
And then goes on to write:
https://vitest.dev/guide/mocking.html#mocking-pitfalls Probably partial mocks should be generally avoided. |
As a side note. Jest and Vitest have very different approach to ESM. In Jest a plain JavaScript ESM is simply executed, in Vitest you will see transform time being reported. I don’t know what is that transformation about, but that is: 1. waste of time; 2. not ESM as implemented by Node.js (for instance |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
1 similar comment
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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. |
Version
29.7.0
Steps to reproduce
Stackblitz playground: https://stackblitz.com/edit/stackblitz-starters-nybxbq
Result:
Follow the documentation available here: https://jestjs.io/docs/mock-functions#mocking-partials
./foo-bar-baz.js
:./tests/test.js
:package.json
is configured correctly (so Jest works with ES6 modules):npm test
Expected behavior
defaultExportResult()
should returnmocked baz
instead ofbaz
Actual behavior
Test fails, and it appears the mocks simply aren't run:
Additional context
Pulling down the StackBlitz and running it locally produces the same result.
Also, in my own repository, I have this same issue.
Environment
This is what my local environment reports:
This is what StackBlitz reports:
System: OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Binaries: Node: 18.20.3 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 10.2.3 - /usr/local/bin/npm pnpm: 8.15.6 - /usr/local/bin/pnpm npmPackages: jest: ^29.7.0 => 29.7.0
The text was updated successfully, but these errors were encountered: