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

Make it so the TypeScript definitions work automatically without config #123

Closed
kentcdodds opened this issue Aug 15, 2019 · 123 comments · Fixed by #189
Closed

Make it so the TypeScript definitions work automatically without config #123

kentcdodds opened this issue Aug 15, 2019 · 123 comments · Fixed by #189
Labels
help wanted Extra attention is needed released

Comments

@kentcdodds
Copy link
Member

If we follow the path we took in React Testing Library then that'll mean that people wont have to do anything fancy to make the typings/autocomplete show up which would be rad.

Specifically:

  1. Add testing-library__jest-dom to DefinitelyTyped
  2. Add @types/testing-library__jest-dom to @testing-library/jest-dom dependencies

I just tested it locally and that appears to work. If I have it in my node_modules then it just works™️

Anyone up for this?

@kentcdodds kentcdodds added the help wanted Extra attention is needed label Aug 15, 2019
@gnapse
Copy link
Member

gnapse commented Aug 15, 2019

Wouldn't this make it possible that the type definitions lag behind the latest features in the library? I understand that part of the motivation in doing this in RTL and DTL was this:

There are no core teammembers with the TS knowledge needed to maintain the types, and they are not well tested or integrated with the codebase.

I'm no expert in TS, but I feel qualified enough to maintain the small needs of jest-dom (which are simpler than the TS needs of RTL/DTL). It's mainly about making sure that PRs that introduce new features or change the external interface make the appropriate updates.

I'll also check if #45 is indeed still an issue. If that's so, then I'd understand the need for this more. Will post my findings here.

@gnapse
Copy link
Member

gnapse commented Aug 15, 2019

My main gripe with DefinitelyTyped is that the type definitions are usually lagging behind and users are unable to use new features until the type definitions get up to speed with the latest developments in the repo.

Also, last time I checked DefinitelyTyped was this huge repo with all the type definitions of everything under the sun, all in the same place. It's hard to search for existing issues of specific libraries, since it's all under the same list of issues. I appreciate its existence because most libs don't provide typings, understandably, but I always thought (and my experience as a consumer of libraries has confirmed) that if the library itself provides the type definitions (e.g. Formik) the experience is so much better. I've been off using TS actively for a few months now, so not sure if any of the reasons for these perceptions have changed. I'd love to know others' input on all this, and if any of this was taken into consideration when making the decision to go in this direction in RTL/DTL.

@jgoz
Copy link
Collaborator

jgoz commented Aug 15, 2019

DefinitelyTyped does have a slight advantage when it comes to global typings, since they will be implicitly included for normal TS projects (those that don’t specify a types tsconfig option).

But as a code/typings contributor to a number of projects, I have to agree with @gnapse about the downsides of projects with typings on DT. With the exception of very popular projects like React, the type definitions are often slightly out of date with the actual library and keeping them in sync involves more overhead than projects with typings in the repo (and more still than projects written in TypeScript).

That said, the DT review/merge process is highly automated and their notification bots are very good, so it’s quite streamlined if you are willing to put in the effort of cloning a second (giant) repo and following the instructions.

If the main concern was ease of use, a theoretical option would be to keep the type definitions inside this repo but also have a DT entry that simply does import “@testing-library/jest-dom/extend-expect”; in the global scope. I’m not sure if that’s a common/recommended practice on DT, but it would make the setup instructions for this repo consistent with other projects and keep the actual typings co-located with the code.

@kentcdodds
Copy link
Member Author

Feel free to continue to maintain them yourself if you feel so inclined. I would love it if the types could somehow get added automatically though.

@kentcdodds kentcdodds changed the title Move TypeScript definitions to DefinitelyTyped Make it so the TypeScript definitions work automatically without config Aug 16, 2019
@gnapse
Copy link
Member

gnapse commented Aug 16, 2019

I just did the following:

npx create-react-app ts-jest-dom-example --typescript
cd ts-jest-dom-example
yarn add --dev @testing-library/react @testing-library/jest-dom

then I changed the src/App.test.tsx file to have this content instead of the default generated by CRA:

import React from 'react';
import { render } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import App from './App';

it('renders the expected content', () => {
  const { container } = render(<App />);
  expect(container).toHaveTextContent("Learn React");
});

