You are here
Home > Technology > Troubleshooting > SyntaxError: Cannot use import statement outside a module

SyntaxError: Cannot use import statement outside a module

If you’re using Supertest & JEST with a Node API to write test using TypeScript , you may encounter the following error:

  Jest encountered an unexpected token
  SyntaxError: Cannot use import statement outside a module
  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1479:14) 

To solve this, we need to tell JEST that we intend to write a test in a Node environment with TS. Add following setting in package.json (or JEST config if you are using that)

"jest": {
    "preset": "ts-jest",
    "testEnvironment": "node"
  },

Don’t forget to install ts-jest dependency

npm -i D ts-jest
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
Top
0
Would love your thoughts, please comment.x
()
x