feat: add support unstable_unmockModule#15080
Conversation
✅ Deploy Preview for jestjs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
exciting start, thanks!
Could you:
- add an integration test
- fix CI failure (we need to add the new function to the types in
@jest/environment) - add docs
- add a changelog entry
- sign the CLA
the code itself looks correct, we just need a few things in addition to that 🙂
|
After implementing typings, could you add type tests as well, please? Similar tests of To run the type test: build the library and run By the way, |
|
Where do I add integration test? |
|
Tests of I think you can add (Don’t worry about your the English skills. This is friendly open-source project. Many of users / contributors are not native speakers.) |
f53d35b to
5294823
Compare
d91b8a7 to
87db828
Compare
SimenB
left a comment
There was a problem hiding this comment.
We're getting close! 🙂
CI is currently failing on the new tests added. One thing is that the snapshots aren't updated (you can do so by running yarn jest nativeEsm.test -u). However, the main problem is that those new tests are failing. The best way to work on them locally is to cd into e2e/native-esm and run yarn followed by node --experimental-vm-modules --no-warnings ../../packages/jest/bin/jest.js.
|
|
||
| jestObject.unstable_unmockModule('../index.js'); | ||
|
|
||
| const importedMockAfterUnmock = await import('../reexport.js'); |
There was a problem hiding this comment.
I don't think this will work as ../reexport.js already has a reference to the old (mocked) index.js.
Do you have an example of a test of ours using unmock that works in this way? If not, I think you can just delete the test
There was a problem hiding this comment.
Yes. It is not work with "mock" and "unmock"
test('test', () => {
jest.mock('./dependencyModule', () => {
return {getSecretNumber: () => 1000};
});
const {getSecretNumber} = require("./dependencyModule");
console.log(getSecretNumber()); // output original implementation. Not mock impl.
jest.unmock('./dependencyModule'); // it is the reset implementation in the file
});
|
Also, I think it'd be easiest to move the mocking tests to its own file so you can use diff --git c/e2e/native-esm/__tests__/native-esm-mocks.test.js w/e2e/native-esm/__tests__/native-esm-mocks.test.js
new file mode 100644
index 0000000000..3670a3525d
--- /dev/null
+++ w/e2e/native-esm/__tests__/native-esm-mocks.test.js
@@ -0,0 +1,82 @@
+/**
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+afterEach(() => {
+ import.meta.jest.resetModules();
+});
+
+test('can mock module', async () => {
+ import.meta.jest.unstable_mockModule(
+ '../mockedModule.mjs',
+ () => ({foo: 'bar'}),
+ {virtual: true},
+ );
+
+ const importedMock = await import('../mockedModule.mjs');
+
+ expect(Object.keys(importedMock)).toEqual(['foo']);
+ expect(importedMock.foo).toBe('bar');
+});
+
+test('can mock transitive module', async () => {
+ import.meta.jest.unstable_mockModule('../index.js', () => ({foo: 'bar'}));
+
+ const importedMock = await import('../reexport.js');
+
+ expect(Object.keys(importedMock)).toEqual(['foo']);
+ expect(importedMock.foo).toBe('bar');
+});
+
+test('can unmock module', async () => {
+ import.meta.jest.unstable_mockModule('../index.js', () => ({
+ double: num => num * 1000,
+ }));
+
+ const importedMock = await import('../index.js');
+ expect(importedMock.double(2)).toBe(2000);
+
+ import.meta.jest.unstable_unmockModule('../index.js');
+
+ const importedMockAfterUnmock = await import('../index.js');
+ expect(importedMockAfterUnmock.double(2)).toBe(4);
+});
+
+test('can unmock transitive module', async () => {
+ import.meta.jest.unstable_mockModule('../index.js', () => ({
+ double: num => num * 1000,
+ }));
+
+ const importedMock = await import('../reexport.js');
+ expect(importedMock.double(2)).toBe(2000);
+
+ import.meta.jest.unstable_unmockModule('../index.js');
+
+ const importedMockAfterUnmock = await import('../reexport.js');
+ expect(importedMockAfterUnmock.double(2)).toBe(4);
+});
+
+test("can unmock module; don't override mock", async () => {
+ import.meta.jest.unstable_mockModule('../index.js', () => ({
+ double: num => num * 1000,
+ }));
+
+ const importedMock = await import('../index.js');
+ expect(importedMock.double(2)).toBe(2000);
+
+ import.meta.jest.unstable_unmockModule('../index.js');
+
+ const importedMockAfterUnmock = await import('../index.js');
+ expect(importedMockAfterUnmock.double(2)).toBe(4);
+
+ import.meta.jest.unstable_mockModule('../index.js', () => ({
+ double: () => 'never call',
+ }));
+
+ const importedMockAfterOverride = await import('../index.js');
+ expect(importedMockAfterOverride.double(2)).not.toBe('never call');
+ expect(importedMockAfterOverride.double(2)).toBe(2000);
+});
diff --git c/e2e/native-esm/__tests__/native-esm.test.js w/e2e/native-esm/__tests__/native-esm.test.js
index 2f5070a826..0b2a0a009d 100644
--- c/e2e/native-esm/__tests__/native-esm.test.js
+++ w/e2e/native-esm/__tests__/native-esm.test.js
@@ -224,76 +224,6 @@ test('require of ESM should throw correct error', () => {
);
});
-test('can mock module', async () => {
- jestObject.unstable_mockModule('../mockedModule.mjs', () => ({foo: 'bar'}), {
- virtual: true,
- });
-
- const importedMock = await import('../mockedModule.mjs');
-
- expect(Object.keys(importedMock)).toEqual(['foo']);
- expect(importedMock.foo).toBe('bar');
-});
-
-test('can mock transitive module', async () => {
- jestObject.unstable_mockModule('../index.js', () => ({foo: 'bar'}));
-
- const importedMock = await import('../reexport.js');
-
- expect(Object.keys(importedMock)).toEqual(['foo']);
- expect(importedMock.foo).toBe('bar');
-});
-
-test('can unmock module', async () => {
- jestObject.unstable_mockModule('../index.js', () => ({
- double: num => num * 1000,
- }));
-
- const importedMock = await import('../index.js');
- expect(importedMock.double(2)).toBe(2000);
-
- jestObject.unstable_unmockModule('../index.js');
-
- const importedMockAfterUnmock = await import('../index.js');
- expect(importedMockAfterUnmock.double(2)).toBe(4);
-});
-
-test('can unmock transitive module', async () => {
- jestObject.unstable_mockModule('../index.js', () => ({
- double: num => num * 1000,
- }));
-
- const importedMock = await import('../reexport.js');
- expect(importedMock.double(2)).toBe(2000);
-
- jestObject.unstable_unmockModule('../index.js');
-
- const importedMockAfterUnmock = await import('../reexport.js');
- expect(importedMockAfterUnmock.double(2)).toBe(4);
-});
-
-test('can unmock module; don`t override mock', async () => {
- jestObject.unstable_mockModule('../index.js', () => ({
- double: num => num * 1000,
- }));
-
- const importedMock = await import('../index.js');
- expect(importedMock.double(2)).toBe(2000);
-
- jestObject.unstable_unmockModule('../index.js');
-
- const importedMockAfterUnmock = await import('../index.js');
- expect(importedMockAfterUnmock.double(2)).toBe(4);
-
- jestObject.unstable_mockModule('../index.js', () => ({
- double: () => 'never call',
- }));
-
- const importedMockAfterOverride = await import('../index.js');
- expect(importedMockAfterOverride.double(2)).not.toBe('never call');
- expect(importedMockAfterOverride.double(2)).toBe(2000);
-});
-
test('supports imports using "node:" prefix', () => {
expect(dns).toBe(prefixDns);
}); |
https://github.com/jestjs/jest/blob/main/packages/jest-cli/bin/jest.js#L16 |
|
@SimenB @mrazauskas |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Closes #15079
Summary
Test plan