Testing with Jest
This guide will help you set up Jest and React Testing Library for your project. Note that this guide only covers shared logic that can be applied to any framework, and it doesn't cover the initial setup of Jest and React Testing Library as it may vary depending on the framework you're using.
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:
Mock Web APIs
Most Mantine components depend on browser APIs like window.matchMedia or ResizeObserver.
These APIs aren't available in the jest-environment-jsdom environment and you'll need to mock them in your tests.
Create a jest.setup.js file in your project root and add the following code to it:
Then add it as a setup file in your jest.config.js:
Framework specific setup
Jest setup for different frameworks may vary and usually changes over time. To learn how to set up Jest for your framework, either check the Jest and React Testing Library documentation or check one of the premade templates. Most of the templates include Jest setup, and you can use them as a reference.
Testing examples
You can find testing examples in Mantine Help Center: