Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes error when mocking mongoose library root object #8040

Merged
merged 11 commits into from
Mar 4, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 14 additions & 6 deletions packages/jest-mock/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,8 @@ class ModuleMockerClass {
if (
metadata.type !== 'undefined' &&
metadata.type !== 'null' &&
mock.prototype
mock.prototype &&
typeof mock.prototype === 'object'
) {
mock.prototype.constructor = mock;
}
Expand Down