---
title: Run React.js tests in a container
linkTitle: Run your tests
weight: 40
keywords: react.js, react, test, vitest
description: Learn how to run your React.js tests in a container.
---
## Prerequisites
Complete all the previous sections of this guide, starting with [Containerize React.js application](containerize.md).
## Overview
Testing is a critical part of the development process. In this section, you'll learn how to:
- Run unit tests using Vitest inside a Docker container.
- Use Docker Compose to run tests in an isolated, reproducible environment.
You’ll use [Vitest](https://vitest.dev) — a blazing fast test runner designed for Vite — along with [Testing Library](https://testing-library.com/) for assertions.
---
## Run tests during development
`docker-reactjs-sample` application includes a sample test file at location:
```console
$ src/App.test.tsx
```
This file uses Vitest and React Testing Library to verify the behavior of `App` component.
### Step 1: Install Vitest and React Testing Library
If you haven’t already added the necessary testing tools, install them by running:
```console
$ npm install --save-dev vitest @testing-library/react @testing-library/jest-dom jsdom
```
Then, update the scripts section of your `package.json` file to include the following:
```json
"scripts": {
"test": "vitest run"
}
```
---
### Step 2: Configure Vitest
Update `vitest.config.ts` file in your project root with the following configuration:
```ts {hl_lines="14-18",linenos=true}
/// <reference types="vitest" />
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
base: "/",
plugins: [react()],
server: {
host: true,
port: 5173,
strictPort: true,
},
test: {
environment: "jsdom",
setupFiles: "./src/setupTests.ts",
globals: true,
},
});
```
> [!NOTE]
> The `test` options in `vitest.config.ts` are essential for reliable testing inside Docker:
> - `environment: "jsdom"` simulates a browser-like environment for rendering and DOM interactions.
> - `setupFiles: "./src/setupTests.ts"` loads global configuration or mocks before each test file (optional but recommended).
> - `globals: true` enables global test functions like `describe`, `it`, and `expect` without importing them.
>
> For more details, see the official [Vitest configuration docs](https://vitest.dev/config/).
### Step 3: Update compose.yaml
Add a new service named `react-test` to your `compose.yaml` file. This service allows you to run your test suite in an isolated containerized environment.
```yaml {hl_lines="22-26",linenos=true}
services:
react-dev:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "5173:5173"
develop:
watch:
- action: sync
path: .
target: /app
react-prod:
build:
context: .
dockerfile: Dockerfile