Ran the tests with yarn test and it works. I added the assertion first and vscode's TS warnings immediately complained about toHaveTextContent not being recognized. But as soon as I typed the import line it removed the alert underline.

Short video of how this vscode behavior happens

Granted, I could not make it work by centrally importing jest-dom in a single location (e.g. src/setupTests.ts) instead of having to import it in every single test file that uses it. Maybe that's what you'd like? Because if the above is enough, I'm not sure I'm getting what's the issue.

@jgoz would you happen to know if we can make the centralized import work? I'll try and dig into this tomorrow but now I need some sleep 😅

@kentcdodds
Copy link
Member Author

kentcdodds commented Aug 16, 2019

I could not make it work by centrally importing jest-dom in a single location (e.g. src/setupTests.ts) instead of having to import it in every single test file that uses it. Maybe that's what you'd like?

Precisely. And having it installed via @types/testing-library__jest-dom will make that work. So maybe all we'd need is:

  1. A contrib to DefinitelyTyped which simply does import '@testing-library/jest-dom/extend-expect'
  2. Add that as a dependency to this project so it gets auto-installed for folks.

I believe that should do it.

@gnapse
Copy link
Member

gnapse commented Aug 16, 2019

Nice! If that works that'd fix #45, your concerns, and my concerns about divergence of types vs lib.

Although I'll dig a bit more. jest-enzyme do not seem to be on DefinitelyTyped, they have the types in the same repo as we do, and their instructions to make the matchers work in TS are basically what we want to support (import once in setupTests.ts). I'll check if that indeed works in a local repo, and if it does, I'll dig into what's the difference.

@kentcdodds
Copy link
Member Author

I believe that theirs works because the setupTests.ts is a TypeScript file. But if it's a .js file it will not work properly. I could be wrong, but I feel like I read a comment in this repo that said that. Yep, this one.

@gnapse
Copy link
Member

gnapse commented Aug 16, 2019

Was your example of "it just works when I tried locally" working with the setupTest.js file being JS, importing jest-dom only in this setup file, and then using the custom matchers in TS test files?

Also, an update: it works for me having the centrally import in setupTest.ts. It's just vscode that does not recognizes it 🤔

Also, I do not think it's unreasonable to require, if you're already using TS, that your jest setupTest file be a TS file.

@kentcdodds
Copy link
Member Author

All of my code is regular JS. No TS. And my local test was working great with that when I created a node_modules/@types/testing-library__jest-dom.d.ts which simply did: import '@testing-library/jest-dom/extend-expect'

I do not think it's unreasonable to require, if you're already using TS, that your jest setupTest file be a TS file.

I agree with you. I'm talking about the experience for people not using TypeScript, but using an editor which supports TypeScript autocomplete (like VSCode).

@weyert
Copy link

weyert commented Aug 16, 2019

Do you want to make extract it? Shall I make a pull request?

@jgoz
Copy link
Collaborator

jgoz commented Aug 16, 2019

would you happen to know if we can make the centralized import work?

AFAIK, there’s no way to expose a global type definition directly from an npm module, but it should work with the DT method described above. I won’t be able to test myself for a couple of days though.

@gnapse
Copy link
Member

gnapse commented Aug 16, 2019

Ok, I agree then to go with what Kent described above.

Regarding this concern from @jgoz

I’m not sure if that’s a common/recommended practice on DT

Indeed, their instructions for creating a new package pretty much assume you're going to add something of substance, not merely a redirector back to your own package. Let's hope that's ok anyway.

@weyert
Copy link

weyert commented Aug 16, 2019

I have added a draft pull request for now: DefinitelyTyped/DefinitelyTyped#37688

@weyert
Copy link

weyert commented Aug 18, 2019

I think the PR at DefinetlyTyped is good to go

@weyert
Copy link

weyert commented Aug 19, 2019

-Merged. Guess, we can now update this package to refer the type definitions :)-
Looks like MSFT has closed the pull request, what shall we do?

@johnnyreilly
Copy link

johnnyreilly commented Aug 20, 2019

I've reopened it and started a discussion. Let's see where it goes.

Just to summarize my understanding: you'd like to remove friction for people acquiring type definitions. @sheetalkamat closed the Definitely Typed PR as this package already has type definitions locally which could be exposed like so:

