diff --git a/CHANGELOG.md b/CHANGELOG.md index 6798246e4d06..cc627eee63da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +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 objects with a primitive `prototype` property. ([#8040](https://github.com/facebook/jest/pull/8040)) ### Chore & Maintenance diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 31cee40614c8..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', () => { @@ -880,6 +880,14 @@ describe('moduleMocker', () => { expect(fn1()).toEqual('abcd'); expect(fn2()).toEqual('abcde'); }); + + it('handles a property called `prototype`', () => { + const mock = moduleMocker.generateFromMetadata( + moduleMocker.getMetadata({prototype: 1}), + ); + + expect(mock.prototype).toBe(1); + }); }); }); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 83ec7e42e936..630dba527cf0 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -850,7 +850,8 @@ class ModuleMockerClass { if ( metadata.type !== 'undefined' && metadata.type !== 'null' && - mock.prototype + mock.prototype && + typeof mock.prototype === 'object' ) { mock.prototype.constructor = mock; }