Skip to content

Commit

Permalink
Fix issue mocking bound method (#3805)
Browse files Browse the repository at this point in the history
  • Loading branch information
xadn authored and cpojer committed Jun 13, 2017
1 parent ec78c96 commit c8c74f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/jest-mock/src/__tests__/jest-mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ describe('moduleMocker', () => {
expect(typeof multipleBoundFuncMock).toBe('function');
});

it('mocks methods that are bound after mocking', () => {
const fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(() => {}),
);

const barMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(fooMock.bind(null)),
);

expect(barMock).not.toThrow();
});

it('mocks regexp instances', () => {
expect(() =>
moduleMocker.generateFromMetadata(moduleMocker.getMetadata(/a/)),
Expand Down
10 changes: 7 additions & 3 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,7 @@ class ModuleMockerClass {
mockConstructor: () => any,
): any {
let name = metadata.name;
// Special case functions named `mockConstructor` to guard for infinite
// loops.
if (!name || name === MOCK_CONSTRUCTOR_NAME) {
if (!name) {
return mockConstructor;
}

Expand All @@ -395,6 +393,12 @@ class ModuleMockerClass {
} while (name && name.startsWith(boundFunctionPrefix));
}

// Special case functions named `mockConstructor` to guard for infinite
// loops.
if (name === MOCK_CONSTRUCTOR_NAME) {
return mockConstructor;
}

// It's a syntax error to define functions with a reserved keyword
// as name.
if (RESERVED_KEYWORDS[name]) {
Expand Down

0 comments on commit c8c74f4

Please sign in to comment.