-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVitest.txt
64 lines (53 loc) · 1.67 KB
/
Vitest.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
you can use Vitest for testing in your Next.js project! Vitest is a fast unit test framework similar to Jest, and it integrates well with modern JavaScript frameworks like Next.js.
Here’s how you can set it up:
Step-by-Step Setup:
Install Vitest and Related Packages: Since you use pnpm, run the following command to install Vitest:
pnpm add -D vitest @testing-library/react @testing-library/jest-dom jsdom
Update package.json: Add a test script for Vitest in your package.json:
{
"scripts": {
"test": "vitest"
}
}
Create a Vitest Config: In the root of your project, create a vitest.config.ts file:
ts
Copy code
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'jsdom',
globals: true,
setupFiles: './vitest.setup.ts',
},
});
Set Up Testing Environment: Create a vitest.setup.ts file in your root folder for global configuration:
ts
Copy code
import '@testing-library/jest-dom';
Write a Simple Test: Inside your __tests__ folder or any test folder you prefer, create a sample test file App.test.tsx:
tsx
Copy code
import { render, screen } from '@testing-library/react';
import Home from '../app/page';
test('renders the home page', () => {
render(<Home />);
expect(screen.getByText('Welcome to the BCFCODEWars!')).toBeInTheDocument();
});
Run the Tests: Now, you can run the tests with the command:
bash
Copy code
pnpm test
Additional Configuration
If you want coverage reports, you can add c8:
bash
Copy code
pnpm add -D c8
Update the test script in package.json:
json
Copy code
{
"scripts": {
"test:coverage": "vitest run --coverage"
}
}
With these steps, you’ll have Vitest set up for your Next.js project!