9

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

Juan Rivas
  • 585
  • 1
  • 3
  • 17
Christian.H
  • 126
  • 8

1 Answers1

9

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
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • This has been bothering me for a day and if this is true it's a bummer - especially for people who write libraries. Does anybody know why type checking is not done in test? – sidecus Jul 13 '20 at 21:04
  • 3
    never mind, found [this](https://github.com/facebook/create-react-app/issues/5626) – sidecus Jul 13 '20 at 21:07