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

Created on 26 Mar 2020  路  10Comments  路  Source: facebook/jest

馃悰 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 

Regression Bug Help Wanted

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

All 10 comments

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 馃檲

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.

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.

Was this page helpful?
0 / 5 - 0 ratings