From 9d5e8f3d7bbfd4b1010e0ea9cc6e24106531f3a5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 3 Sep 2022 12:02:41 +0200 Subject: [PATCH 1/3] fix: support `jest.resetModules` correctly in ESM --- .../__snapshots__/nativeEsm.test.ts.snap | 2 +- e2e/native-esm/__tests__/native-esm.test.js | 19 +++ e2e/native-esm/package.json | 3 +- e2e/native-esm/yarn.lock | 157 +++++++++++++++++- packages/jest-runtime/src/index.ts | 2 +- 5 files changed, 171 insertions(+), 12 deletions(-) diff --git a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap index 1e6cd80fa82e..4324b26d16c4 100644 --- a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap +++ b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap @@ -10,7 +10,7 @@ Ran all test suites matching /native-esm-deep-cjs-reexport.test.js/i." exports[`runs test with native ESM 1`] = ` "Test Suites: 1 passed, 1 total -Tests: 33 passed, 33 total +Tests: 34 passed, 34 total Snapshots: 0 total Time: <> Ran all test suites matching /native-esm.test.js/i." diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index aeac49331cb7..4acf62ad655f 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -46,6 +46,7 @@ test('should support importing node core modules', () => { expect(JSON.parse(readFileSync(packageJsonPath, 'utf8'))).toEqual({ devDependencies: { 'discord.js': '14.3.0', + yargs: '^17.5.1', }, jest: { testEnvironment: 'node', @@ -302,3 +303,21 @@ test('can mock "data:" URI module', async () => { const mocked = await import(dataModule); expect(mocked.foo).toBe('bar'); }); + +test('can import with module reset', async () => { + const {default: yargs} = await import('yargs'); + + let args = yargs().parse([]); + + expect(args._).toEqual([]); + + import.meta.jest.resetModules(); + + const {default: yargsAgain} = await import('yargs'); + + expect(yargs).not.toBe(yargsAgain); + + args = yargsAgain().parse([]); + + expect(args._).toEqual([]); +}); diff --git a/e2e/native-esm/package.json b/e2e/native-esm/package.json index 7e5307b71f1d..60993c0776ca 100644 --- a/e2e/native-esm/package.json +++ b/e2e/native-esm/package.json @@ -1,7 +1,8 @@ { "type": "module", "devDependencies": { - "discord.js": "14.3.0" + "discord.js": "14.3.0", + "yargs": "^17.5.1" }, "jest": { "testEnvironment": "node", diff --git a/e2e/native-esm/yarn.lock b/e2e/native-esm/yarn.lock index 737d96c077d9..227ec7c5703f 100644 --- a/e2e/native-esm/yarn.lock +++ b/e2e/native-esm/yarn.lock @@ -48,12 +48,12 @@ __metadata: linkType: hard "@sapphire/shapeshift@npm:^3.5.1": - version: 3.5.1 - resolution: "@sapphire/shapeshift@npm:3.5.1" + version: 3.6.0 + resolution: "@sapphire/shapeshift@npm:3.6.0" dependencies: fast-deep-equal: ^3.1.3 lodash.uniqwith: ^4.5.0 - checksum: caecfef844c9e43e921a5051da888fae7da8980bfd9f9bb4f7fee85931d40929ffb9b6dfae464c0dccee61e56f7698f998e4d9a54d25f35fad39a51ba1a4f391 + checksum: 31b426424d064c516144c6eda07dfa0e44d7cbb8309dde919b923aa6ae939faac6384fa4d08db391a8da11efa3a83c18c9be1ebd053ba29403e61f5e2450b788 languageName: node linkType: hard @@ -72,9 +72,9 @@ __metadata: linkType: hard "@types/node@npm:*": - version: 18.7.13 - resolution: "@types/node@npm:18.7.13" - checksum: 45431e7e89ecaf85c7d2c180d801c132a7c59e2f8ad578726b6d71cc74e3267c18f9ccdcad738bc0479790c078f0c79efb0e58da2c6be535c15995dbb19050c9 + version: 18.7.14 + resolution: "@types/node@npm:18.7.14" + checksum: 99cf28ff854100158de875cca23c7acc3cc01dfee526a52b90b7f36767c821bcbaf2be0a98a70f06f3b78f3c60639168ff949d725b61e2e124f9f71f1fb8043d languageName: node linkType: hard @@ -87,10 +87,53 @@ __metadata: languageName: node linkType: hard +"ansi-regex@npm:^5.0.1": + version: 5.0.1 + resolution: "ansi-regex@npm:5.0.1" + checksum: 2aa4bb54caf2d622f1afdad09441695af2a83aa3fe8b8afa581d205e57ed4261c183c4d3877cee25794443fde5876417d859c108078ab788d6af7e4fe52eb66b + languageName: node + linkType: hard + +"ansi-styles@npm:^4.0.0": + version: 4.3.0 + resolution: "ansi-styles@npm:4.3.0" + dependencies: + color-convert: ^2.0.1 + checksum: 513b44c3b2105dd14cc42a19271e80f386466c4be574bccf60b627432f9198571ebf4ab1e4c3ba17347658f4ee1711c163d574248c0c1cdc2d5917a0ad582ec4 + languageName: node + linkType: hard + +"cliui@npm:^7.0.2": + version: 7.0.4 + resolution: "cliui@npm:7.0.4" + dependencies: + string-width: ^4.2.0 + strip-ansi: ^6.0.0 + wrap-ansi: ^7.0.0 + checksum: ce2e8f578a4813806788ac399b9e866297740eecd4ad1823c27fd344d78b22c5f8597d548adbcc46f0573e43e21e751f39446c5a5e804a12aace402b7a315d7f + languageName: node + linkType: hard + +"color-convert@npm:^2.0.1": + version: 2.0.1 + resolution: "color-convert@npm:2.0.1" + dependencies: + color-name: ~1.1.4 + checksum: 79e6bdb9fd479a205c71d89574fccfb22bd9053bd98c6c4d870d65c132e5e904e6034978e55b43d69fcaa7433af2016ee203ce76eeba9cfa554b373e7f7db336 + languageName: node + linkType: hard + +"color-name@npm:~1.1.4": + version: 1.1.4 + resolution: "color-name@npm:1.1.4" + checksum: b0445859521eb4021cd0fb0cc1a75cecf67fceecae89b63f62b201cca8d345baf8b952c966862a9d9a2632987d4f6581f0ec8d957dfacece86f0a7919316f610 + languageName: node + linkType: hard + "discord-api-types@npm:^0.37.3": - version: 0.37.4 - resolution: "discord-api-types@npm:0.37.4" - checksum: 18b74acfcd5f6c554668eb5cb31a5db9076dfb5cb6b5c538a342ab9d22816644bada22a66285468fe7dd9980287921d5d50ba3b11cc8e391fe4f09e761bcd179 + version: 0.37.5 + resolution: "discord-api-types@npm:0.37.5" + checksum: 10a23b813d9a30d836aa27169f768883eeed971ab67e58325ff272bfe6b63b169d727ae983d7c1b69c7e0b02ed9072e66e79319ce48174b36cd4a2a5fafe2e9f languageName: node linkType: hard @@ -113,6 +156,20 @@ __metadata: languageName: node linkType: hard +"emoji-regex@npm:^8.0.0": + version: 8.0.0 + resolution: "emoji-regex@npm:8.0.0" + checksum: d4c5c39d5a9868b5fa152f00cada8a936868fd3367f33f71be515ecee4c803132d11b31a6222b2571b1e5f7e13890156a94880345594d0ce7e3c9895f560f192 + languageName: node + linkType: hard + +"escalade@npm:^3.1.1": + version: 3.1.1 + resolution: "escalade@npm:3.1.1" + checksum: a3e2a99f07acb74b3ad4989c48ca0c3140f69f923e56d0cba0526240ee470b91010f9d39001f2a4a313841d237ede70a729e92125191ba5d21e74b106800b133 + languageName: node + linkType: hard + "fast-deep-equal@npm:^3.1.3": version: 3.1.3 resolution: "fast-deep-equal@npm:3.1.3" @@ -131,6 +188,13 @@ __metadata: languageName: node linkType: hard +"get-caller-file@npm:^2.0.5": + version: 2.0.5 + resolution: "get-caller-file@npm:2.0.5" + checksum: b9769a836d2a98c3ee734a88ba712e62703f1df31b94b784762c433c27a386dd6029ff55c2a920c392e33657d80191edbf18c61487e198844844516f843496b9 + languageName: node + linkType: hard + "ieee754@npm:^1.2.1": version: 1.2.1 resolution: "ieee754@npm:1.2.1" @@ -145,6 +209,13 @@ __metadata: languageName: node linkType: hard +"is-fullwidth-code-point@npm:^3.0.0": + version: 3.0.0 + resolution: "is-fullwidth-code-point@npm:3.0.0" + checksum: 44a30c29457c7fb8f00297bce733f0a64cd22eca270f83e58c105e0d015e45c019491a4ab2faef91ab51d4738c670daff901c799f6a700e27f7314029e99e348 + languageName: node + linkType: hard + "lodash.snakecase@npm:^4.1.1": version: 4.1.1 resolution: "lodash.snakecase@npm:4.1.1" @@ -186,11 +257,19 @@ __metadata: languageName: node linkType: hard +"require-directory@npm:^2.1.1": + version: 2.1.1 + resolution: "require-directory@npm:2.1.1" + checksum: fb47e70bf0001fdeabdc0429d431863e9475e7e43ea5f94ad86503d918423c1543361cc5166d713eaa7029dd7a3d34775af04764bebff99ef413111a5af18c80 + languageName: node + linkType: hard + "root-workspace-0b6124@workspace:.": version: 0.0.0-use.local resolution: "root-workspace-0b6124@workspace:." dependencies: discord.js: 14.3.0 + yargs: ^17.5.1 languageName: unknown linkType: soft @@ -201,6 +280,17 @@ __metadata: languageName: node linkType: hard +"string-width@npm:^4.1.0, string-width@npm:^4.2.0, string-width@npm:^4.2.3": + version: 4.2.3 + resolution: "string-width@npm:4.2.3" + dependencies: + emoji-regex: ^8.0.0 + is-fullwidth-code-point: ^3.0.0 + strip-ansi: ^6.0.1 + checksum: e52c10dc3fbfcd6c3a15f159f54a90024241d0f149cf8aed2982a2d801d2e64df0bf1dc351cf8e95c3319323f9f220c16e740b06faecd53e2462df1d2b5443fb + languageName: node + linkType: hard + "string_decoder@npm:^1.1.1": version: 1.3.0 resolution: "string_decoder@npm:1.3.0" @@ -210,6 +300,15 @@ __metadata: languageName: node linkType: hard +"strip-ansi@npm:^6.0.0, strip-ansi@npm:^6.0.1": + version: 6.0.1 + resolution: "strip-ansi@npm:6.0.1" + dependencies: + ansi-regex: ^5.0.1 + checksum: f3cd25890aef3ba6e1a74e20896c21a46f482e93df4a06567cebf2b57edabb15133f1f94e57434e0a958d61186087b1008e89c94875d019910a213181a14fc8c + languageName: node + linkType: hard + "strtok3@npm:^7.0.0-alpha.9": version: 7.0.0 resolution: "strtok3@npm:7.0.0" @@ -258,6 +357,17 @@ __metadata: languageName: node linkType: hard +"wrap-ansi@npm:^7.0.0": + version: 7.0.0 + resolution: "wrap-ansi@npm:7.0.0" + dependencies: + ansi-styles: ^4.0.0 + string-width: ^4.1.0 + strip-ansi: ^6.0.0 + checksum: a790b846fd4505de962ba728a21aaeda189b8ee1c7568ca5e817d85930e06ef8d1689d49dbf0e881e8ef84436af3a88bc49115c2e2788d841ff1b8b5b51a608b + languageName: node + linkType: hard + "ws@npm:^8.8.1": version: 8.8.1 resolution: "ws@npm:8.8.1" @@ -272,3 +382,32 @@ __metadata: checksum: 2152cf862cae0693f3775bc688a6afb2e989d19d626d215e70f5fcd8eb55b1c3b0d3a6a4052905ec320e2d7734e20aeedbf9744496d62f15a26ad79cf4cf7dae languageName: node linkType: hard + +"y18n@npm:^5.0.5": + version: 5.0.8 + resolution: "y18n@npm:5.0.8" + checksum: 54f0fb95621ee60898a38c572c515659e51cc9d9f787fb109cef6fde4befbe1c4602dc999d30110feee37456ad0f1660fa2edcfde6a9a740f86a290999550d30 + languageName: node + linkType: hard + +"yargs-parser@npm:^21.0.0": + version: 21.1.1 + resolution: "yargs-parser@npm:21.1.1" + checksum: ed2d96a616a9e3e1cc7d204c62ecc61f7aaab633dcbfab2c6df50f7f87b393993fe6640d017759fe112d0cb1e0119f2b4150a87305cc873fd90831c6a58ccf1c + languageName: node + linkType: hard + +"yargs@npm:^17.5.1": + version: 17.5.1 + resolution: "yargs@npm:17.5.1" + dependencies: + cliui: ^7.0.2 + escalade: ^3.1.1 + get-caller-file: ^2.0.5 + require-directory: ^2.1.1 + string-width: ^4.2.3 + y18n: ^5.0.5 + yargs-parser: ^21.0.0 + checksum: 00d58a2c052937fa044834313f07910fd0a115dec5ee35919e857eeee3736b21a4eafa8264535800ba8bac312991ce785ecb8a51f4d2cc8c4676d865af1cfbde + languageName: node + linkType: hard diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 4541cd44d6e0..639ccc9371c3 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1109,6 +1109,7 @@ export default class Runtime { this._mockRegistry.clear(); this._moduleRegistry.clear(); this._esmoduleRegistry.clear(); + this._fileTransformsMutex.clear(); this._cjsNamedExports.clear(); this._moduleMockRegistry.clear(); this._cacheFS.clear(); @@ -1278,7 +1279,6 @@ export default class Runtime { this._sourceMapRegistry.clear(); this._fileTransforms.clear(); - this._fileTransformsMutex.clear(); this.jestObjectCaches.clear(); this._v8CoverageSources?.clear(); From 259a67a67db81c05945b53eacfb25768ab0b978f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 3 Sep 2022 12:07:41 +0200 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + e2e/native-esm/__tests__/native-esm.test.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80867e863a56..d54ee20c7d7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes - `[babel-plugin-jest-hoist]` Support imported `jest` in mock factory ([#13188](https://github.com/facebook/jest/pull/13188)) +- `[jest-runtime]` Support `jest.resetModules()` with ESM ([#13211](https://github.com/facebook/jest/pull/13211)) ### Chore & Maintenance diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 4acf62ad655f..36e661813743 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -311,7 +311,7 @@ test('can import with module reset', async () => { expect(args._).toEqual([]); - import.meta.jest.resetModules(); + jestObject.resetModules(); const {default: yargsAgain} = await import('yargs'); From 4c57b5ea9a5036b7c504c49e104d004afb32a127 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 3 Sep 2022 12:10:09 +0200 Subject: [PATCH 3/3] show reset modules work in the test --- e2e/native-esm/__tests__/native-esm.test.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 36e661813743..0e0f13554801 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -306,6 +306,9 @@ test('can mock "data:" URI module', async () => { test('can import with module reset', async () => { const {default: yargs} = await import('yargs'); + const {default: yargsAgain} = await import('yargs'); + + expect(yargs).toBe(yargsAgain); let args = yargs().parse([]); @@ -313,11 +316,11 @@ test('can import with module reset', async () => { jestObject.resetModules(); - const {default: yargsAgain} = await import('yargs'); + const {default: yargsYetAgain} = await import('yargs'); - expect(yargs).not.toBe(yargsAgain); + expect(yargs).not.toBe(yargsYetAgain); - args = yargsAgain().parse([]); + args = yargsYetAgain().parse([]); expect(args._).toEqual([]); });