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

时间:2019-05-01 14:55:25

标签: javascript reactjs typescript jestjs create-react-app

正如标题所述,在使用 create-react-app 设置的项目中编写不正确的TypeScript代码时,我在终端上没有任何错误通过npm test运行测试时。也许这是预期的行为?但是,最好能得到错误信息以防止在测试中编写错误的TypeScript。

错误代码示例:

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

P.S。如果您想知道我是否通过以下方式使用create-react-app的TypeScript版本:npx create-react-app my-app --typescript。其他一切工作正常,如果我在组件文件中编写了错误的TypeScript,终端会告诉我

1 个答案:

答案 0 :(得分:1)

测试不进行类型检查。这些测试也不需要正确编译,尽管我不确定为什么会这样,因此测试中的类型错误不会显示出来。

如果要在测试中进行类型检查,请使用yarn tsc和默认配置。这将执行类型检查,并且设置了noEmit,因此不会生成任何内容。默认情况下,测试文件包含在配置中。

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

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

tsc && eslint --ext ts,tsx,js src && react-scripts test
相关问题