【问题标题】:isolatedModules error on Jest test with Create React App and TypeScript?使用 Create React App 和 TypeScript 进行 Jest 测试时的 isolatedModules 错误?
【发布时间】:2020-01-11 14:08:59
【问题描述】:

我已经使用 --typescript 启动了一个 Create React App 项目。当我写一个测试我得到一个编译器错误:

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

错误是:

TS1208:如果提供了“--isolatedModules”标志,所有文件都必须是模块。

通过谷歌搜索,我认为解决方法是创建一个 jest.config.tsx:

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

然而这并没有什么不同。

【问题讨论】:

    标签: typescript jestjs create-react-app


    【解决方案1】:

    您的代码中没有任何import 语句。这基本上意味着您没有测试测试文件之外的任何内容。

    如果你测试了一些不在测试代码中的东西(因此导入了一些东西),测试文件将变成一个模块,错误就会消失?

    【讨论】:

    • 我试图从 tsconfig 中排除测试文件,但这更有意义,不要做不测试任何东西的测试,但如果你想制作一个测试文件只是为了测试测试正常,尝试导入一些随机模块,它会工作。
    • 添加随机导入会使文件中有一个未使用的导入。
    猜你喜欢
    • 2020-07-22
    • 2017-07-04
    • 2019-01-30
    • 2017-02-25
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2020-07-01
    相关资源
    最近更新 更多