Node.js's module module API is not compliant after gets instrumented by JEST
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
Test should pass
https://github.com/hugebdu/jest-bug
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
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
@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
@SimenB thanks, meanwhile I simply switched to jsonpath-plus - does the same and seems to be more active.
Opened up a quick PR #9711.
Here it was 馃檲
Next time when mocking node modules, I will definitely look at the source code of nodejs.
Thanks for the blazing-fast turnaround time on this. :smile:
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
25.2.2 released with the fix. If it's still broken somehow we'll need to revert #9469 and revisit it later
The fix is working fine for tests importing ESM-based dependencies. Thank you!
This fixes my observed issue as well. Thanks again for hitting this so fast.
Most helpful comment
25.2.2 released with the fix. If it's still broken somehow we'll need to revert #9469 and revisit it later