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
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 mongoose library without error. ([#8040](https://github.com/facebook/jest/pull/8040))

### Chore & Maintenance

Expand Down
8 changes: 8 additions & 0 deletions packages/jest-mock/src/__tests__/index.test.ts
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
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