This change is not needed. The package can add "types" field to the package and thats the desired way of adding types. Refer to https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/declaration%20files/Publishing.md

This is an entirely valid solution.

The alternative is doing what the React Testing Library did. i.e. dropping local definitions in favour of them being managed in DefinitelyTyped. That's what the linked PR to Definitely Typed by @weyert is about. If that was merged then a PR would follow for @testing-library/jest-dom which dropped the local type definitions and added a dependency for the generated @types/testing-library__jest-dom package.

More details in the suggestion from @kentcdodds here: #123 (comment)

To be entirely clear, my understanding is that the desire is to move to the latter solution (using DT) rather than the former. Is that correct? Or is exposing "types" via package.json being actively considered as well?

@weyert
Copy link

weyert commented Aug 20, 2019

Yes, that's the idea to do it for all the @testing-library brother and sister packages. I thought it would be better to sort out the type definitions in TD first before making a PR here

@gnapse
Copy link
Member

gnapse commented Aug 20, 2019

I thought we were going to first attempt what @kentcdodds described above:

Precisely. And having it installed via @types/testing-library__jest-dom will make that work. So maybe all we'd need is:

  1. A contrib to DefinitelyTyped which simply does import '@testing-library/jest-dom/extend-expect'
  2. Add that as a dependency to this project so it gets auto-installed for folks.
    I believe that should do it.

Now, I realize this may not be the kind of type definition packages expected and accepted in DT. If that's the case, and the only option is to entirely move the type definitions to DT and remove them from here, I already expressed my concerns about that above.

However, if completely moving the types to DT removes the friction in using types even for non-TS users, then so be it. But I'm a bit disappointed that no other solution exists. Does it not?

@johnnyreilly would the solution to keep types in this package, and adding a "types" entry to package.json solve @kentcdodds' concerns expressed here

I'm talking about the experience for people not using TypeScript, but using an editor which supports TypeScript autocomplete (like VSCode).

@jgoz
Copy link
Collaborator

jgoz commented Aug 20, 2019

The fundamental reason these types need to be published on DT in some form is because they are global types that extend Jest's global types.

I.e., most people are probably not doing import {toHaveTextContent} from "@testing-library/jest-dom", they are probably just using expect(foo).toHaveTextContent(...) after configuring Jest.

As far as I know, it is not possible to publish global types from an npm package and have them "just work" when someone installs that package. This is because TypeScript's default type root is @types, so it will only ever look in node_modules/@types (and search up the directory hierarchy for the same) for types that will be automatically included.

So global typings like ours must be published to DT if we don't want to force the user would to do some kind of configuration (like they do now). But we also wanted to be able to keep maintenance of those types inside this repository so there is less chance that they go out of sync with the actual functionality, hence the PR that technically breaks the DT rules.

The flipside of having types on DT, whether directly or as a redirect, is that it might be confusing for new users who don't realize they need to add import "@testing-library/jest-dom/extend-expect" to a Jest configuration file. They could add the dependency and then toHaveTextContent magically appears in their editor, but running the tests would fail.

All of this could be avoided if Jest allowed the setupFilesAfterEnv to be written in TypeScript. That way, the setup file could be added to a user's tsconfig.json and we wouldn't need to publish the types to DT, since they would be explicitly imported as a one-time global import. But since that isn't possible, we have this set of tradeoffs to weigh.

@kentcdodds
Copy link
Member Author

To be clear, here's the tradeoff, we either:

A. Require JS folks to install the types to get autocomplete (most wont).
B. Make it automatic without config, but risk people getting autocomplete suggestions when jest-dom has not been configured properly.

When talking about risk, it's always good to consider how the likelihood and the impact. I would say the impact is a moderate-to-low impact of confusion only. It's pretty unlikely that this confusion would lead to a broken build/inability to deploy an app for example. Confusion as an impact is not great, so it'd be nice to avoid.

However, the likelihood is very low I would say. When someone installs @testing-library/jest-dom they'll probably be reading the instructions for setting things up and they'll add the configuration at that point.

So I'd say the risk is moderate-to-low impact and low risk. So the cost is low, and the benefit is high, IMO.

@johnnyreilly
Copy link

johnnyreilly commented Aug 20, 2019

