Important
When I created this package, @testing-library/jest-dom
had been implemented
in such a way that it was difficult to integrate with Vitest without bringing
a lot of cruft from Jest, or foregoing proper types. This is no longer the
case, and @testing-library/jest-dom
appears to support both Jest and
Vitest.
I recommend using that package instead of this one moving forward, as I'm unlikely to maintain this in the future.
This library is a fork of
@testing-library/jest-dom
. It
shares that library's implementation and API. It is intended to make it easier to include
its matchers without clashes between Vitest and Jest's environment or types.
See the README
for the original package for usage details.
This module should be installed as one of your project's devDependencies
:
# with npm
npm install --save-dev vitest-dom
# yarn
yarn add --dev vitest-dom
# pnpm
pnpm add --dev vitest-dom
Import the matchers from vitest-dom/matchers
once (perferably in your tests
setup file), then pass them to Vitest's expect.extend
method:
// vitest-setup.js
import * as matchers from "vitest-dom/matchers";
import { expect } from "vitest";
expect.extend(matchers);
// or:
import "vitest-dom/extend-expect";
// In vitest.config.js, add the following
export default defineConfig({
test: {
setupFiles: ["vitest-setup.js"],
},
});
If you're using TypeScript, make sure your setup file has a .ts
extension to
include the necessary types.
If you import from vitest-dom/extend-expect
to run expect.extend
for you,
you will get TypeScript support automatically.
// vitest-setup.ts
import "vitest-dom/extend-expect";
If you want to run extend.expect
yourself, you will need to include the type defintions either with a /// <reference />
directive or including the type in your compilerOptions
:
- In your test file via a reference directive:
/// <reference types="vitest-dom/extend-expect" />
- In your
tsconfig.json
via thetypes
compiler option:{ "compilerOptions": { "types": ["vitest-dom/extend-expect"] } }