Testing with Vitest
This guide will help you set up Vitest and React Testing Library for your project. Note that this guide is intended for projects that use Vite as a bundler. If you're using other frameworks/bundlers, we recommend using Jest instead.
Installation
Install Vitest and React Testing Library:
If you want to run tests from your IDE, install one of the extensions.
Configuration
Add Vitest configuration to your Vite config file:
Then create a vitest.setup.mjs file in your project root and add the following code to it:
The code above mocks the window.matchMedia and ResizeObserver APIs that aren't available
in the jsdom environment but are required by some Mantine components.
Optionally, you can add Vitest scripts to your package.json:
Custom render
All Mantine components require MantineProvider to be present in the component tree. To add MantineProvider to the component tree in your tests, create a custom render function:
It's usually more convenient to export all @testing-library/* functions that you're planning to use
from a ./testing-utils/index.ts file:
Then you should import all testing utilities from ./testing-utils instead of @testing-library/react:
Example of a full setup
You can find an example with a full Vitest setup in mantine-vite-template.