Skip to content

Latest commit

 

History

History
36 lines (24 loc) · 958 Bytes

Globally Extending Matchers in the Test Setup for Component Testing.md

File metadata and controls

36 lines (24 loc) · 958 Bytes

Let's work under the assumption that you're not interested in doing this in every single test:

expect.extend(matchers);

Making it, expect, and Friends Globally Available

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',
  },
});

Creating a Test Setup File

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.