Do you want to request a _feature_ or report a _bug_?
Bug
What is the current behavior?
Importing [email protected] and [email protected] 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
> [email protected] 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.
Array and Object should definitely be there – the vm object creates those. I don't really know why lodash three never worked – can you investigate a bit? They fixed their detection in lodash 4.
The global detection in lodash 3 looks looks like this:
/** Detect free variable `exports`. */
var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
/** Detect free variable `module`. */
var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
/** Detect free variable `global` from Node.js. */
var freeGlobal = freeExports && freeModule && typeof global == 'object' && global && global.Object && global;
It doesn't choose the global on the line where it tries to assign freeGlobal, because global.Object evaluates to undefined.
RxJS also can't be imported because global.Object is undefined. (It assigns global as the "root object" here and fails with TypeError: Cannot read property 'assign' of undefined here because global.Object is undefined.)
In fact, all of the following evaluate to undefined in Jest node environment:
global.Array,
global.Date,
global.Error,
global.Function,
global.Math,
global.Number,
global.Object,
global.RegExp,
global.String,
global.TypeError
(The repro includes a test case that shows this.)
Just to add to this, importing [email protected] 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;
@fson what is your workaround for now?
@sibelius I'm using the jsdom environment for now.
This also breaks Immutable.fromJS
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
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
I'm also now seeing TypeError: myArray.includes is not a function. Is this related. It seems like if I log [].includes I get undefined, even though I thought we were using babel and all that. Is this related?
I recommend updaring to Node 6+
Will be fixed in the next release.
@xjamundx how have u added this globals to jest-environment-node?
you can try jest@test which has a fix for this.
thanks @cpojer, u rock 💯
Most helpful comment
Will be fixed in the next release.