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 function name sanitization in jest-mock #4464

Merged
merged 1 commit into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 15 additions & 7 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,22 @@ describe('moduleMocker', () => {
});

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

const mock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo['foo-bar']),
);
expect(!mock.name || mock.name === 'foo$bar').toBeTruthy();
return moduleMocker.generateFromMetadata(moduleMocker.getMetadata(fn));
}

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

it('special cases the mockConstructor name', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type MockFunctionConfig = {

const MOCK_CONSTRUCTOR_NAME = 'mockConstructor';

const FUNCTION_NAME_RESERVED_PATTERN = /[\s!-\/:-@\[-`{-~]/g;
Copy link
Collaborator

@thymikee thymikee Sep 12, 2017

Choose a reason for hiding this comment

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

Does this need to contain a global flag? This makes this regex stateful, which may result in unexpected behaviors like:

screen shot 2017-09-12 at 08 46 22

Copy link
Member

Choose a reason for hiding this comment

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

Ohh, yeah, let's fix that. Good find.

Copy link
Member

Choose a reason for hiding this comment

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

The call to .test doesn't need the flag, but the call to .replace does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll put up another PR to fix this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see it's already fixed in master. Thanks for spotting this @thymikee !


// $FlowFixMe
const RESERVED_KEYWORDS = Object.assign(Object.create(null), {
arguments: true,
Expand Down Expand Up @@ -474,8 +476,8 @@ class ModuleMockerClass {

// It's also a syntax error to define a function with a reserved character
// as part of it's name.
if (/[\s-]/.test(name)) {
name = name.replace(/[\s-]/g, '$');
if (FUNCTION_NAME_RESERVED_PATTERN.test(name)) {
name = name.replace(FUNCTION_NAME_RESERVED_PATTERN, '$');
}

const body =
Expand Down