Skip to content

TypeScript types for the "./matchers" export don't match implementations #554

@jakeboone02

Description

@jakeboone02
  • @testing-library/jest-dom version: 6.1.5
  • node version: N/A - using Bun 1.0.15
  • jest (or vitest) version: N/A - using bun test
  • npm (or yarn) version: N/A - using Bun 1.0.15
  • @testing-library/react version: 14.1.2

Relevant code or config:

This code works in practice, it's just the types that are causing a problem:

import * as matchers from "@testing-library/jest-dom/matchers";
import { expect, mock, test } from "bun:test";

expect.extend(matchers);
//            ^ Argument of type 'TestingLibraryMatchers<any, void> & Record<string, any>' is not assignable to parameter of type 'ExpectExtendMatchers...

What you did:

Tried to extend expect from bun:test with matchers from @testing-library/jest-dom/matchers.

I don't think this is specific to Bun, but they just added support for expect.extend so I thought I'd give it go.

What happened:

The TypeScript definitions for the "./matchers" export don't seem to match the implementations of the actual matchers.

Take toBeChecked, for example. The implementation is here:

export function toBeChecked(element) {

But the type definition doesn't include the element parameter:

toBeChecked(): R

Also, the R generic is specified as void in the export statement, so the return type is effectively missing as well. (Should be something like { pass: boolean; message: () => string; }.)

declare const matchers: matchers.TestingLibraryMatchers<any, void> &
Record<string, any>
export = matchers

Reproduction:

See "Relevant code" section above.

Problem description:

Types should match the implementation.

Suggested solution:

https://github.com/testing-library/jest-dom/blob/main/types/matchers.d.ts should be updated to match the implementation of the matcher functions. If those types are correct in some other context (since they do seem to match the implementation when called as a property of an expect() result), then a separate definition file should be created for the "./matchers" export.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions