Skip to content

Commit 4e45432

Browse files
r1tsuuAlessioGr
andauthored
test: migrate to vitest (#14337)
Continuation of #10200 * For VSCode - you'll need to install the official plugin https://marketplace.visualstudio.com/items?itemName=vitest.explorer * Every test suite now explicitly imports the test runner functions like `describe`, `expect`, instead of using it from `global`. While `vitest` supports globals with https://vitest.dev/config/#globals, the main benefit of not doing this is that we don't conflict with test runners that have the same functions, e.g `playwright` and `tstyche`. * Now we have only 1 test config file that uses [projects](https://vitest.dev/guide/projects) for unit and integration tests --------- Co-authored-by: Alessio Gravili <github@gravili.net>
1 parent b4abd04 commit 4e45432

152 files changed

Lines changed: 1094 additions & 924 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/reproduction-guide.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
```
2121

2222
- `config.ts` - This is the _granular_ Payload config for testing. It should be as lightweight as possible. Reference existing configs for an example
23-
- `int.spec.ts` [Optional] - This is the test file run by jest. Any test file must have a `*int.spec.ts` suffix.
23+
- `int.spec.ts` [Optional] - This is the test file run by vitest. Any test file must have a `*int.spec.ts` suffix.
2424
- `e2e.spec.ts` [Optional] - This is the end-to-end test file that will load up the admin UI using the above config and run Playwright tests.
2525
- `payload-types.ts` - Generated types from `config.ts`. Generate this file by running `pnpm dev:generate-types _community`.
2626

@@ -34,11 +34,11 @@ An issue does not need to have failing tests — reproduction steps with your fo
3434

3535
### Running integration tests (Payload API tests)
3636

37-
First install [Jest Runner for VSVode](https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner).
37+
First install [Vitest Plugin for VSCode](https://marketplace.visualstudio.com/items?itemName=vitest.explorer).
3838

3939
There are a couple ways run integration tests:
4040

41-
- **Granularly** - you can run individual tests in vscode by installing the Jest Runner plugin and using that to run individual tests. Clicking the `debug` button will run the test in debug mode allowing you to set break points.
41+
- **Granularly** - you can run individual tests in vscode by installing the [Vitest Plugin](https://marketplace.visualstudio.com/items?itemName=vitest.explorer) and using that to run individual tests. Clicking the `debug` button will run the test in debug mode allowing you to set break points.
4242

4343
<img src="https://raw.githubusercontent.com/payloadcms/payload/main/.github/assets/int-debug.png" />
4444

.vscode/extensions.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"recommendations": [
3-
"dbaeumer.vscode-eslint",
43
"esbenp.prettier-vscode",
5-
"ms-playwright.playwright",
6-
"orta.vscode-jest"
4+
"dbaeumer.vscode-eslint",
5+
"vitest.explorer",
6+
"ms-playwright.playwright"
77
]
88
}

.vscode/settings.json

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,9 @@
1717
"typescript.tsdk": "node_modules/typescript/lib",
1818
// Load .git-blame-ignore-revs file
1919
"gitlens.advanced.blame.customArguments": ["--ignore-revs-file", ".git-blame-ignore-revs"],
20-
"jestrunner.jestCommand": "pnpm exec cross-env NODE_OPTIONS=\"--no-deprecation\" node 'node_modules/jest/bin/jest.js'",
21-
"jestrunner.changeDirectoryToWorkspaceRoot": false,
22-
"jestrunner.debugOptions": {
23-
"runtimeArgs": ["--no-deprecation"]
24-
},
2520
// Essentially disables bun test buttons
2621
"bun.test.filePattern": "bun.test.ts",
2722
"playwright.env": {
2823
"NODE_OPTIONS": "--no-deprecation --no-experimental-strip-types"
29-
},
30-
"jest.virtualFolders": [
31-
{
32-
"name": "root",
33-
"rootPath": ".",
34-
"runMode": {
35-
"type": "on-demand"
36-
}
37-
},
38-
{
39-
"name": "test",
40-
"rootPath": "test",
41-
"runMode": {
42-
"type": "on-demand"
43-
}
44-
}
45-
]
24+
}
4625
}

CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Each test directory in `test/` follows this pattern:
9696
```
9797
test/<feature-name>/
9898
├── config.ts # Lightweight Payload config for testing
99-
├── int.spec.ts # Integration tests (Jest)
99+
├── int.spec.ts # Integration tests (Vitest)
100100
├── e2e.spec.ts # End-to-end tests (Playwright)
101101
└── payload-types.ts # Generated types
102102
```

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ A typical directory with `test/` will be structured like this:
5555
```
5656

5757
- `config.ts` - This is the _granular_ Payload config for testing. It should be as lightweight as possible. Reference existing configs for an example
58-
- `int.spec.ts` - This is the test file run by jest. Any test file must have a `*int.spec.ts` suffix.
58+
- `int.spec.ts` - This is the test file run by vitest. Any test file must have a `*int.spec.ts` suffix.
5959
- `e2e.spec.ts` - This is the end-to-end test file that will load up the admin UI using the above config and run Playwright tests. These tests are typically only needed if a large change is being made to the Admin UI.
6060
- `payload-types.ts` - Generated types from `config.ts`. Generate this file by running `pnpm dev:generate-types my-test-dir`. Replace `my-test-dir` with the name of your testing directory.
6161

ISSUE_GUIDE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To report an issue, please follow the steps below:
1919
```
2020

2121
- `config.ts` - This is the _granular_ Payload config for testing. It should be as lightweight as possible. Reference existing configs for an example
22-
- `int.spec.ts` [Optional] - This is the test file run by jest. Any test file must have a `*int.spec.ts` suffix.
22+
- `int.spec.ts` [Optional] - This is the test file run by vitest. Any test file must have a `*int.spec.ts` suffix.
2323
- `e2e.spec.ts` [Optional] - This is the end-to-end test file that will load up the admin UI using the above config and run Playwright tests.
2424
- `payload-types.ts` - Generated types from `config.ts`. Generate this file by running `pnpm dev:generate-types _community`.
2525

@@ -43,7 +43,7 @@ An issue does not need to have failing tests — reproduction steps with your fo
4343

4444
There are a couple ways to do this:
4545

46-
- **Granularly** - you can run individual tests in vscode by installing the Jest Runner plugin and using that to run individual tests. Clicking the `debug` button will run the test in debug mode allowing you to set break points.
46+
- **Granularly** - you can run individual tests in vscode by installing the [Vitest Plugin](https://marketplace.visualstudio.com/items?itemName=vitest.explorer) and using that to run individual tests. Clicking the `debug` button will run the test in debug mode allowing you to set break points.
4747

4848
<img src="https://raw.githubusercontent.com/payloadcms/payload/main/.github/assets/int-debug.png" />
4949

eslint.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export const defaultESLintIgnores = [
99
'**/.pnp.*',
1010
'**/.svn',
1111
'**/playwright.config.ts',
12-
'**/jest.config.js',
12+
'**/vitest.config.ts',
13+
'**/vitest.setup.ts',
1314
'**/tsconfig.tsbuildinfo',
1415
'**/README.md',
1516
'**/eslint.config.js',
@@ -23,7 +24,6 @@ export const defaultESLintIgnores = [
2324
'next-env.d.ts',
2425
'**/app',
2526
'src/**/*.spec.ts',
26-
'**/jest.setup.js',
2727
'packages/payload/rollup.dts.config.mjs',
2828
]
2929

jest.config.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@
131131
"test:e2e:prod": "pnpm prepare-run-test-against-prod && pnpm runts ./test/runE2E.ts --prod",
132132
"test:e2e:prod:ci": "pnpm prepare-run-test-against-prod:ci && pnpm runts ./test/runE2E.ts --prod",
133133
"test:e2e:prod:ci:noturbo": "pnpm prepare-run-test-against-prod:ci && pnpm runts ./test/runE2E.ts --prod --no-turbo",
134-
"test:int": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand",
135-
"test:int:firestore": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=firestore DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand",
136-
"test:int:postgres": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=postgres DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand",
137-
"test:int:sqlite": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=sqlite DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand",
134+
"test:int": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true vitest --project int",
135+
"test:int:firestore": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=firestore DISABLE_LOGGING=true vitest --project int",
136+
"test:int:postgres": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=postgres DISABLE_LOGGING=true vitest --project int",
137+
"test:int:sqlite": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=sqlite DISABLE_LOGGING=true vitest --project int",
138138
"test:types": "tstyche",
139-
"test:unit": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=jest.config.js --runInBand",
139+
"test:unit": "vitest run --project unit",
140140
"translateNewKeys": "pnpm --filter @tools/scripts run generateTranslations:core"
141141
},
142142
"lint-staged": {
@@ -152,7 +152,6 @@
152152
},
153153
"devDependencies": {
154154
"@axe-core/playwright": "4.11.0",
155-
"@jest/globals": "29.7.0",
156155
"@libsql/client": "0.14.0",
157156
"@next/bundle-analyzer": "15.4.7",
158157
"@payloadcms/db-postgres": "workspace:*",
@@ -167,7 +166,6 @@
167166
"@swc/core": "1.15.3",
168167
"@swc/jest": "0.2.39",
169168
"@types/fs-extra": "^11.0.2",
170-
"@types/jest": "29.5.12",
171169
"@types/minimist": "1.2.5",
172170
"@types/node": "22.15.30",
173171
"@types/react": "19.2.1",
@@ -188,7 +186,6 @@
188186
"fs-extra": "10.1.0",
189187
"globby": "11.1.0",
190188
"husky": "9.0.11",
191-
"jest": "29.7.0",
192189
"lint-staged": "15.2.7",
193190
"minimist": "1.2.8",
194191
"mongoose": "8.15.1",
@@ -212,6 +209,7 @@
212209
"tsx": "4.19.2",
213210
"turbo": "^2.5.4",
214211
"typescript": "5.7.3",
212+
"vitest": "4.0.15",
215213
"wrangler": "~4.42.0"
216214
},
217215
"packageManager": "pnpm@9.7.1",
@@ -224,7 +222,7 @@
224222
"copyfiles": "$copyfiles",
225223
"cross-env": "$cross-env",
226224
"dotenv": "$dotenv",
227-
"graphql": "^16.8.1",
225+
"graphql": "16.8.1",
228226
"react": "$react",
229227
"react-dom": "$react-dom",
230228
"typescript": "$typescript"

packages/create-payload-app/jest.config.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)