Testing a React-Vite App
Introduction
Before proceeding, make sure you have already understood the basics of testing in React. See the article on Testing a normal React App for the basics. This article covers only the setup for testing a React-Vite app.
Bootstrap a React-vite app by running the following command:
1 npx create-vite@latest GIVEN-PROJECT-NAMEBe sure to replace
GIVEN-PROJECT-NAMEwith the name of your project. Choosereactas the template when prompted. ChooseJavaScriptas the language when prompted.
Setting up a React-Vite app for testing
To set up a React-Vite app for testing, you need to install the following packages:
@testing-library/react- This package provides utilities for testing React components.vitest- This package provides utilities for testing Vite apps.jsdom- This package provides a JavaScript implementation of the DOM.
Run the following command to install the packages:
1
npm install --save-dev @testing-library/react vitest jsdom
- add the following to the
vite.config.jsfile:
1
2
3
4
5
6
7
8
9
10
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
test: {
testEnvironment: "jsdom",
testMatch: ["**/__tests__/**/*.test.js"],
},
});
This post is licensed under CC BY 4.0 by the author.