jest: ReferenceError: requirejsVars is not defined

Do you want to request a feature or report a bug?

bug

What is the current behavior?


 FAIL  ./my.test.js
  ● Test suite failed to run

    ReferenceError: requirejsVars is not defined

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install and npm test.

jest.config.json

{
        "testEnvironment": "node"
}

my.test.js

var requirejs = require('requirejs')
test(() => {})

./node_modules/.bin/jest -c jest.config.json my.test.js

What is the expected behavior?

PASS

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system.

 ./node_modules/.bin/jest --debug -c jest.config.js my.test.js 
jest version = 16.0.1
test framework = jasmine2
config = {
  "testEnvironment": "/Users/jamuferguson/dev/paypal/p2pnodeweb/node_modules/jest-environment-node/build/index.js",
  "rootDir": "/Users/jamuferguson/dev/paypal/p2pnodeweb",
  "name": "-Users-jamuferguson-dev-paypal-p2pnodeweb",
  "setupFiles": [
    "/Users/jamuferguson/dev/paypal/p2pnodeweb/node_modules/babel-polyfill/lib/index.js"
  ],
  "testRunner": "/Users/jamuferguson/dev/paypal/p2pnodeweb/node_modules/jest-jasmine2/build/index.js",
  "scriptPreprocessor": "/Users/jamuferguson/dev/paypal/p2pnodeweb/node_modules/babel-jest/build/index.js",
  "usesBabelJest": true,
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/9d/p3qfw0g94yz7qh3z9dr4btwm391xgk/T/jest",
  "clearMocks": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "jsx",
    "node"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "preprocessorIgnorePatterns": [
    "/node_modules/"
  ],
  "resetModules": false,
  "testPathDirs": [
    "/Users/jamuferguson/dev/paypal/p2pnodeweb"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.jsx?$",
  "testURL": "about:blank",
  "timers": "real",
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": true,
  "watchman": true,
  "testcheckOptions": {
    "times": 100,
    "maxSize": 200
  }
}
Determining test suites to run...watchman warning:  Recrawled this watch 1 times, most recently because:
/Users/jamuferguson/dev/paypal/p2pnodeweb: kFSEventStreamEventFlagUserDropped
To resolve, please review the information on
https://facebook.github.io/watchman/docs/troubleshooting.html#recrawl
To clear this warning, run:
`watchman watch-del /Users/jamuferguson/dev/paypal/p2pnodeweb ; watchman watch-project /Users/jamuferguson/dev/paypal/p2pnodeweb`

 FAIL  ./my.test.js
  ● Test suite failed to run

    ReferenceError: requirejsVars is not defined

      at <anonymous>:2:3
      at Object.exports.runInThisContext (vm.js:54:17)
      at exec (node_modules/requirejs/bin/r.js:80:23)
      at setBaseUrl (node_modules/requirejs/bin/r.js:31952:13)
      at node_modules/requirejs/bin/r.js:32042:9
      at Object.<anonymous> (node_modules/requirejs/bin/r.js:32137:2)
      at Object.<anonymous> (my.test.js:1:145)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.767s
Ran all test suites matching "my.test.js".

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 25 (3 by maintainers)

Most upvoted comments

I worked around this problem with the following hack:

  • Create a manual mock for vm and replace runInThisContext

    // __mocks__/vm.js
    const vm = jest.genMockFromModule('vm');
    // r.js calls vm.runInThisContext during initialization
    // but when running under jest it does not work as expected.
    // This assumes nothing else calls this method during the tests...
    vm.runInThisContext = function () {
     return undefined
    };
    
    module.exports = vm;
    
  • Load the mock in any tests that transitively tries to load **r.js **

    // my_spec.js
    jest.mock('vm');
    

This only works because my code does not actually use require.js bundle loader in node.js it only uses the .optimize method for bundling. So I don’t mind potentially breaking require.js by providing this no-op mock.

Also if any other package needs to use vm.runInThisContext during the test it will be broken as well.

Yeah, let’s not spend time on this from our side. If somebody feels strongly and can find a clean fix, send a PR and tests, that is obviously always welcome.