Let's work under the assumption that you're not interested in doing this in every single test:
expect.extend(matchers);
We can extend expect
globally, with one caveat: You have to use the globally available version of expect
in your tests.
import { defineConfig } from 'vitest/config';
import configuration from './vite.config';
export default defineConfig({
...configuration,
test: {
globals: true,
setupFiles: './test/setup.ts',
},
});
In the event you don't already have a test setup file, let's create one. You could name this file whatever you. I'm going to name it vitest.setup.ts
because that's my preference.
In vitest.setup.ts
, I can add the following:
import '@testing-library/jest-dom';
We can do something similar for the environment as well.