testing-library / react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
https://testing-library.com/react
MIT License
18.84k stars 1.09k forks source link

fix: Don't raise TypeScript errors when hydating `document` #1304

Closed eps1lon closed 2 months ago

eps1lon commented 2 months ago

Closes https://github.com/testing-library/react-testing-library/issues/1250

As a follow-up, we need to make @types/react-dom a peer dependency. Since react-dom is a peer, the types also need to be peer. Otherwise you would get the wrong version of @types/react-dom if you'd install React 19.

codesandbox-ci[bot] commented 2 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6c1fdf789d6ebfb6acd82a797ef447ba9feee3d8:

Sandbox Source
react-testing-library-examples Configuration
eps1lon commented 2 months ago

Do we want to merge this as a fix before merging the peer deps change?

Yeah, I also want to wait a week (or until React 19 alpha comes out) before merging https://github.com/testing-library/react-testing-library/pull/1305

AviVahl commented 2 months ago

@eps1lon we see type errors since this change:

node_modules/@testing-library/react/types/index.d.ts:129:52 - error TS2344: Type 'Container' does not satisfy the constraint 'Element | DocumentFragment'.
  Type 'Document | Container' is not assignable to type 'Element | DocumentFragment'.
    Type 'Document' is not assignable to type 'Element | DocumentFragment'.
      Type 'Document' is missing the following properties from type 'Element': attributes, classList, className, clientHeight, and 100 more.
        Type 'Container' is not assignable to type 'DocumentFragment'.
          Type 'Document | Container' is not assignable to type 'DocumentFragment'.
            Type 'Document' is not assignable to type 'DocumentFragment'.
              Types of property 'ownerDocument' are incompatible.
                Type 'null' is not assignable to type 'Document'.

can be reproduced with:

mkdir check-testinglib
cd check-testinglib
npm init -y
npm i react react-dom @types/react @types/react-dom typescript @testing-library/react
npx tsc --init --outDir dist --skipLibCheck false
echo "export * from '@testing-library/react';" > test.ts
npx tsc --noEmit