Skip to content

Commit

Permalink
Fix automock for numeric function names (jestjs#7653)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal authored and captain-yossarian committed Jul 18, 2019
1 parent 14b41f3 commit 7c26305
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

### Fixes

- `[jest-mock]` Fix automock for numeric function names ([#7653](https://github.com/facebook/jest/pull/7653))
- `[jest-config]` Ensure `existsSync` is only called with a string parameter ([#7607](https://github.com/facebook/jest/pull/7607))
- `[expect]` `toStrictEqual` considers sparseness of arrays. ([#7591](https://github.com/facebook/jest/pull/7591))
- `[jest-cli]` Fix empty coverage data for untested files ([#7388](https://github.com/facebook/jest/pull/7388))
Expand Down
35 changes: 19 additions & 16 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,20 @@ describe('moduleMocker', () => {
expect(mock.name).toBe('foo');
});

it('escapes illegal characters in function name property', () => {
it('fixes illegal function name properties', () => {
function getMockFnWithOriginalName(name) {
const fn = () => {};
Object.defineProperty(fn, 'name', {value: name});

return moduleMocker.generateFromMetadata(moduleMocker.getMetadata(fn));
}

expect(
getMockFnWithOriginalName('foo-bar').name === 'foo$bar',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo-bar-2').name === 'foo$bar$2',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo-bar-3').name === 'foo$bar$3',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo/bar').name === 'foo$bar',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo𠮷bar').name === 'foo𠮷bar',
).toBeTruthy();
expect(getMockFnWithOriginalName('1').name).toBe('$1');
expect(getMockFnWithOriginalName('foo-bar').name).toBe('foo$bar');
expect(getMockFnWithOriginalName('foo-bar-2').name).toBe('foo$bar$2');
expect(getMockFnWithOriginalName('foo-bar-3').name).toBe('foo$bar$3');
expect(getMockFnWithOriginalName('foo/bar').name).toBe('foo$bar');
expect(getMockFnWithOriginalName('foo𠮷bar').name).toBe('foo𠮷bar');
});

it('special cases the mockConstructor name', () => {
Expand Down Expand Up @@ -348,6 +339,18 @@ describe('moduleMocker', () => {
).not.toThrow();
});

it('mocks functions with numeric names', () => {
const obj = {
1: () => {},
};

const objMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(obj),
);

expect(typeof objMock[1]).toBe('function');
});

describe('mocked functions', () => {
it('tracks calls to mocks', () => {
const fn = moduleMocker.fn();
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 @@ -623,9 +623,13 @@ class ModuleMockerClass {
return mockConstructor;
}

// It's a syntax error to define functions with a reserved keyword
// as name.
if (RESERVED_KEYWORDS[name]) {
if (
// It's a syntax error to define functions with a reserved keyword
// as name.
RESERVED_KEYWORDS[name] ||
// It's also a syntax error to define functions with a name that starts with a number
/^\d/.test(name)
) {
name = '$' + name;
}

Expand Down

0 comments on commit 7c26305

Please sign in to comment.