0

I have a question about extending Typescript interfaces. Here is my situation:

I am using expect in my tests without Jest (I installed it separately and it works).

Now I want to use jest-dom to add the expect matchers for DOM. (they are super helpful and will make my tests easier to write).

I did the following to extend expect

import expect from 'expect'
import * as matchers from '@testing-library/jest-dom/matchers'

expect.extend(matchers)

This works and adds the matchers, but Typescript doesn't know about them.

I installed @types/testing-library/jest-dom But it didn't solve the issue.

Looking inside @types/testing-library__jest-dom I see that it extends the types of jest and not the standalone expect.

I tried adding a expect.d.ts with the following content:

import 'expect'

declare module 'expect' {
  export interface Matchers<R> {
    toBeInTheDOM(container?: HTMLElement | SVGElement): R
    // ... other methods
  }
}

But Typescript is still complaining.

Any ideas?

1
  • what is your package json? I tried doing that and I get: Module not found: Error: Can't resolve 'module' in '/......../node_modules/stack-utils' and also picomatch/lib missing Commented Nov 16, 2022 at 0:01

1 Answer 1

0

I found the solution, the following works

import 'expect/build/types'

declare module 'expect/build/types' {
  export interface Matchers<R> {
    // matchers methods here
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.