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

Conversation

jsdf
Copy link
Contributor

@jsdf jsdf commented Sep 11, 2017

Summary
When creating mock functions, jest-mock attempts to copy over the existing name of the function being mocked. However, it creates the mock functions using Function() (a form of eval), using string interpolation to specify the function name in the code being evaluated. This means that only function names consisting of valid characters for JS identifiers can be used.

Currently there is some code which attempts to sanitize function names so they are valid, but it only replaces spaces and hypens. Other invalid characters remain.

I've expanded the regex used to include other special characters in the ASCII space, while leaving ASCII chars which are valid for identifiers, as well as unicode characters (for people coding in non-Latin alphabets, for example).

Test plan

  • yarn test

I've expanded the relevant test case in packages/jest-mock/src/__tests__/jest_mock.test.js to include more cases. I also changed the test to use defineProperty to ensure the input function has a name, so the existing special case where function.name is null can be removed.

@cpojer cpojer merged commit 19b5cce into jestjs:master Sep 12, 2017
@@ -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 !

tabrindle pushed a commit to tabrindle/jest that referenced this pull request Oct 2, 2017
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants