Skip to content

TypeError: expect.extend is not a function #420

@cseas

Description

@cseas

I'm trying to add @testing-library/jest-dom to a project that uses both chai and jest. The global expect is aliased to chai.expect and jest's expect is available as jestExpect.

const jestExpect = global.expect;
global.expect = chai.expect;
global.jestExpect = jestExpect;
  • @testing-library/jest-dom version: 5.15.0
  • node version: v12.22.6
  • npm (or yarn) version: yarn 1.22.11

What you did:

The default import doesn't work: import '@testing-library/jest-dom';
Using it emits the error: TypeError: expect.extend is not a function

So I tried importing the matchers separately and adding them to jestExpect manually.

import { toHaveTextContent } from '@testing-library/jest-dom';
jestExpect.extend({ toHaveTextContent });

But the same error still shows up even in the named import.

Suggested solution:

Importing '@testing-library/jest-dom' directly sets up matchers for global expect and that's okay but we should be able to individually import the matchers and assign them to an aliased jest expect as I tried to do above. The only other option to add these custom matchers looks like we'd need to migrate the entire codebase from chai to jest and that'd require a huge effort.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions