jest: Importing lodash, rxjs fails in node environment, globals undefined

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

Bug

What is the current behavior?

Importing lodash@3.10.1 and rxjs@5.0.0-beta.11 fails in Jest, when testEnvironment is set to node.

Here’s a minimal repository reproduce the bug: https://github.com/fson/sandbox/tree/3ad45f19a1a3efa711cc8eba44fe349673471092/jest-node-environment-repro (just npm install and npm test).

It seems that the problem is that some globals (e.g. Array, Object) are missing from the global object created by jest-environment-node.

What is the expected behavior?

Expected to be able to import lodash and rxjs without errors thrown.

Tested on OS X 10.11.6, Node.js v6.4.0.

> npm test -- --debug

> jest-node-environment-repro@1.0.0 test /Users/ville/Projects/sandbox/jest-node-environment-repro
> jest "--debug"

jest version = 15.1.1
test framework = jasmine2
config = {
  "testEnvironment": "/Users/ville/Projects/sandbox/jest-node-environment-repro/node_modules/jest-environment-node/build/index.js",
  "rootDir": "/Users/ville/Projects/sandbox/jest-node-environment-repro",
  "name": "-Users-ville-Projects-sandbox-jest-node-environment-repro",
  "setupFiles": [
    "/Users/ville/Projects/node_modules/babel-polyfill/lib/index.js"
  ],
  "testRunner": "/Users/ville/Projects/sandbox/jest-node-environment-repro/node_modules/jest-jasmine2/build/index.js",
  "scriptPreprocessor": "/Users/ville/Projects/sandbox/jest-node-environment-repro/node_modules/babel-jest/build/index.js",
  "usesBabelJest": true,
  "preprocessorIgnorePatterns": [
    "/node_modules/"
  ],
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/qm/_psd4jjx3t3d_7hr4w4m6jpr0000gn/T/jest",
  "colors": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "node"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "resetModules": false,
  "testPathDirs": [
    "/Users/ville/Projects/sandbox/jest-node-environment-repro"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.js$",
  "testURL": "about:blank",
  "timers": "real",
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": true,
  "watchman": true,
  "testcheckOptions": {
    "times": 100,
    "maxSize": 200
  }
}
 FAIL  ./rxjs.test.js
  ● can import rxjs

    TypeError: Cannot read property 'assign' of undefined

      at Object.<anonymous> (node_modules/rxjs/util/assign.js:4:18)

 FAIL  ./lodash.test.js
  ● can import lodash

    TypeError: Cannot read property 'prototype' of undefined

      at runInContext (node_modules/lodash/index.js:718:27)
      at Object.it (lodash.test.js:2:18)

 FAIL  ./globals.test.js
  ● defines globals like Node.js does

    expect(received).toBeDefined()

    Expected value to be defined, instead received
      "undefined"

      at Object.it (globals.test.js:2:24)

Test Summary
 › Ran all tests.
 › 3 tests failed, 0 tests passed (3 total in 3 test suites, run time 0.649s)
npm ERR! Test failed.  See above for more details.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 12
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Will be fixed in the next release.

I’ve added the @xjamundx’s code to the setupTestFrameworkScriptFile’s file and it worked.

Maybe we need to add that in https://github.com/facebook/jest/blob/master/packages/jest-environment-node/src/index.js#L29?

I think this is the whole list

global.Array = Array
global.Date = Date
global.Function = Function
global.Math = Math
global.Number = Number
global.Object = Object
global.RegExp = RegExp
global.String = String
global.Uint8Array = Uint8Array
global.WeakMap = WeakMap
global.Set = Set
global.Error = Error
global.TypeError = TypeError
global.parseInt = parseInt
global.parseFloat = parseFloat

@cpojer

To get loadsh to work I had to add the following in jest-environment-node:

   // missing globals
    global.Array = Array;
    global.Object = Object;
    global.String = String;
    global.Function = Function;
    global.RegExp = RegExp;
    global.Math = Math;
    global.Number = Number;
    global.Date = Date;
    global.parseInt = parseInt

Just to add to this, importing lodash@4.15.0 also results in TypeError: Cannot read property 'prototype' of undefined, because its global detection looks like this:

/** Detect free variable `global` from Node.js. */
  var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;

@sibelius I’m using the jsdom environment for now.