Skip to content

The inferred type of 'renderWithTheme' cannot be named without a reference to @types/testing-library__dom/queries'. This is likely not portable. A type annotation is necessary. #587

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

Closed
RIP21 opened this issue Feb 12, 2020 · 10 comments

Comments

@RIP21
Copy link

RIP21 commented Feb 12, 2020

  • @testing-library/react version: 9.4.0
  • react version: 16.12.0
  • node version: 12.14.1
  • pnpm version: 4.9.3
  • yarn version: 1.21.1

Relevant code or config:

import { render } from '@testing-library/react'
import { withTheme } from './withTheme'

export const renderWithTheme = (component: React.ReactElement) =>
  render(withTheme(component)) // No matter withTheme or just render, same error

What you did:

What happened:

Reproduction:

  1. Install Typescript
  2. Install @testing-library/react
  3. Add any function that takes component and returns render(component)
  4. Run tsc with following config
{
  "compilerOptions": {
    "baseUrl": "src",
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "declarationDir": "./types",
    "declarationMap": true,
    "downlevelIteration": true,
    "emitDeclarationOnly": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "preserve",
    "lib": ["dom", "es2018"],
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "esnext"
  },
  "include": ["src"],
  "exclude": [
    "./src/**/__tests__/**/*.ts",
    "./src/**/__tests__/**/*.tsx",
    "./src/**/*.test.tsx",
    "./src/**/*.test.ts"
  ]
}

Problem description:

I tried yarn as well as the pnpm, the result is the same all the time.
Cannot compile declarations with this error:

The inferred type of 'renderWithTheme' cannot be named without a reference to '../../../../common/temp/node_modules/.pnpm/registry.npmjs.org/@types/testing-library__react/9.1.2/node_modules/@types/testing-library__dom/queries'. This is likely not portable. A type annotation is necessary.

Suggested solution:

I have no idea.

@RIP21
Copy link
Author

RIP21 commented Feb 12, 2020

All typings are the freshest one (dom, react, react-hooks) explicitly installed in package.json due to pnpm strict nature that doesn't see any typings/modules included in the dependencies of the dependent packages so I had to that. With yarn tho, and no explicit typings in package.json result is pretty much the same.
typescript is of 3.7.5 version.

@RIP21
Copy link
Author

RIP21 commented Feb 13, 2020

