Jest: Jest Overwriting Require?

Created on 27 Jan 2019  路  6Comments  路  Source: facebook/jest

馃悰 Bug Report

Jest appears to override the require module, which is causing some issues with Jest recognizing an object as a constructor.

To Reproduce

Steps to reproduce the behavior: Created a repo to make this simple, repo. If you run it directly through Node, it will work- but if you run it through Jest, it will fail complaining there isn't a constructor. A co-worker ran it through Mocha and that didn't fail out either.

Expected behavior

Expected behavior is for the constructor to create a realtime object.

Link to repl or repo (highly encouraged)

repo - This is a basic one with just a few pieces in place, but it highlights the issue

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

Paste the results here:

(node_client) $ > npx envinfo --preset jest
npx: installed 1 in 3.369s

  System:
    OS: macOS 10.14
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
    npm: 6.4.1 - ~/.nvm/versions/node/v8.9.4/bin/npm

Ran this on Windows 10, PopOS (linux), and OSX. Issues seen on all platforms.

Bug

All 6 comments

Yeah, Jest implements its own require in order to support mocking and JIT.

Also, you shouldn't import jsdom and stick its stuff on globals (it's discouraged and can break at any time: https://github.com/jsdom/jsdom/wiki/Don't-stuff-jsdom-globals-onto-the-Node-global). Jest comes with jest-environment-jsdom which runs your code inside of jsdom, which means this is a more correct test:

describe('Test Error on Constructor Require', () => {
  test('Try and create an instance of Realtime', () => {
    const Realtime = require('../resources/Realtime_node.js');

    // This portion fails in Jest, but works in a raw script or in Mocha
    const test_realtime = new Realtime();
    console.log(test_realtime);
  })
})

It still fails with the same error though.


Would you be able to reduce Realtime_node.js into a smaller piece so it's easier to debug why it behaves wrong? Note that it's recommended to have Jest run on source code, not bundled code (it should still work, of course, but it'll be slower on reruns)

Yeah sorry I actually had the main version running it with Jest's JSDOM instead of importing it, but was just throwing this together as an example. I'll speak with my other devs on how this was bundled and if we can make a light version to help with debugging.

Updated the repo to have a tiny file, just a basic file with a console.log inside of it now (still run through browserify and still has the same error).

On second thought- while we did make the constructor smaller and it does still have the same issue in Jest, we realized that the Node portion now also fails. So we'll be putting a fix up for a middle ground soon. Sorry about that.

It's probably something to do with the require wrapper that browserify adds, although I'm not entirely sure what 馃槄 But yeah, getting a small file that does not implode on node file.js would be awesome 馃檪

For now I'm going to close this- I was out of the country for a while but the other dev I was working with couldn't really narrow down the scope of the problem. Thanks for the help, if we are able to narrow down everything with a small file we'll revisit.

Was this page helpful?
0 / 5 - 0 ratings