-
Notifications
You must be signed in to change notification settings - Fork 413
Description
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-domversion: 5.15.0nodeversion: v12.22.6npm(oryarn) 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.