Jest.js and Create React app Get Syntax Error: Cannot use Import Statement Outside a Module when Running Test


import moment from "moment-timezone";

let result = moment().format();

    it("should return a fail..or at least something",()=>{


npx jest or yarn test or npm test


npx jest
 FAIL  src/App.test.js
  ● Test suite failed to run

    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import moment from "moment-timezone";

    SyntaxError: Cannot use import statement outside a module

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.586s
Ran all test suites.

It looks like it is similar to this:

Do npm ls jest it says there are missing dependencies. Manually install them they don’t install and the problem is not fixed.

UNMET DEPENDENCY react-scripts@2.1.5
  └── UNMET DEPENDENCY jest@23.6.0 

you add jest manually, remove jest dependency from package.json and run npm install or yarn install.

or you can run npm test or yarn test to check the instruction.

This Post Has One Comment

  1. Malki Nama

    To fix that do you need create the file: ./__tests__/setup.js and put, After that you only need run: npm run test,

Leave a Reply