Lädt...


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


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Playwright is a framework-agnostic end-to-end testing (also known as E2E, or integration testing) tool for web apps. Playwright has great developer experience and makes writing good and resilient to changes tests straightforward.

1. Install Playwright

To set up Playwright, run the following command:

npm init playwright@latest

\n\n

You'll be guided through a setup wizard in your terminal. When prompted with "Where to put your end-to-end tests?", you can set it to src/tests (as recommended in earlier tutorials).

terminal prompt

Add Scripts to package.json

In your package.json, add the following two scripts for running Playwright tests:

package.json config

This allows you to run the tests in both development and CI environments.

Playwright Configuration

The playwright.config.ts file should be configured as follows:

playwright.config.ts file

Key Changes in the Configuration:

  • use.baseURL: This sets the base URL of your development server, so you don’t have to write it in every test.
  • webServer: This block describes how to start your development server. It will reuse an already-running server unless you are in a CI environment.
  • testDir: The directory where Playwright should look for your E2E tests (in this case, src/tests/e2e).

2. Configure MSW for Browser Testing

So far, MSW has been set up for mocking API responses in a Node.js environment. However, because Playwright uses a real browser for testing, you need to configure MSW to work in the browser environment.

Create a new JavaScript module that will register the MSW service worker for browser-based tests:

service worker file

3. Register the Worker in Development Mode

To start the MSW worker when the app is running in development mode, add the following to your app’s root module (e.g., src/main.tsx):

main file

Make sure to set the VITE_API_MOCK environment variable in your .env file:

VITE_API_MOCK=enabled

4. Write an E2E Test

Now you can write an E2E test for a flow in your app. Here’s an example Playwright test:

test file

5. Running the Test

To run the E2E test, use the following command:

npm run test:e2e:ci

This will execute the test in CI mode, which is useful for automated pipelines.

...

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


📈 101.22 Punkte
🔧 Programmierung

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


📈 48.21 Punkte
🔧 Programmierung

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


📈 46.68 Punkte
🔧 Programmierung

🔧 Easier TypeScript API Testing with Vitest + MSW


📈 46.53 Punkte
🔧 Programmierung

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


📈 42.21 Punkte
🔧 Programmierung

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


📈 38.14 Punkte
🔧 Programmierung

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


📈 37.92 Punkte
🔧 Programmierung

🔧 Effective Visual Regression Testing for Developers: Vitest vs Playwright


📈 37.21 Punkte
🔧 Programmierung

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


📈 34.09 Punkte
🔧 Programmierung

🔧 Testing a React Application with Vitest and React Testing Library


📈 34.09 Punkte
🔧 Programmierung

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


📈 33.43 Punkte
🔧 Programmierung

🔧 Testing React App Using Vitest & React Testing Library


📈 33.37 Punkte
🔧 Programmierung

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


📈 32.38 Punkte
🎥 Video | Youtube

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


📈 32.01 Punkte
🔧 Programmierung

🔧 Introducing Auto Playwright: Transforming Playwright Tests with AI


📈 31.67 Punkte
🔧 Programmierung

🔧 Accelerate Your Playwright Test Suite with Microsoft Playwright Testing


📈 31.67 Punkte
🔧 Programmierung

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


📈 31.43 Punkte
🔧 Programmierung

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


📈 31.3 Punkte
🔧 Programmierung

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


📈 31.3 Punkte
🔧 Programmierung

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


📈 28.55 Punkte
🔧 Programmierung

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


📈 28.25 Punkte
🔧 Programmierung

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


📈 28.25 Punkte
📰 IT Nachrichten

🔧 Why Vite is the best? Advanced Features of Vite


📈 28.25 Punkte
🔧 Programmierung

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


📈 28.25 Punkte
🔧 Programmierung

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


📈 27.96 Punkte
🔧 Programmierung

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


📈 27.37 Punkte
🔧 Programmierung

🔧 Efficiently Testing Asynchronous React Hooks with Vitest


📈 27.37 Punkte
🔧 Programmierung

🔧 React Unit Testing using Vitest


📈 27.37 Punkte
🔧 Programmierung

🔧 Effortless Testing Setup for React with Vite, TypeScript, Jest, and React Testing Library


📈 26.84 Punkte
🔧 Programmierung

🔧 React as a Library and Create React App / Vite as Frameworks


📈 26.84 Punkte
🔧 Programmierung

🔧 Setting Up Project React and TypeScript Application with Vite and Tailwind CSS


📈 26.73 Punkte
🔧 Programmierung

🔧 How to configure PHPStorm to work with Vite - Aliases


📈 26.25 Punkte
🔧 Programmierung

🔧 This Week In React #174: ReactLabs, React-Strict-DOM, Remix, RNGH, Expo, RN+TV, TC39, Vite...


📈 26.12 Punkte
🔧 Programmierung

matomo