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

jest.mock module auto-mock error when Function names are numeric #7624

Closed
gregsochanik opened this issue Jan 14, 2019 · 4 comments · Fixed by #7653
Closed

jest.mock module auto-mock error when Function names are numeric #7624

gregsochanik opened this issue Jan 14, 2019 · 4 comments · Fixed by #7653

Comments

@gregsochanik
Copy link

gregsochanik commented Jan 14, 2019

🐛 Bug Report

When using the jest.mock('module'); feature to deep auto-mock an entire module, an error is thrown if the module contains any objects that contain functions with a numeric name.

To Reproduce

Example library kafkajs

require(`kafkajs`)
jest.mock('kafkajs');

Expected behavior

The library should be mocked with no issues.

Actual behaviour

Application exits with the following error thrown:

 FAIL  tests/unit/mock.test.js
  ● Test suite failed to run

    SyntaxError: Unexpected number
        at new Function (<anonymous>)

Which is caused by the following line in jest-mock/index.js (https://github.com/facebook/jest/blob/master/packages/jest-mock/src/index.js#L647)

    const createConstructor = new this._environmentGlobal.Function(
      MOCK_CONSTRUCTOR_NAME,
      body
    );

the error is a standard nodejs error which can be emulated in the repl via

> new Function(1, {})
SyntaxError: Unexpected number

Due to new Function() throwing Unexpected number if a numeric value is passed to it.

Offending object with numeric function names in kafkajs library can be found here:
https://github.com/tulios/kafkajs/blob/master/src/protocol/message/compression/index.js#L14

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: Linux 4.18 Ubuntu 18.10 (Cosmic Cuttlefish)
    CPU: (4) x64 Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz
  Binaries:
    Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
    Yarn: 1.12.3 - ~/.yarn/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0
@jeysal
Copy link
Contributor

jeysal commented Jan 14, 2019

I can confirm this bug.

@SimenB
Copy link
Member

SimenB commented Jan 14, 2019

Not sure how we should handle this. @thymikee ideas?

@jeysal
Copy link
Contributor

jeysal commented Jan 19, 2019

Best handling is probably prefix a $ or something, like we do with other invalid function names. I'll work on a fix for this.
Note that in our case, rather than new Function(1, {}), it's something like

new Function('mockConstructor', 'return function 1() {return mockConstructor.apply(this,arguments);}')

@github-actions
Copy link

This issue 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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants