DEV Community

Cover image for 🚀 Configure Vitest with React Testing Library 🚀
Thiago Pacheco
Thiago Pacheco

Posted on

🚀 Configure Vitest with React Testing Library 🚀

It is 2023 and we have so many options to create a react app that it can be hard to choose between tools, but most people are choosing Vite because of its simplicity and speed. If you are one of those people and are looking for an easy way to setup your tests, you came to the right place!

Let's go right to the point with setting up a new application:

Create a new React app with Vite:

yarn create vite
Enter fullscreen mode Exit fullscreen mode

Install the dependencies

yarn
Enter fullscreen mode Exit fullscreen mode

Install the necessary dependencies

yarn add -D vitest jsdom @testing-library/react @testing-library/jest-dom
Enter fullscreen mode Exit fullscreen mode

Add a test script

In your package.json file, add the following line under the scripts attribute:

"scripts": {
+  "test": "vitest"
}
Enter fullscreen mode Exit fullscreen mode

Create a setup tests file

Create a new file under tests/setup.ts with the following content:

import { expect, afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
import matchers from '@testing-library/jest-dom/matchers';

expect.extend(matchers);

afterEach(() => {
  cleanup();
});
Enter fullscreen mode Exit fullscreen mode

Configure vite to use this setup

Edit the file vite.config.js in the root folder

  plugins: [react()],
+  test: {
+    environment: 'jsdom',
+    setupFiles: ['./tests/setup.ts'],
+    testMatch: ['./tests/**/*.test.tsx'],
+    globals: true
+  }
Enter fullscreen mode Exit fullscreen mode

That is all the configuration needed.
Now, let's go ahead and create a quick test to try this out

Your first test

Create a test file at tests/App.test.tsx

touch tests/App.test.tsx
Enter fullscreen mode Exit fullscreen mode

Add the following content

import { render, screen } from '@testing-library/react';
import App from "../src/App";


describe('App', () => {
  it('renders headline', () => {
    render(<App />);
    const headline = screen.getByText(/It works and you found me!/i);
    expect(headline).toBeInTheDocument();
  });
});
Enter fullscreen mode Exit fullscreen mode

Run the tests (and expect to fail)

yarn test
Enter fullscreen mode Exit fullscreen mode

Fix the test

Replace the html content of the App.tsx file with the following:

+  return (
+    <div>
+      <h1>It works and you found me!</h1>
+    </div>
+  )
Enter fullscreen mode Exit fullscreen mode

Run the tests

Try running again the tests, it should be working fine now!

it works

Top comments (12)

Collapse
 
alc profile image
Aaron Cooper

Great article, thanks, but it has an error in the matchers import, at least at the present time in my project (perhaps the API has changed).

You must now use * as matchers in the import inside setup.ts, like this:

import * as matchers from "@testing-library/jest-dom/matchers";
Enter fullscreen mode Exit fullscreen mode

After which it'll work. Cheers!

Collapse
 
pacheco profile image
Thiago Pacheco

Thanks a lot, I'll update the post to have the correct import.

Collapse
 
aaronyamil profile image
Aaron Luna

Thanks a lot!!!

Collapse
 
musman0741 profile image
Muhammad Usman

Great, thanks Thiago

Collapse
 
lighteningcode profile image
LighteningCode

Thanks so much, very simple and straight forward, this helped me :)

Collapse
 
ardiwsaputra profile image
ardiwsaputra • Edited

Hello, Thanks for the sharing. When i follow the step and run npm run test. I got some warning like bellow, but the test passed. Any solutions?

TypeError: Failed to parse URL from /eslint.json
at Object.fetch (node:internal/deps/undici/undici:11576:11) {
[cause]: TypeError: Invalid URL

Image description

Collapse
 
irshsheik profile image
irshad sheikh

should the config be vitest.config.ts?
i was not able to configure in vite.config.ts

Collapse
 
walicar profile image
Will Alicar • Edited

Was getting this error: Property 'toBeInTheDocument' does not exist on type 'JestMatchers'.ts(2339)

Fixed by adding @testing-library/jest-dom to setup.ts

Collapse
 
alc profile image
Aaron Cooper

This didn't work for me, however adding the following inside src/vite-env.d.ts did the trick to include the ambient types on the expect interface:

/// <reference types="@testing-library/jest-dom" />
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tarkeasy profile image
Taras Kulchytskyi

If you have an issue with matchers, your envirement can't see it, just install it.
yarn add -D @types/testing-library__jest-dom

Collapse
 
blessdarah profile image
Bless Darah Gah

Simple and straight to the point article. I used a vitest.config.ts file for me though.

Collapse
 
fazle-rabbi-dev profile image
Fazle Rabbi

Thanks for sharing this