How to resolve “Cannot use import statement outside a module” in jest

前端 未结 7 1712
庸人自扰
庸人自扰 2020-12-15 16:39

I have a React application (not using Create React App) built using TypeScript, Jest, Webpack, and Babel. When trying to run \"yarn jest\", I get the following error:

相关标签:
7条回答
  • 2020-12-15 17:36

    I encountered the same problem with Typescript, Jest, and VueJS/VueCli 3. The normal build has no problem. only happens for Jest. I struggled for hours by searching. But no answer actually works. In my case, I have a dependency on my own typescript package which I specific "target": "es6" in the tsconfig.json. That's the root cause. So the solution is simply to change the dependent's (Not the same project) back to es5 tsconfig.json:

    {
      "compilerOptions": {
        "target": "es5",
        ...
      },
      ...
    }
    
    0 讨论(0)
提交回复
热议问题