Fixing error : Cannot find module ‘jest’ from ‘import-editions-test.js’

If you try to use ES6 import syntax with Jest, you can get the following error:

import { describe } from 'jest';
Using Jest CLI v13.2.3, jasmine2
 FAIL  dist\__tests__\import-editions-test.js (0s)
● Runtime Error
  - Error: Cannot find module 'jest' from 'import-editions-test.js'
        at Resolver.resolveModule (node_modules\jest-resolve\build\index.js:197:17)
        at Runtime._resolveModule (node_modules\jest-runtime\build\index.js:327:32)
        at Runtime._normalizeID (node_modules\jest-runtime\build\index.js:492:31)
        at Runtime._shouldMock (node_modules\jest-runtime\build\index.js:536:27)
        at Runtime.requireModuleOrMock (node_modules\jest-runtime\build\index.js:271:14)
        at Object. (dist\__tests__\import-editions-test.js:3:16)
        at Runtime._execModule (node_modules\jest-runtime\build\index.js:375:17)
        at Runtime.requireModule (node_modules\jest-runtime\build\index.js:210:14)
        at jasmine2 (node_modules\jest-jasmine2\build\index.js:293:11)
        at Test.run (node_modules\jest-cli\build\Test.js:50:12)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 0.983s)
npm ERR! Test failed.  See above for more details.

Unfortunately this seems to mean that you can’t use ES6 imports at all, which means losing TypeScript bindings. To fix this, you can just remove the ‘import’ and it should work.

If you want a good Javascript book, I got a lot out of Secrets of the Javascript Ninja.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *