isolatedModules error on Jest test with Create React App and TypeScript?

五迷三道 提交于 2020-07-18 09:24:36

问题


I've started a Create React App project with --typescript. When I write a test Im getting a compiler error:

// something-test.tsx
test('something', ()=>{
  expect(1).toBe(1)
})

The error is:

TS1208: All files must be modules when the '--isolatedModules' flag is provided.

From googling I thought the fix was to create a jest.config.tsx with:

module.exports = {
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
};

However it's made no difference.


回答1:


You do not have any import statements in your code. This basically means you are not testing anything outside of the test file.

If you test something that is not in the test code (and therefore import something), the test file will become a module and the error will go away 🌹



来源:https://stackoverflow.com/questions/57860261/isolatedmodules-error-on-jest-test-with-create-react-app-and-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!