@johnnyreilly would the solution to keep types in this package, and adding a "types" entry to package.json solve @kentcdodds' concerns expressed here

I believe so yes. Either solution works AFAIK; both give the same good developer experience. It's just a choice around which way you'd like to go.

@jgoz
Copy link
Collaborator

jgoz commented Aug 20, 2019

@johnnyreilly I don't think the "types" entry on its own would allow things to "just work", since they are global types, so DT is really the only option for automatic inclusion.

@johnnyreilly
Copy link

I haven't tried it but I have a feeling that either solution will work. I think that this being here:

declare namespace jest {

will extend the jest namespace which is already pulled in globally. I'm not 💯% on this; but it's easy to test out. I recommend just adding the "types" entry and seeing if it works. If it doesn't then your decision is made 😄

@jgoz
Copy link
Collaborator

jgoz commented Aug 20, 2019

I'm not 💯% on this; but it's easy to test out. I recommend just adding the "types" entry and seeing if it works. If it doesn't then your decision is made 😄

I should have mentioned that I tried this out last night and it only worked if you added import "@testing-library/jest-dom/extend-expect" to a TS file that is included for compile.

@kentcdodds
Copy link
Member Author

To be clear, the suggestion is that if we add a "types" entry to the package.json of this project that'll make it work automatically as well? I tried that locally just now and it didn't seem to make any difference. I could have done it incorrectly though.

@johnnyreilly
Copy link

ah well then. Decision made!

@zhex900
Copy link

zhex900 commented Jun 29, 2022

Hi,
I am still having the same problem.

any
Property 'toBeInTheDocument' does not exist on type 'Matchers<void> & SnapshotMatchers<void, HTMLElement> & Inverse<JestMatchers<void, HTMLElement>> & PromiseMatchers<...>'.ts(2339
// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": "./src",
    "paths": {
      "@/*": ["*"]
    },
    "incremental": true,
    "types": ["node", "jest", "@testing-library/jest-dom"]
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules", "infrastructure"],
  "moduleResolution": ["node_modules", ".next", "node"]
}

    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^14.2.1",
    "@types/jest": "^28.1.3",
  "@types/testing-library__jest-dom": "^5.14.5",
    "jest": "^28.1.2",
    "jest-environment-jsdom": "^28.1.2",

@thisismydesign
Copy link

@zhex900 I run into this issue when importing types from @jest/globals es explained in the docs. If I don't import the types everything works out of the box. 👍

@hongkiulam
Copy link

For pnpm users: since the DefinitelyTyped definitions are marked as a dependency by @testing-library/jest-dom it downloads them into node_modules/.pnpm/@types+testing-library__jest-dom which isn't picked up by the typescript server. This works for npm since the dependency is correctly downloaded into @types.

To resolve, I just added @types/testing-library__jest-dom as a dependency to my own project so that pnpm downloads it to @types

@piotr-czerwik
Copy link

piotr-czerwik commented Dec 2, 2022

An alternative approach for pnpm could be to add the following entry into the project's .npmrc file:

public-hoist-pattern[]=@types/*

@unitydynamics
Copy link

I think adding to tsconfig types as zhex900 showed could be a preferable solution than dealing with setup scripts...

venables added a commit to startkit-dev/next that referenced this issue Mar 24, 2023
pnpm installs these types to node_modules/.pnpm by default, so we need
to explicitly add this dependency.

See testing-library/jest-dom#123 (comment)
@garrett-thompson
Copy link

garrett-thompson commented Apr 25, 2023

What can I do if my repo also has Chai as dependency, and the global expect object is using the Chai types? It seems import '@testing-library/jest-dom'; only affects the global expect type so I can't directly import { expect } from '@jest/globals'; either.

@Bram-Zijp
Copy link

What can I do if my repo also has Chai as dependency, and the global expect object is using the Chai types? It seems import '@testing-library/jest-dom'; only affects the global expect type so I can't directly import { expect } from '@jest/globals'; either.

Import { expect as jestExpect } from "jest"

@garrett-thompson
Copy link

That doesn't include the extended jest-dom matchers on the interface.

@Bram-Zijp
Copy link

At a party atm but you can globalise a constant jestExpect in the setup step and figure out how extended expect type defs overwrite the global expect + jest import by looking at their source code type defs.

@karlhorky
Copy link

Seems like this is still broken when using @jest/globals with the default create-react-app test:

import '@testing-library/jest-dom';
import { expect, test } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
  render(<App />);
  const linkElement = screen.getByText(/learn react/i);
  expect(linkElement).toBeInTheDocument(); // 💥 Property 'toBeInTheDocument' does not exist on type
                                           // 'Matchers<void, HTMLElement> & SnapshotMatchers<void,
                                           // HTMLElement> & Inverse<JestMatchers<void, HTMLElement>> &
                                           // PromiseMatchers<...>'
});

I've been trying to find alternatives, including interesting overrides of @jest/globals like @johncrim posted in the comment below, but have been unsuccessful:

Another option was the comment by @mrazauskas here:

But neither seem to offer a way to use @testing-library/jest-dom with @jest/globals while using TypeScript 🤔

@mrazauskas
Copy link

@types/testing-library__jest-dom is extending Matchers interface of the jest namespace which works for @types/jest: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1ea40b1e5475bb7b6d7c3e85ea38d57ca18098d2/types/testing-library__jest-dom/index.d.ts#L14-L18

I guess provide the same functional for users who import { expect } from '@jest/globals', next to that declarations one should extend Matchers interface from the 'expect' module (reference):

declare module 'expect' {
  interface AsymmetricMatchers {
    // ...
  }
  interface Matchers<R> {
    // ...
  }
}

The build-in jest typings do not know (and will not know) anything about @types/jest. Hence libraries interested to provide typings for Jest users should consider the build-in typings as source of truth.

@karlhorky
Copy link

karlhorky commented May 5, 2023

Interesting, I did already try that and it didn't work before.

But now in trying to create a reproduction for it, I cannot make it fail in a TypeScript Playground (see the working playground below) 😄 I'll try to reproduce in my app.

TypeScript Playground (working)

import { expect, test } from '@jest/globals';

declare module 'expect' {
  interface AsymmetricMatchers {
  }
  interface Matchers<R> {
     /**
     * @description
     * Assert whether an element is present in the document or not.
     * @example
     * <svg data-testid="svg-element"></svg>
     *
     * expect(queryByTestId('svg-element')).toBeInTheDocument()
     * expect(queryByTestId('does-not-exist')).not.toBeInTheDocument()
     * @see
     * [testing-library/jest-dom#tobeinthedocument](https://github.com/testing-library/jest-dom#tobeinthedocument)
     */
    toBeInTheDocument(): R;
  }
}

