question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type check test files

See original GitHub issue

Bug report

I created a new project with the --typescript flag, and added a type for my component’s props like so:

type AppProps = {
  name: string
};

class App extends Component<AppProps> {
  render() {
    return (
      ...
    );
  }
}

In index.tsx when I write something like (note the missing prop):

ReactDOM.render(<App />, document.getElementById('root')); // name prop is missing

I get an error in both my editor (VSCode) and on my console when I run yarn start.

However in my tests if I have something like:

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
  ReactDOM.unmountComponentAtNode(div);
});

I get the type error in my editor but NOT when I run yarn test, the tests run successfully in fact.

Is this intended?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:41
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
beausmithcommented, Mar 5, 2019

Another option, update your test scripts to run tsc.

Update your package.json file:

- "test": "react-scripts test",
+ "test": "tsc && react-scripts test",

The tsc command will look for compiler options in the tsconfig.json file if one exists.

Additional Optional Solutions

If you have a different testing script for CI then add tsc to that one too:

"test:ci": "CI=true tsc && react-scripts test --env=jsdom --coverage",

If you’re using husky, add tsc to your pre-commit hook:

  "husky": {
    "hooks": {
      "pre-commit": "tsc && lint-staged"
    }
  },
6reactions
DjebbZcommented, Sep 24, 2021

Any update on this point ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Types | Guide - Vitest
Vitest allows you to write tests for your types, using expectTypeOf or assertType syntaxes. By default all tests inside *.test-d.ts files are ...
Read more >
How to disable ALL type checking in *.test.* files (including ...
Is there a way to disable ALL type checking for all test files from the tsconfig, including any imported components from non-test files?...
Read more >
Exclude test files from Compilation in TypeScript | bobbyhadz
To exclude test files from compilation, but still have them type checked, create a second configuration file, e.g. tsconfig.build.json , which uses the ......
Read more >
How setting up unit tests with TypeScript - Medium
Unit testing is one of the most valuable and tedious measures of your codebase ... tsconfig to check the test files during the...
Read more >
Structuring Tests - Node Tap
Each file that is run by tap defines a "suite". The ideal structure and organization of these suites will depend on what kind...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found