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

Fix automock for numeric function names #7653

Merged
merged 3 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (TODO)
- `[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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

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
/^[0-9]/.test(name)
jeysal marked this conversation as resolved.
Show resolved Hide resolved
) {
name = '$' + name;
}

Expand Down