Lädt...


🔧 Configure Vitest, MSW and Playwright in a React project with Vite and TS


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Hi, web traveler! If you're here, it's probably because you understand the importance of good testing—or maybe you're just tired of the QA team pointing out issues. Either way, you're in the right place. This guide will help you configure and integrate tools that cover all the angles of testing your app.

Just a quick reminder of what this tools are:

  • Vitest: For unit testing. If you've used Jest, this tool is quite similar but powered by the Vite engine.
  • MSW(Mock Service Worker): A tool that intercepts API calls and changes their response, so you can test different scenarios in your app without relying on real API endpoints.
  • Playwirght: For end-to-end testing.

For this guide, we'll configure a simple React app with Vite. The app will query the public API of the Art Institute of Chicago, display the items in cards, and, when a user clicks on a card, navigate to a detailed view. If you're curious about the API, check it out here.

You can clone the basic code to start this guide in this repo. Here is a snapshot:

snapshot of the app

Vitest

We'll start by configuring Vitest. If you’ve set up your app with Vite, you’ll probably want to use Vitest since it has a similar API to Jest but is optimized for the Vite environment. Let’s go step-by-step through the configuration.

1. Install packages

First, install these packages as dev dependencies:

npm install -D @testing-library/react vitest jsdom @types/testing-library__jest-dom @testing-library/user-event

Here is what you have installed:

  • @testing-library/react: classic library build on top of the DOM Testing LIbrary to work with React components --> it lets you render and query React Components
  • vitest: test runner --> it runs your tests
  • jsdom: acts as a lightweight browser, creating an environment that simulates the DOM so you can test DOM-related behavior
  • @types/testing-library__jest-dom: adds typing
  • @testing-library/user-event: is partner of @testing-library/react: and adds simulation of user interactions with browser

2. Configure Vite to handle tests with Vitest

Next, modify your vite.config.ts like this:

vite config file

3. Setup files

Create the setup file we specified in vite.config.ts. Inside src, create tests folder, and inside that, create Setup.ts with the following basic setup:

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

afterEach(() => {
  cleanup();
});

Now, update your tsconfig.json to include Vitest types:

{
  "compilerOptions": {
    ...
    "types": ["vitest/globals"] 
  }
}

and don't forget to include the scripts in package.json:

{
 "scripts": {
    ...
    "test": "vitest",
    "test:watch": "vitest --watch"
  }
}

4. Utils

This is an extra step, but it’s super helpful to create a utility file for your tests. Inside the tests folder, create a TestUtils.tsx file:

utils file

Explanation:

  1. The Wrapper function wraps your components in a BrowserRouter. You can extend this by adding other providers like QueryClientProvider or IntlProvider as needed.
  2. customRender provides this wrapper to the render function, and you can pass additional options as required.
  3. You export the necessary utilities from @testing-library/react.
  4. The setup function simplifies test setup by providing access to userEvent and the custom render function.

5. Testing.

Finally, let’s test our configuration with a basic test in CopyrightWarning.test.tsx`. For examplee:

testing with vitest

This should provide a solid guide for integrating Vitest into your project. Next steps will involve configuring MSW and Playwright for complete test coverage. Stay tuned!

...

🔧 Configure Vitest, MSW and Playwright in a React project with Vite and TS - Part 3


📈 98.69 Punkte
🔧 Programmierung

🔧 Configure Vitest, MSW and Playwright in a React project with Vite and TS - Part 2


📈 98.69 Punkte
🔧 Programmierung

🔧 Configure Vitest, MSW and Playwright in a React project with Vite and TS


📈 98.69 Punkte
🔧 Programmierung

🔧 Introduction to Testing React Components with Vite, Vitest and React Testing Library


📈 46.51 Punkte
🔧 Programmierung

🔧 Easier TypeScript API Testing with Vitest + MSW


📈 46.13 Punkte
🔧 Programmierung

🔧 Setting up a React project using Vite + TypeScript + Vitest


📈 45.22 Punkte
🔧 Programmierung

🔧 How to Create a Live Football Scoreboard in React with Vite and Vitest


📈 40.82 Punkte
🔧 Programmierung

🔧 Lessons Learned Migrating from React CRA & Jest to Vite & Vitest


📈 40.15 Punkte
🔧 Programmierung

🔧 Configure Eslint, Prettier and show eslint warning into running console vite react typescript project


📈 36.87 Punkte
🔧 Programmierung

🔧 Reliable Component Testing with Vitest's Browser Mode and Playwright


📈 36.66 Punkte
🔧 Programmierung

🔧 Effective Visual Regression Testing for Developers: Vitest vs Playwright


📈 35.99 Punkte
🔧 Programmierung

🔧 Beginner Guide on Unit Testing in React using React Testing Library and Vitest


📈 33.02 Punkte
🔧 Programmierung

🔧 Testing a React Application with Vitest and React Testing Library


📈 33.02 Punkte
🔧 Programmierung

🔧 Testing React App Using Vitest & React Testing Library


📈 32.35 Punkte
🔧 Programmierung

🔧 Make Your Vite Project LLM-Friendly with vite-plugin-llms


📈 32.06 Punkte
🔧 Programmierung

🔧 Data Visualization and Dropdowns made simple in Vite React: Chart.js, React-Chartjs-2, and React-Select


📈 31.9 Punkte
🔧 Programmierung

🔧 Create an SSR Application with Vite, React, React Query and React Router


📈 31.23 Punkte
🔧 Programmierung

🎥 Playwright Testing and GitHub Actions Tutorial: Run Your Playwright Tests on Every Code Commit


📈 30.72 Punkte
🎥 Video | Youtube

🔧 React Monorepo Setup Tutorial with pnpm and Vite: React project + UI, Utils


📈 30.61 Punkte
🔧 Programmierung

🔧 Configure tanstack router into vite project with authenticate routes, active routes.


📈 30.51 Punkte
🔧 Programmierung

🔧 Introducing Auto Playwright: Transforming Playwright Tests with AI


📈 30.05 Punkte
🔧 Programmierung

🔧 Accelerate Your Playwright Test Suite with Microsoft Playwright Testing


📈 30.05 Punkte
🔧 Programmierung

🔧 Create a project in React without create-react-app/vite 2023 (Spanish)


📈 29.94 Punkte
🔧 Programmierung

🔧 Why I choose CRA ( Create-react-app) over Vite for this React project ?


📈 29.94 Punkte
🔧 Programmierung

🔧 Deploy Vite React project with React router without 404 on Github pages


📈 29.94 Punkte
🔧 Programmierung

🔧 Comparing Jest, React Testing Library, and Playwright: Testing Approaches for React Applications


📈 27.07 Punkte
🔧 Programmierung

🔧 Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


📈 26.99 Punkte
🔧 Programmierung

📰 Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


📈 26.99 Punkte
📰 IT Nachrichten

🔧 Why Vite is the best? Advanced Features of Vite


📈 26.99 Punkte
🔧 Programmierung

🔧 Laravel Mix vs Vite: Why did Laravel Transitioned to Vite


📈 26.99 Punkte
🔧 Programmierung

🔧 Configure Playwright with Next.js & Mock APIs for Testing


📈 26.97 Punkte
🔧 Programmierung

🔧 React.js Vitest Unit Testing (Husky, lint-staged, ESLint, Prettier)


📈 26.66 Punkte
🔧 Programmierung

🔧 Efficiently Testing Asynchronous React Hooks with Vitest


📈 26.66 Punkte
🔧 Programmierung

🔧 React Unit Testing using Vitest


📈 26.66 Punkte
🔧 Programmierung

matomo