From 67081da147f44274d02bdc6aa01b21fabb965c62 Mon Sep 17 00:00:00 2001 From: Hannah Luedeke Date: Tue, 16 Oct 2018 09:48:55 -0500 Subject: [PATCH 1/7] Fixes issue #3073. Typechecks prototype before assignment. Fixes interaction with mongoos --- .idea/codeStyles/Project.xml | 42 ++ .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/inspectionProfiles/Project_Default.xml | 6 + .idea/jest.iml | 12 + .idea/misc.xml | 9 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 423 +++++++++++++++++++ packages/jest-mock/src/index.js | 3 +- 9 files changed, 513 insertions(+), 1 deletion(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/jest.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 000000000000..937ab46aa1bf --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 000000000000..79ee123c2b23 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 000000000000..c6cc8c8196a2 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/jest.iml b/.idea/jest.iml new file mode 100644 index 000000000000..24643cc37449 --- /dev/null +++ b/.idea/jest.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000000..23bb55b70f5a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + ApexVCS + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000000..cd6cbe53a60e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000000..94a25f7f4cb4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 000000000000..80e83faa04f0 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,423 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + true + true + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1539380317907 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 25af98b21c020e8052130bb8d4755592413701bb Mon Sep 17 00:00:00 2001 From: Hannah Luedeke Date: Mon, 4 Mar 2019 09:26:02 -0600 Subject: [PATCH 3/7] Updated CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75e8894b3e4a..010860213e95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,7 @@ - `[jest-snapshot]` Write and read snapshots from disk even if `fs` is mocked ([#7080](https://github.com/facebook/jest/pull/7080)) - `[jest-config]` Normalize `config.cwd` and `config.rootDir` using `realpath ([#7598](https://github.com/facebook/jest/pull/7598)) - `[jest-environment-node]` Fix buffer property is not ArrayBuffer issue. ([#7626](https://github.com/facebook/jest/pull/7626)) +- `[jest-mock]` Adds a type check to prototype to allow mocks of mongoose library without error. ([#8040](https://github.com/facebook/jest/pull/8040)) ### Chore & Maintenance From 68d9748c6105ad35fb3b97265e24b314e1760957 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Mar 2019 18:27:43 +0100 Subject: [PATCH 4/7] add test --- packages/jest-mock/src/__tests__/index.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 31cee40614c8..d158fa5ce108 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -880,6 +880,14 @@ describe('moduleMocker', () => { expect(fn1()).toEqual('abcd'); expect(fn2()).toEqual('abcde'); }); + + it('handles a property called `prototype`', () => { + expect(() => + moduleMocker.generateFromMetadata( + moduleMocker.getMetadata({prototype: 1}), + ), + ).not.toThrow(); + }); }); }); From af11db203496387be08736fbe27d4c34d53de4e4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Mar 2019 18:42:19 +0100 Subject: [PATCH 5/7] test that generating a mock actually works, not just that it doesn't explode --- packages/jest-mock/src/__tests__/index.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index d158fa5ce108..5c79d7078da9 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -882,11 +882,11 @@ describe('moduleMocker', () => { }); it('handles a property called `prototype`', () => { - expect(() => - moduleMocker.generateFromMetadata( - moduleMocker.getMetadata({prototype: 1}), - ), - ).not.toThrow(); + const mock = moduleMocker.generateFromMetadata( + moduleMocker.getMetadata({prototype: 1}), + ); + + expect(mock.prototype).toBe(1); }); }); }); From b69ff0a6e15496a2cce6046bcc47e5f977dda42e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Mar 2019 18:48:15 +0100 Subject: [PATCH 6/7] make typings in jest-mock slightly better --- packages/jest-mock/src/__tests__/index.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 5c79d7078da9..bd0d57f50746 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -6,18 +6,18 @@ * */ -import vm from 'vm'; +import vm, {Context} from 'vm'; +import {ModuleMocker} from '../'; describe('moduleMocker', () => { - let moduleMocker; - let mockContext; - let mockGlobals; + let moduleMocker: ModuleMocker; + let mockContext: Context; + let mockGlobals: NodeJS.Global; beforeEach(() => { - const mock = require('../'); mockContext = vm.createContext(); mockGlobals = vm.runInNewContext('this', mockContext); - moduleMocker = new mock.ModuleMocker(mockGlobals); + moduleMocker = new ModuleMocker(mockGlobals); }); describe('getMetadata', () => { From fde6dba792ed27355a5c670b98891ebb2361ba87 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Mar 2019 19:05:36 +0100 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 037ddd3e6cdc..0bd042036a3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ - `[jest-jasmine2]`: Throw explicit error when errors happen after test is considered complete ([#8005](https://github.com/facebook/jest/pull/8005)) - `[jest-circus]`: Throw explicit error when errors happen after test is considered complete ([#8005](https://github.com/facebook/jest/pull/8005)) - `[expect]` Remove duck typing and obsolete browser support code when comparing DOM nodes and use DOM-Level-3 API instead ([#7995](https://github.com/facebook/jest/pull/7995)) -- `[jest-mock]` Adds a type check to prototype to allow mocks of mongoose library without error. ([#8040](https://github.com/facebook/jest/pull/8040)) +- `[jest-mock]` Adds a type check to `prototype` to allow mocks of objects with a primitive `prototype` property. ([#8040](https://github.com/facebook/jest/pull/8040)) ### Chore & Maintenance