Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type check test files #5626

Open
affanshahid opened this issue Oct 30, 2018 · 10 comments
Open

Type check test files #5626

affanshahid opened this issue Oct 30, 2018 · 10 comments

Comments

@affanshahid
Copy link

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?

@martinmckenna
Copy link

martinmckenna commented Oct 30, 2018

Check out your tsconfig.json.

Change this to an empty array if you want to include them

  "exclude": [
    "**/__tests__/**",
    "**/?*test.*",
    "**/?*spec.*"
  ]

@eddedd88
Copy link
Contributor

eddedd88 commented Oct 30, 2018

the types are checked in the build script

@Timer
Copy link
Contributor

Timer commented Oct 30, 2018

This is currently a known limitation. We can check these types in the future.

For now, please rely on VSCode to type check your tests or run tsc.

@beausmith
Copy link

beausmith commented 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"
    }
  },

@iansu iansu added this to To do in v3 Mar 10, 2019
@lorenzorapetti
Copy link
Contributor

We can also use ts-jest which does the actual typechecking in jest.

@ianschmitz
Copy link
Contributor

We can also use ts-jest which does the actual typechecking in jest.

The future of ts-jest is uncertain. We are eager to get some form of type checking in for test files, but we also don't want to adopt a library that potentially won't be maintained going forward. We are exploring other options to satisfy this that better fits in our current ecosystem of tools.

@iansu iansu removed this from To do in v3 Apr 3, 2019
@iansu iansu modified the milestones: 3.0, 3.x Apr 3, 2019
@gpg0
Copy link

gpg0 commented Sep 5, 2019

This is also happening to me. Any updates?

netzwerg added a commit to netzwerg/react-you-do-you that referenced this issue Sep 11, 2019
- Invoke TSLint
- Invoke tsc to type-check tests (facebook/create-react-app#5626)
larsthorup added a commit to larsthorup/react-redux-typescript-sandbox that referenced this issue Feb 15, 2020
@ahnpnl
Copy link

ahnpnl commented Apr 21, 2020

FYI: kulshekhar/ts-jest#1549 will be in alpha version of ts-jest (possibly today). You can test the alpha version and give some feedbacks for kulshekhar/ts-jest#1115

BboyStatix added a commit to crossroads/app.goodchat.hk that referenced this issue Dec 8, 2020
* GCW-3432 Remove routing logic

* GCW-3432 Create component for main routing

Essential for testing routing by allowing us to mock router history

* GCW-3432 Move MainRouter to its own directory

* GCW-3432 Test for presence of ion-router-outlet

Ionic uses this to handle animations related to navigation/routing

* GCW-3432 Move router outlet to MainRouter

Component required by Ionic so rather not have to mock this out in tests

* GCW-3432 Install history

* GCW-3432 Test navigation to /home url

* GCW-3432 Use Router instead of IonReactRouter

Components do not show up as expected on JSDOM with IonReactRouter

* GCW-3432 Show the home page

* GCW-3432 Create login page and routing

* GCW-3432 Refactor tests

* GCW-3432 Remove test in incorrect location

Test belongs to MainRouter

* GCW-3432 Add missing assertion in test

* GCW-3432 Add assertion for ion-router-outlet

* GCW-3432 Make assertions more explicitly

* GCW-3432 Create context for authentication state

* GCW-3432 Route users based on auth state

* GCW-3432 Downgrade history version

Latest version has compatibility issues esp. redirects

* GCW-3432 Test redirect normally without spyOn

Reverting history ver. to 4 fixes the issue with redirects

* GCW-3432 Redirect user from root path

Routing should be based on user auth status

* GCW-3432 Add title to login page

* GCW-3432 Test without resorting to data-testid

Rely on actual DOM elements for testing rather than data-testid

* GCW-3432 Change file naming convention

index.ts convention makes it harder to distinguish tabs in editors

* GCW-3432 Put Login into its own folder

* GCW-3432 Move MainRouter to components folder

* GCW-3432 Create login button

* GCW-3432 Simplify test element selectors

* GCW-3432 Create login functionality

* GCW-3432 Update ion router outlet test

Don't rely on data-testid

* GCW-3432 Encapsulate auth access in custom hook

* GCW-3432 Install TS + add type checking to tests

Reference: facebook/create-react-app#5626

* GCW-3432 Encapsulate auth state in Provider

* GCW-3432 Update tests to use AuthProvider

* GCW-3432 Wrap app with AuthProvider

* GCW-3432 Fix type error in test

* GCW-3432 Refactor tests

* GCW-3432 Refactor test expectations to function

* GCW-3432 Refactor tests

Change function signature of render function to allow for currying

* GCW-3432 Refactor tests

* GCW-3432 Route from non-existent page (unauthed)

* GCW-3432 Route from non-existent page (authed)

* GCW-3432 Remove redundant route

* GCW-3432 Handle visiting page with /login prefix

* GCW-3432 Handle visiting page with /home prefix

* GCW-3432 Encapsulate private route logic

* GCW-3432 Refactor login tests

* GCW-3432 Redirect user to home on login

* GCW-3432 Increase test clarity

* GCW-3432 Move expectation fn to test utils
@DjebbZ
Copy link

DjebbZ commented Sep 24, 2021

Any update on this point ?

microbit-matt-hillsdon added a commit to microbit-foundation/python-editor-v3 that referenced this issue Jan 25, 2022
microbit-matt-hillsdon added a commit to microbit-foundation/python-editor-v3 that referenced this issue Jan 25, 2022
Neither CRA's build nor test steps do this.

Fix various minor typing errors to get this clean again.

facebook/create-react-app#5626
microbit-matt-hillsdon added a commit to microbit-foundation/python-editor-v3 that referenced this issue Jan 25, 2022
Neither CRA's build nor test steps do this.

Fix various minor typing errors to get this clean again.

facebook/create-react-app#5626
@MustardForBreakfast
Copy link

MustardForBreakfast commented Apr 25, 2022

This behavior comes from how Jest runs against typescript. Not really a CRA thing per se, as CRA is just using a relatively vanilla Jest typescript configuration under the hood: https://jestjs.io/docs/getting-started#using-typescript

From the Jest docs:

there are some caveats to using TypeScript with Babel. Because TypeScript support in Babel is purely transpilation, Jest will not type-check your tests as they are run. If you want that, you can use ts-jest instead, or just run the TypeScript compiler tsc separately (or as part of your build process).

Jest seems to just recommend running tsc (or react-scripts build) separately somewhere as part of your CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests