2

I'm using ES6 style and want to be consistent of using import from ES6.

Here is my jest test file I'm setting up:

import Get from './Get'

test('Some test', async () => {
    expect(true).toBe(true)
})

And here is my Get file:

import cli from "commander";
import get from "./myHandler.js"

const GET = () => {
    // Doing some stuff here...
}

export default GET

I keep getting:

    C:\...\GET\get.test.js:2
    import Get from './Get';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1773:14)

And I have already added "type": "module", in my package.json as suggested by this and others. What am I doing wrong here?

Amir-61
  • 23
  • 4
  • 2
    This is not a node.js problem but rather a jest.js problem – Bergi May 15 '22 at 01:30
  • 1
    Try this https://stackoverflow.com/questions/68956636/how-to-use-esm-tests-with-jest/69059786#69059786 – morganney May 15 '22 at 01:49
  • Jest's support for native modules is severely lacking, unfortunately. Can you post your jest config? Also are you using TypeScript by any chance? Also what version of Jest and Node, and the command you're using to run the script? – Derek May 23 '22 at 04:00

0 Answers0