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