Skip to content

Commit c15b7a5

Browse files
hluedekeSimenB
authored andcommittedMar 4, 2019
Removes error when mocking mongoose library root object (#8040)
1 parent 5f7c3e4 commit c15b7a5

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- `[jest-jasmine2]`: Throw explicit error when errors happen after test is considered complete ([#8005](https://github.com/facebook/jest/pull/8005))
3333
- `[jest-circus]`: Throw explicit error when errors happen after test is considered complete ([#8005](https://github.com/facebook/jest/pull/8005))
3434
- `[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))
35+
- `[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))
3536

3637
### Chore & Maintenance
3738

‎packages/jest-mock/src/__tests__/index.test.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
*
77
*/
88

9-
import vm from 'vm';
9+
import vm, {Context} from 'vm';
10+
import {ModuleMocker} from '../';
1011

1112
describe('moduleMocker', () => {
12-
let moduleMocker;
13-
let mockContext;
14-
let mockGlobals;
13+
let moduleMocker: ModuleMocker;
14+
let mockContext: Context;
15+
let mockGlobals: NodeJS.Global;
1516

1617
beforeEach(() => {
17-
const mock = require('../');
1818
mockContext = vm.createContext();
1919
mockGlobals = vm.runInNewContext('this', mockContext);
20-
moduleMocker = new mock.ModuleMocker(mockGlobals);
20+
moduleMocker = new ModuleMocker(mockGlobals);
2121
});
2222

2323
describe('getMetadata', () => {
@@ -880,6 +880,14 @@ describe('moduleMocker', () => {
880880
expect(fn1()).toEqual('abcd');
881881
expect(fn2()).toEqual('abcde');
882882
});
883+
884+
it('handles a property called `prototype`', () => {
885+
const mock = moduleMocker.generateFromMetadata(
886+
moduleMocker.getMetadata({prototype: 1}),
887+
);
888+
889+
expect(mock.prototype).toBe(1);
890+
});
883891
});
884892
});
885893

‎packages/jest-mock/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@ class ModuleMockerClass {
850850
if (
851851
metadata.type !== 'undefined' &&
852852
metadata.type !== 'null' &&
853-
mock.prototype
853+
mock.prototype &&
854+
typeof mock.prototype === 'object'
854855
) {
855856
mock.prototype.constructor = mock;
856857
}

0 commit comments

Comments
 (0)
Please sign in to comment.