jsjoeio / react-ts-vitest-template

A template with React, TypeScript, Vite, Vitest, React Testing Library
10 stars 8 forks source link

fix types this way #1

Closed jsjoeio closed 1 year ago

jsjoeio commented 2 years ago

https://github.com/testing-library/jest-dom/issues/427#issuecomment-1177538312

jschuur commented 1 year ago

testing-library/jest-dom#427 (comment)

Is this template still good for the latest versions of the different dependencies?

TypeScript in VS Code currently tells me Property 'toBeInTheDocument' does not exist on type 'Assertion<HTMLElement>'. here

expect(screen.getByText("Learn React")).toBeInTheDocument();

and then other errors relating to describe in Avatar.test.tsx

Cannot find name 'describe'. Do you need to install type definitions 
for a test runner? Try `npm i --save-dev @types/jest` or `npm i 
--save-dev @types/mocha`.ts(2582)

I can fix the latter by importing describe, expect etc from vitest, but not sure about the former.

The tests run just fine. I tried to force VS Code to use a local version of TypeScript 4.9.4, but that didn't make a difference.

jschuur commented 1 year ago

Well, somewhere along the line my TS errors went away: https://github.com/jschuur/react-testing-test

I imagine this is related to the type narrowing that I'm doing:

const input = screen.getByRole('textbox');

if (!(input instanceof HTMLInputElement)) {
   throw new Error('expected input to be an HTMLInputElement');
}
jsjoeio commented 1 year ago

Is this template still good for the latest versions of the different dependencies?

I haven't tested it in a while so not sure unfortunately.

Well, somewhere along the line my TS errors went away: https://github.com/jschuur/react-testing-test

Ah nice!! So all is good then?

jschuur commented 1 year ago

Ah nice!! So all is good then?

Yup. In the sense that eventually, in on my own project things randomly started working :)

I googled some of the error messages and found a few threads (including the one you were on) pointing at causes but it was a little beyond my abilities to understand. So I just hit it with a hammer until it worked.

jsjoeio commented 1 year ago

So I just hit it with a hammer until it worked

Honestly...most days that is my approach if Google and ChatGPT fail me 😂 Glad you got it resolved!

jsjoeio commented 1 year ago

Closing because who knows if I'll ever get around to fixing this 😂