-1

app.js

export const temp = "Hello World";

app.test.js

import { temp } from "./app.js";

When I run npm test it throws "SyntaxError: Cannot use import statement outside a module"

This is the test script: {scripts: {test: "jest"}}

I've enabled ES modules by adding "type": "module" in package.json.

2 Answers2

0

Make sure you followed the instructions provided on the ECMA Modules page of the Jest documentation. If that doesn’t work, can you provide a reproduction? It would also help to know your version of Node/Jest/etc.

Bart
  • 1,600
  • 1
  • 13
  • 29
0

I had to add the experimental-vm-modules flag

package.json file:

"scripts": {
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
}
Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28