使用 create-react-app 通过 JEST 运行测试时,不会显示 TypeScript 错误

Chr*_*n.H 8 javascript typescript reactjs jestjs create-react-app

正如标题所说,在使用create-react-app设置的项目中编写不正确的 TypeScript 代码时,通过. 也许这是预期的行为?然而,如果能得到错误以防止在测试中编写不正确的 TypeScript 会很好npm test

错误代码示例:

// App.test.tsx
it('Test of incorrect TypeScript', () => {
  let aSimpleString: string = 'hello';
  aSimpleString = 5;
});
Run Code Online (Sandbox Code Playgroud)

PS如果你想知道我现在用的打字稿版本创建反应的应用程序内通过:npx create-react-app my-app --typescript。其他一切正常,如果我在组件文件中编写了不正确的 TypeScript,终端会让我知道

Exp*_*lls 6

测试不做类型检查。测试也不需要正确编译,虽然我不确定为什么会这样,所以测试中的类型错误不会出现。

如果要对测试进行类型检查,请使用yarn tsc默认配置。这将执行类型检查,并且它已noEmit设置为不会构建任何内容。默认情况下,测试文件包含在配置中。

如果您愿意,还可以将测试脚本更新为:tsc && react-scripts test

请注意,这只会进行类型检查。您还可以使用 eslint 进行 linting,例如

tsc && eslint --ext ts,tsx,js src && react-scripts test
Run Code Online (Sandbox Code Playgroud)

  • 没关系,找到了[这个](https://github.com/facebook/create-react-app/issues/5626) (2认同)