Skip to content

Commit

Permalink
Removes error when mocking mongoose library root object (#8040)
Browse files Browse the repository at this point in the history
  • Loading branch information
hluedeke authored and SimenB committed Mar 4, 2019
1 parent 5f7c3e4 commit c15b7a5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
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 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
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
Expand Up @@ -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;
}
Expand Down

0 comments on commit c15b7a5

Please sign in to comment.