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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-runtime breaks node.js 'module' module API #9710

Closed
hugebdu opened this issue Mar 26, 2020 · 11 comments 路 Fixed by #9711
Closed

jest-runtime breaks node.js 'module' module API #9710

hugebdu opened this issue Mar 26, 2020 · 11 comments 路 Fixed by #9711

Comments

@hugebdu
Copy link

hugebdu commented Mar 26, 2020

馃悰 Bug Report

Node.js's module module API is not compliant after gets instrumented by JEST

To Reproduce

const jq = require('jsonpath');

describe('JEST test', () => {

  test('hello world', async () => {

    expect(1).toBe(1);
  });
});

This test will fail with TypeError: Module is not a constructor

Expected behavior

Test should pass

Link to repl or repo (highly encouraged)

https://github.com/hugebdu/jest-bug

envinfo

  System:
    OS: macOS 10.15.3
    CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
  Binaries:
    Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
    Yarn: 1.21.1 - ~/.nvm/versions/node/v12.13.1/bin/yarn
    npm: 6.12.1 - ~/.nvm/versions/node/v12.13.1/bin/npm
  npmPackages:
    jest: ~25.2.1 => 25.2.1 

@SimenB
Copy link
Member

SimenB commented Mar 26, 2020

Oops, comes from #9469, /cc @doniyor2109.

I didn't know require('module'); returned a constructible class - doesn't seem to be documented behavior. We need to fix this of course

@SimenB
Copy link
Member

SimenB commented Mar 26, 2020

@hugebdu if you have patch-package you can change the import in jsonpath/lib/aesprim.js from var Module = require('module'); to var { Module } = require('module'); to unblock yourself

@hugebdu
Copy link
Author

hugebdu commented Mar 26, 2020

@SimenB thanks, meanwhile I simply switched to jsonpath-plus - does the same and seems to be more active.

@SimenB
Copy link
Member

SimenB commented Mar 26, 2020

Opened up a quick PR #9711.

@doniyor2109
Copy link
Contributor

Here it was 馃檲

https://github.com/nodejs/node/blob/49ad16104fb467e5e76049950830b15f4793bc7c/lib/internal/modules/cjs/loader.js#L156

Next time when mocking node modules, I will definitely look at the source code of nodejs.

@NickHeiner
Copy link

Thanks for the blazing-fast turnaround time on this. 馃槃

@vlaurin
Copy link

vlaurin commented Mar 26, 2020

This seems to also affect all modules built with ESM:

TypeError: f is not a constructor

      at Object.<anonymous> (node_modules/esm/esm.js:1:529)

Which points to https://github.com/standard-things/esm/blob/master/esm.js#L48

@SimenB
Copy link
Member

SimenB commented Mar 26, 2020

25.2.2 released with the fix. If it's still broken somehow we'll need to revert #9469 and revisit it later

@vlaurin
Copy link

vlaurin commented Mar 26, 2020

The fix is working fine for tests importing ESM-based dependencies. Thank you!

@NickHeiner
Copy link

This fixes my observed issue as well. Thanks again for hitting this so fast.

@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 11, 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.

5 participants