The solution was to set preventSymlinks to true in tsconfig. I spent 3 days on that. Dayum :(

@RIP21 RIP21 closed this as completed Feb 13, 2020
@CodingDive
Copy link

@RIP21 do you mean the compiler option preserveSymlinks? Also didn't fix the error for me.

@RIP21
Copy link
Author

RIP21 commented Sep 26, 2020

@CodingDive yes. It's not guaranteed to fix the issue surely, but sometimes it indeed fixes it. Another way to try to fix it is described here
microsoft/TypeScript#36800
So import such as import * as ___ from @types/testing-library__dom/queries and ignore it as it will be unused, can fix it. Or without queries or just the library itself. It's super tricky and rather try and trial but sometimes the hack mentioned above works.

@RIP21
Copy link
Author

RIP21 commented Sep 26, 2020

Regarding preventSymlinks it's better to keep it false and enable it only for declaration generation. Regular compilation without any emit should be done with preventSymlinks: false to prevent typings from being missing as preventSymlinks kinda affects it this way somehow (nature of that I cannot explain), especially in pnpm managed repos.

@tu4mo
Copy link

tu4mo commented Oct 1, 2020

I had both @testing-library/dom and @testing-library/react as deps. @testing-library/react already has @testing-library/dom as dependency and it was a different version. Removing @testing-library/dom fixed the issue.

mstuartf added a commit to newscorp-ghfb/NewsKit that referenced this issue Aug 15, 2022
mstuartf added a commit to newscorp-ghfb/NewsKit that referenced this issue Aug 15, 2022
- testing-library/react-testing-library#587
- prevents 'inferred type of 'renderWithTheme' cannot be named'
mutebg added a commit to newscorp-ghfb/NewsKit that referenced this issue Aug 26, 2022
* feat(PPDSC-2242): update to node 18

* feat(PPDSC-2242): update types

* feat(PPDSC-2242): remove @types/tar

* feat(PPDSC-2242): update unite tests

* feat(PPDSC-2242): update select and form tests

* feat(PPDSC-2242): update form tests

* feat(PPDSC-2242): update test

* feat(PPDSC-2242): update tests after merge main

* feat(PPDSC-2242): update e2e build for react 18

* feat(PPDSC-2242): fix doc site build for react 18

* feat(PPDSC-2242): update links

* feat(PPDSC-2242): update links

* feat(PPDSC-2242): bump cypress cache version

* feat(PPDSC-2242): update cypress version

* feat(PPDSC-2242): fix hydration using span

* feat(PPDSC-2242): fix hydration using span 2

* feat(PPDSC-2242): fix column-renderer

* feat(PPDSC-2242): fix hydration using span 3

* feat(PPDSC-2242): add wait to accessibility tests

* feat(PPDSC-2242): small changes to tidy up code

* feat(PPDSC-2242): update type

* feat(PPDSC-2242): textBlock to styled TextBlock

* feat(PPDSC-2242): fix story

* feat(PPDSC-2242): update yarn.lock

* feat(PPDSC-2242): add type after merge

* feat(PPDSC-2242): remove unused border card prop

* feat(PPDSC-2242): update node version required by deps

* feat(PPDSC-2242): revert base card changes

* feat(PPDSC-2242): tidy base card typings

* feat(PPDSC-2242): revert content base changes

* feat(PPDSC-2242): revert content base changes

* feat(PPDSC-2242): fix feature card types and revert styling

* feat(PPDSC-2242): revert line truncation changes

* feat(PPDSC-2242): revert media list changes

* feat(PPDSC-2242): revert usage card changes

* feat(PPDSC-2242): revert section intro changes

* feat(PPDSC-2242): revert table snapshot

* feat(PPDSC-2242): revert tabs with table snapshots

* feat(PPDSC-2242): revert text-section snapshots

* feat(PPDSC-2242): revert pages/components changes

* feat(PPDSC-2242): revert getting-started changes

* feat(PPDSC-2242): revert getting-started/design changes

* feat(PPDSC-2242): revert component page template test change

* feat(PPDSC-2242): revert template snapshot changes

* feat(PPDSC-2242): revert base card changes

- children is not used by base card
- remove from interface and remove unused prop in component

* feat(PPDSC-2242): revert component api snapshots

* feat(PPDSC-2242): add expicit types for test render utils

- testing-library/react-testing-library#587
- prevents 'inferred type of 'renderWithTheme' cannot be named'

* feat(PPDSC-2242): revert swatch card changes

* feat(PPDSC-2242): revert accordion test changes

* feat(PPDSC-2242): remove seek bar explicit any

* feat(PPDSC-2242): remove unknown typing from floating els

* feat(PPDSC-2242): update base switch types

* feat(PPDSC-2242): add undefined error text

* feat(PPDSC-2242): remove grid-layout fragment

* feat(PPDSC-2242): update menu title syntax

* feat(PPDSC-2242): Revert "feat(PPDSC-2242): revert swatch card changes"

This reverts commit 6e85495.

* feat(PPDSC-2242): fix list types

- Support single child and array of children

* feat(PPDSC-2242): update popover types

* feat(PPDSC-2242): revert radio button changes

* feat(PPDSC-2242): fix select typing

* feat(PPDSC-2242): mock downshift ids to make select tests less brittle

* feat(PPDSC-2242): revert stack changes

* feat(PPDSC-2242): fix text-input types

* feat(PPDSC-2242): fix toast types

* feat(PPDSC-2242): revert unordered list changes

* feat(PPDSC-2242): mock ids to make audio player tests less brittle

* feat(PPDSC-2242): fix form input type

* feat(PPDSC-2242): update test utils types

* feat(PPDSC-2242): fix popover test warnings

* feat(PPDSC-2242): use act in audio player tests

* feat(PPDSC-2242): use act in tooltip tests to prevent warnings

* feat(PPDSC-2242): revert cypress version bump

* feat(PPDSC-2242): remove conflicting static dirs config

- -s storybook cli command will be deprected in v7
- need to update package.json scripts before then

* feat(PPDSC-2242): correct menu types

* feat(PPDSC-2242): add private fonts

Co-authored-by: mikef <[email protected]>
Co-authored-by: Stoyan Delev <[email protected]>
Co-authored-by: Stoyan Delev <[email protected]>
Xin00163 pushed a commit to newscorp-ghfb/NewsKit that referenced this issue Oct 17, 2022
* feat(PPDSC-2242): update to node 18

* feat(PPDSC-2242): update types

* feat(PPDSC-2242): remove @types/tar

* feat(PPDSC-2242): update unite tests

* feat(PPDSC-2242): update select and form tests

* feat(PPDSC-2242): update form tests

* feat(PPDSC-2242): update test

* feat(PPDSC-2242): update tests after merge main

* feat(PPDSC-2242): update e2e build for react 18

* feat(PPDSC-2242): fix doc site build for react 18

* feat(PPDSC-2242): update links

* feat(PPDSC-2242): update links

* feat(PPDSC-2242): bump cypress cache version

* feat(PPDSC-2242): update cypress version

* feat(PPDSC-2242): fix hydration using span

* feat(PPDSC-2242): fix hydration using span 2

* feat(PPDSC-2242): fix column-renderer

* feat(PPDSC-2242): fix hydration using span 3

* feat(PPDSC-2242): add wait to accessibility tests

* feat(PPDSC-2242): small changes to tidy up code

* feat(PPDSC-2242): update type

* feat(PPDSC-2242): textBlock to styled TextBlock

* feat(PPDSC-2242): fix story

* feat(PPDSC-2242): update yarn.lock

* feat(PPDSC-2242): add type after merge

* feat(PPDSC-2242): remove unused border card prop

* feat(PPDSC-2242): update node version required by deps

* feat(PPDSC-2242): revert base card changes

* feat(PPDSC-2242): tidy base card typings

* feat(PPDSC-2242): revert content base changes

* feat(PPDSC-2242): revert content base changes

* feat(PPDSC-2242): fix feature card types and revert styling

* feat(PPDSC-2242): revert line truncation changes

* feat(PPDSC-2242): revert media list changes

* feat(PPDSC-2242): revert usage card changes

* feat(PPDSC-2242): revert section intro changes

* feat(PPDSC-2242): revert table snapshot

* feat(PPDSC-2242): revert tabs with table snapshots

* feat(PPDSC-2242): revert text-section snapshots

* feat(PPDSC-2242): revert pages/components changes

* feat(PPDSC-2242): revert getting-started changes

* feat(PPDSC-2242): revert getting-started/design changes

* feat(PPDSC-2242): revert component page template test change

* feat(PPDSC-2242): revert template snapshot changes

* feat(PPDSC-2242): revert base card changes

- children is not used by base card
- remove from interface and remove unused prop in component

* feat(PPDSC-2242): revert component api snapshots

* feat(PPDSC-2242): add expicit types for test render utils

- testing-library/react-testing-library#587
- prevents 'inferred type of 'renderWithTheme' cannot be named'

* feat(PPDSC-2242): revert swatch card changes

* feat(PPDSC-2242): revert accordion test changes

* feat(PPDSC-2242): remove seek bar explicit any

* feat(PPDSC-2242): remove unknown typing from floating els

* feat(PPDSC-2242): update base switch types

* feat(PPDSC-2242): add undefined error text

* feat(PPDSC-2242): remove grid-layout fragment

* feat(PPDSC-2242): update menu title syntax

* feat(PPDSC-2242): Revert "feat(PPDSC-2242): revert swatch card changes"

This reverts commit 6e85495.

* feat(PPDSC-2242): fix list types

- Support single child and array of children

* feat(PPDSC-2242): update popover types

* feat(PPDSC-2242): revert radio button changes

* feat(PPDSC-2242): fix select typing

* feat(PPDSC-2242): mock downshift ids to make select tests less brittle

* feat(PPDSC-2242): revert stack changes

* feat(PPDSC-2242): fix text-input types

* feat(PPDSC-2242): fix toast types

* feat(PPDSC-2242): revert unordered list changes

* feat(PPDSC-2242): mock ids to make audio player tests less brittle

* feat(PPDSC-2242): fix form input type

* feat(PPDSC-2242): update test utils types

* feat(PPDSC-2242): fix popover test warnings

* feat(PPDSC-2242): use act in audio player tests

* feat(PPDSC-2242): use act in tooltip tests to prevent warnings

* feat(PPDSC-2242): revert cypress version bump

* feat(PPDSC-2242): remove conflicting static dirs config

- -s storybook cli command will be deprected in v7
- need to update package.json scripts before then

* feat(PPDSC-2242): correct menu types

* feat(PPDSC-2242): add private fonts

Co-authored-by: mikef <[email protected]>
Co-authored-by: Stoyan Delev <[email protected]>
Co-authored-by: Stoyan Delev <[email protected]>
@Kishokanth-netizen
Copy link

is the file called renderWithTheme? this is what fixed this issue for me when creating a custom renderer for RTL

@solyane2002
Copy link

If someone still has the issue, I fixed it by adding the returned type as follow:

 ReturnType<typeof render>

@hugotox
Copy link

hugotox commented May 7, 2024

If someone still has the issue, I fixed it by adding the returned type as follow:

 ReturnType<typeof render>

Hi @solyane2002

Where did you add that?

@solyane2002
Copy link

Hi,

as follow:

const AllTheProviders = ({ children }: { children: React.ReactNode }) => {
  return (
    <ThemeProviderWrapper mode='light'>
      {children}
    </ThemeProviderWrapper>
  );
};

const customRender = (
  ui: ReactElement,
  options?: Omit<RenderOptions, 'wrapper'>,
): ReturnType<typeof render> => render(ui, { wrapper: AllTheProviders, ...options });

export * from '@testing-library/react';
export { customRender as render };

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

No branches or pull requests

6 participants