Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript errors doesn't show when running tests through JEST with create-react-app

As the title says, when writing incorrect TypeScript-code in a project set up with create-react-app, I don't get any errors in the terminal when running the tests through npm test. Maybe this is expected behaviour? Would however be nice to get the errors to prevent one from writing incorrect TypeScript in tests as well

Sample of incorrect code:

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

P.S. In case you were wondering I am using the TypeScript-version of create-react-app through: npx create-react-app my-app --typescript. Everything else works fine, and if I write incorrect TypeScript in component files the terminal let's me know

like image 643
Christian.H Avatar asked May 01 '19 14:05

Christian.H


1 Answers

Testing does not do type checking. The tests also don't need to compile properly, although I'm not sure why this is, so type errors in the tests don't manifest.

If you want to do type checking on the tests, use yarn tsc with the default config. This will perform type checking, and it has noEmit set so it will not build anything. The test files are included in the config by default.

If you like, you can also update the test script to: tsc && react-scripts test.

Note that this will only do type checking. You can also use eslint for linting, e.g.

tsc && eslint --ext ts,tsx,js src && react-scripts test
like image 65
Explosion Pills Avatar answered Sep 29 '22 03:09

Explosion Pills