From 601ec1e939b2d28276bd1d6ec2961ec4344711b1 Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Tue, 25 Oct 2022 18:42:07 +0200 Subject: [PATCH 1/4] Consider cjs modules as object --- packages/jest-mock/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index b74d14f53f16..4b6a1c82b5cb 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -426,7 +426,7 @@ function getType(ref?: unknown): MockMetadataType | null { return 'function'; } else if (Array.isArray(ref)) { return 'array'; - } else if (typeName === 'Object') { + } else if (typeName === 'Object' || typeName === 'Module') { return 'object'; } else if ( typeName === 'Number' || From 98aac3bacf3df2635abf7ba06bb8aa0a36f3b3d5 Mon Sep 17 00:00:00 2001 From: Tofandel Date: Wed, 26 Oct 2022 15:37:29 +0200 Subject: [PATCH 2/4] Add unit test and changelog entry --- CHANGELOG.md | 2 ++ .../jest-mock/src/__tests__/index.test.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a32bb7c980d..494e36427afb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-mock]` Treat cjs modules as objects so they can be mocked ([#13513](https://github.com/facebook/jest/pull/13513)) + ### Chore & Maintenance - `[@jest/transform]` Update `convert-source-map` ([#13509](https://github.com/facebook/jest/pull/13509)) diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 4d134554108f..03ff184ef200 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -213,6 +213,25 @@ describe('moduleMocker', () => { expect(mock.enumGetter).toBeDefined(); }); + it('mocks getters of cjs modules', () => { + const foo = Object.defineProperties( + { + enumGetter: { + enumerable: true, + get: () => 10, + }, + }, + { + __esModule: {value: true}, + [Symbol.toStringTag]: {value: 'Module'}, + }, + ); + const mock = moduleMocker.generateFromMetadata( + moduleMocker.getMetadata(foo), + ); + expect(mock.enumGetter).toBeDefined(); + }); + it('mocks ES2015 non-enumerable methods', () => { class ClassFoo { foo() {} From d77170ba60b98fa9f7b79c5fbcf66fe05bb0b01f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 26 Oct 2022 19:03:15 +0200 Subject: [PATCH 3/4] Update index.test.ts --- packages/jest-mock/src/__tests__/index.test.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 03ff184ef200..7457d7042cb9 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -215,12 +215,7 @@ describe('moduleMocker', () => { it('mocks getters of cjs modules', () => { const foo = Object.defineProperties( - { - enumGetter: { - enumerable: true, - get: () => 10, - }, - }, + {foo: 'bar'}, { __esModule: {value: true}, [Symbol.toStringTag]: {value: 'Module'}, @@ -229,7 +224,7 @@ describe('moduleMocker', () => { const mock = moduleMocker.generateFromMetadata( moduleMocker.getMetadata(foo), ); - expect(mock.enumGetter).toBeDefined(); + expect(mock.foo).toBeDefined(); }); it('mocks ES2015 non-enumerable methods', () => { From f09cf2acbbdf629ec430d4aceba108595cd8f335 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 26 Oct 2022 19:05:09 +0200 Subject: [PATCH 4/4] Update index.test.ts --- packages/jest-mock/src/__tests__/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 7457d7042cb9..39146edd7e30 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -213,7 +213,7 @@ describe('moduleMocker', () => { expect(mock.enumGetter).toBeDefined(); }); - it('mocks getters of cjs modules', () => { + it('handles custom toString of transpiled modules', () => { const foo = Object.defineProperties( {foo: 'bar'}, {