Jest: Enable jest mocking to work properly with npm3.

Created on 16 Oct 2015  ·  14Comments  ·  Source: facebook/jest

Seeing this issue. Not sure where to begin with it.

Using Jest CLI v0.5.10
 FAIL  __tests__/editor/js/views/ElementsPanel/components/Libraries.react.test.js (1.97s)
● LibrariesComponent › it encountered a declaration exception
  - TypeError: /Users/jgt/Sites/app/node_modules/react-addons-test-utils/index.js: /Users/jgt/Sites/app/node_modules/react/lib/ReactTestUtils.js: /Users/jgt/Sites/app/node_modules/react/lib/React.js: /Users/jgt/Sites/app/node_modules/react/lib/ReactDOM.js: /Users/jgt/Sites/app/node_modules/react/lib/ReactDefaultInjection.js: /Users/jgt/Sites/app/node_modules/react/lib/BeforeInputEventPlugin.js: Cannot read property 'topCompositionEnd' of undefined
        at Object.<anonymous> (node_modules/react/lib/BeforeInputEventPlugin.js:65:33)
        at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:527:17)
        at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:239:9)
        at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:902:12)
        at Loader.requireModuleOrMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:923:17)
        at Object.<anonymous> (node_modules/react/lib/ReactDefaultInjection.js:14:30)
        at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:527:17)
        at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:239:9)
        at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:902:12)
        at Loader.requireModuleOrMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:923:17)
        at Object.<anonymous> (node_modules/react/lib/ReactDOM.js:18:29)
        at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:527:17)
        at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:239:9)
        at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:902:12)
        at Loader.requireModuleOrMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:923:17)
        at Object.<anonymous> (node_modules/react/lib/React.js:14:16)
        at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:527:17)
        at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:239:9)
        at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:902:12)
        at Loader.requireModuleOrMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:923:17)
        at Object.<anonymous> (node_modules/react/lib/ReactTestUtils.js:17:13)
        at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:527:17)
        at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:239:9)
        at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:902:12)
        at Loader.requireModuleOrMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:923:17)
        at Object.<anonymous> (node_modules/react-addons-test-utils/index.js:1:115)
        at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:527:17)
        at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:239:9)
        at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:902:12)
        at Loader.requireModuleOrMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:923:17)
        at Suite.<anonymous> (__tests__/editor/js/views/ElementsPanel/components/Libraries.react.test.js:4:19)
        at env.describe_ (evalmachine.<anonymous>:40:25)
        at env.describe (evalmachine.<anonymous>:27:19)
        at Object.<anonymous> (__tests__/editor/js/views/ElementsPanel/components/Libraries.react.test.js:3:1)
        at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:527:17)
        at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:239:9)
        at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:902:12)
        at jasmineTestRunner (node_modules/jest-cli/src/jasmineTestRunner/jasmineTestRunner.js:284:16)
        at node_modules/jest-cli/src/TestRunner.js:379:12
Bug

All 14 comments

Are you using npm3?

@cpojer

$ npm --version
$ 3.3.4

Also see: https://github.com/facebook/react/issues/5183

We don't have a good solution for this yet, unfortunately.

Manually unmocking fbjs as well as react should work for now.

Workarounds for React:

  1. Disable automocking entirely (_suggested because fuck mocking_)
  2. Disable mocking for node_modules/{react,fbjs}

Good to know 'fbjs' was the culprit. For me, even 'babel' and 'core-js' needed to be in that unmocked list.

For those confused:

I made this error go away by making my package.json look like this:

  "jest": {
    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/react",
      "<rootDir>/node_modules/fbjs",
      "<rootDir>/node_modules/react-dom",
      "<rootDir>/node_modules/react-addons-test-utils"
    ]
  },

Thanks @jezen, I made those changes and now the tests are working fine.

fyi the tentative plan is to wait for Haste 2 in #599 and then I'll fix unmock patterns to be deep-unmocks when they are inside of node_modules.

any idea of the timeline of when it'll be merged back to master? :) great job in this PR!!

should be out sometime by the end of this year, that's the plan at least :)

Will be fixed in 0.9.0. I'll publish a new version of jest-cli@next that solves this problem.

0.9.0 has been published and this issue is now properly fixed :)

getting following issue while running tests
node_modules\bootstrap\dist\css\bootstrap.min.css:5
//! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css
*/html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjus
t:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgrou
p,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{displa
y:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height
:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hove
r{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{fo
nt-style:italic}h1{margin:.67em 0;font-size:2em}mark{color:#000;background:#ff0}
small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertic
al-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){ov
erflow:hidden}figure{margin:1em 40px}hr{height:0;-webkit-box-sizing:content-box;
-moz-box-sizing
SyntaxError: Unexpected token {

  at transformAndBuildScript (node_modules\jest-cli\node_modules\jest-runtim

e\build\transform.js:320:12)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samzhang111 picture samzhang111  ·  3Comments

paularmstrong picture paularmstrong  ·  3Comments

stephenlautier picture stephenlautier  ·  3Comments

StephanBijzitter picture StephanBijzitter  ·  3Comments

withinboredom picture withinboredom  ·  3Comments