test('renders learn react link', () => {
  expect(1).toBe(1);
  expect('').toBeInTheDocument(); // ✅ Property 'toBeInTheDocument' from Matchers interface above
});

@karlhorky
Copy link

karlhorky commented May 5, 2023

Ok, so I got it to break again across file boundaries: in the same file it was working as above, but as soon as I moved my declare module 'expect' {} block to another file expect.d.ts, it stopped working. Through some trial and error, I found two problems and workarounds for both:

Property 'toBeInTheDocument' does not exist on type 'Matchers<void, string> ... error

First of all, the property was completely unrecognized with the same error message Property 'toBeInTheDocument' does not exist on type 'Matchers<void, string> ... as I was experiencing before. I eventually found out that this was due to how my package manager pnpm handles package hoisting - expect is not directly in node_modules and TypeScript has a problem with that.

Workarounds

  • Install expect in the project to make sure that it is recognized and the module can be overridden
  • Configure hoisting in pnpm to always hoist expect

expect().toBeInTheDocument() type set to any in other files

Once I resolved that problem, a different error occurred - the toBeInTheDocument property was receiving an any type 😬 Definitely not what is wanted here.

Workaround

The way that I've found to deal with this (at least for now) is to add an import '@jest/globals'; at the top of the expect.d.ts file - this somehow causes TypeScript to correctly apply the type inside of the Matchers interface inside of the declare module 'expect'.


The final expect.d.ts file (only for a single property toBeInTheDocument):

import '@jest/globals';

