Testing with Vitest
This guide will help you setup Vitest and React Testing Library for your project. Note that this guide intended for projects that use Vite as a bundler, if you are using other frameworks/bundlers, it is recommended to use 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 vitest.setup.mjs
file in your project root and add the following code to it:
The code above mocks window.matchMedia
and ResizeObserver
APIs that are not available
in 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 is usually more convenient to export all @testing-library/*
functions that you are planning to use
from ./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.