Underscore currently breaks in 'strict' mode while trying to access properties on the global object. The global object is being set using the (function(root) { ... })(this) pattern. However, in this case, root remains undefined and hence the issue.
Using the Function("return this")() pattern instead alleviates the issue, though arguably ugly. Thoughts?
Steps to reproduce: Import underscore in a babel-jest test harness. (uses strict mode)
Would be better to just create a root object on L12.
var root = this || {};
Underscore doesn't use strict mode.
It's not an issue of Underscore using strict mode, but the new ES6 module loaders forcing strict mode.
Seriously? Strict mode is enforced now in "real" ES6? Or is this just "polyfill" module loaders being zealous?
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-strict-mode-code
Ugh.
Is this going to play nice with _.template?
It's not strict mode exactly. The ES6 spec says that this is undefined for module contexts.
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-strict-mode-code
I stand corrected, it's always strict mode.
Can we replace this with self? I'm pretty sure that should work with all browsers I've ever used
^ That would also add support for loading underscore in webworkers
@jashkenas jest doesn't play well with loading underscore either.
Also, this issue affects Backbone as well. Relevant issue: jashkenas/backbone#3566
@megawac self won't work in node or iojs.
See pull
On Apr 16, 2015 8:13 AM, "Diwank Singh Tomer" [email protected]
wrote:
@megawac https://github.com/megawac self won't work in node or iojs.
—
Reply to this email directly or view it on GitHub
https://github.com/jashkenas/underscore/issues/2152#issuecomment-93719147
.
ran into this when using babel-jest as scriptPreprocessor. Any idea? 1.8.3
"dependencies": {
"react-native": "^0.12.0",
"react-native-animatable": "^0.1.0",
"react-native-webintent": "^1.0.2"
},
"devDependencies": {
"babel-jest": "*",
"jest-cli": "^0.7.0"
},
"jest": {
"collectCoverage": true,
"rootDir": "./", (I also used "", but same thing)
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"testFileExtensions": ["es6", "js"],
"moduleFileExtensions": ["js", "json", "es6"]
}
● Runtime Error
TypeError: /Users/yourname/Proj/ReactNativeExperiment/__tests__/test2-test.js: /Proj/src/components/assets/multiSelect.js:Proj/lib/underscore.js: Cannot read property '_' of undefined
at Proj/lib/underscore.js:15:32
at Object.<anonymous> (Proj/src/lib/underscore.js:1548:1)
at Object.runContentWithLocalBindings (/Proj/node_modules/jest-cli/src/lib/utils.js:394:17)
at Loader._execModule (/Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
at Loader.requireModule (/Prou/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
at Loader.requireModuleOrMock /Prou/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:875:19)
at Object.<anonymous> (Proj/src/components/assets/multiSelect.js:9:45)
at Object.runContentWithLocalBindings (/Proj/node_modules/jest-cli/src/lib/utils.js:394:17)
at Loader._execModule (/Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
at Loader.requireModule (Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
at Loader.requireModuleOrMock (Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:875:19)
at Object.<anonymous> (//Proj/__tests__/test2-test.js:4:19)
at Object.runContentWithLocalBindings (/Proj/node_modules/jest-cli/src/lib/utils.js:394:17)
at Loader._execModule /Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
at Loader.requireModule (/Proj//node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
at jasmineTestRunner (/Proj/node_modules/jest-cli/src/jasmineTestRunner/jasmineTestRunner.js:264:16)
at /Proj/node_modules/jest-cli/src/TestRunner.js:379:14