declare module 'expect' {
  interface AsymmetricMatchers {}
  interface Matchers<R> {
    /**
     * @description
     * Assert whether an element is present in the document or not.
     * @example
     * <svg data-testid="svg-element"></svg>
     *
     * expect(queryByTestId('svg-element')).toBeInTheDocument()
     * expect(queryByTestId('does-not-exist')).not.toBeInTheDocument()
     * @see
     * [testing-library/jest-dom#tobeinthedocument](https://github.com/testing-library/jest-dom#tobeinthedocument)
     */
    toBeInTheDocument(): R;
  }
}

@karlhorky
Copy link

karlhorky commented May 5, 2023

Workaround (using @jest/globals and pnpm)

From that point above, to get all of the matchers in the TestingLibraryMatchers interface exported by @types/testing-library__jest-dom is not super tough:

import { expect } from '@jest/globals';
import { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';

declare module 'expect' {
  interface Matchers<R = void>
    extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
}

For pnpm, just add expect to your devDependencies to be able to override the module in TypeScript:

{
  "devDependencies": {
+   "expect": "^29.5.0"
  }
}

And from there, you have working types while using imported globals in pnpm:

Screenshot 2023-05-05 at 23 59 59

Commit on GitHub here: upleveled/preflight-test-project-react-passing@0c6a178

@karlhorky
Copy link

Thanks @mrazauskas for the hint and the push to try out that option again!! 🙌🚀

@mrazauskas
Copy link

Thanks for the details. Somehow I missed that pnpm is involved here. Hm.. It looks somewhat clumsy to require to install 'expect'. The same Matchers interface is exposed from '@jest/expect', so I thought how else could it be expose to help pnpm users?

Perhaps '@jest/globals'? You are installing it already for pnpm, right? It declares jest namespace similar to @types/jest: https://github.com/jestjs/jest/blob/bc26cd79e60b7bf29d854293f0f011936fda1a5a/packages/jest-globals/src/index.ts#L46

If Matchers interface would be added to jest namespace in '@jest/globals', does it mean that '@types/testing-library__jest-dom' would magically do its job?

Might work, but it might be that /// <reference types="jest" /> can be a problem. Hm.. '@types/jest' is included in dependencies list, so it should work. I will try to play with the idea.

Just to say that I don’t use pnpm at all, so I might miss some detail. @karlhorky could you try patching two lines in index.d.ts of '@jest/globals' (if that is possible with pnpm):

- import type { JestExpect } from '@jest/expect';
+ import type { JestExpect, Matchers as JestMatchers } from '@jest/expect';
  declare namespace jest {
+   export type Matchers<R extends void | Promise<void>, T> = JestMatchers<R, T>;

@mrazauskas
Copy link

mrazauskas commented May 6, 2023

It was interesting idea, but it does not work. No luck with that.


In the other hand, if I have import { expect } from '@jest/globals' adding the following to index.d.ts of '@types/testing-library__jest-dom' does the job:

declare module 'expect' {
  interface Matchers<R = void> extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
}

EDIT If pnpm still requires to install 'expect' to make the above code work, I think it is fine adding export { Matchers } from '@jest/expect' to '@jest/globals'. To make this possible:

declare module '@jest/globals' {
  interface Matchers<R = void> extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
}

@karlhorky
Copy link

karlhorky commented May 7, 2023

Nice, cool, so if I understand correctly, there are two ways that this could be resolved for the combination of @jest/globals and pnpm:

  1. Modifying + publishing a new release of @types/testing-library__jest-dom to override the expect module - should work out of the box for @jest/globals + pnpm users with no code changes
  2. Modifying + publishing a new release of @jest/globals to also export Matchers from expect - still needs a type file for @jest/globals + pnpm users, overriding the '@jest/globals' library

Did I understand that correctly? Would be of course great to have it work out of the box and not have to fix this from userland...!

@garrett-thompson
Copy link

Thanks for the detailed posts @karlhorky! The snippet you shared (posted again below) worked for me using npm.

import { expect } from '@jest/globals';
import { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';

declare module 'expect' {
  interface Matchers<R = void>
    extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
}

karlhorky added a commit to upleveled/preflight that referenced this issue May 11, 2023
qrg added a commit to qrg/playground-nextjs that referenced this issue Jun 16, 2023
gruvector pushed a commit to gruvector/preflight that referenced this issue Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed released
Projects
None yet
Development

Successfully merging a pull request may close this issue.