Skip to content

Commit 9ce03dd

Browse files
committed
05/01: add solution
1 parent 29c30c8 commit 9ce03dd

File tree

11 files changed

+965
-2
lines changed

11 files changed

+965
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ data.db
1212
**/*.tsbuildinfo
1313
__screenshots__
1414
*.sqlite
15+
test-profiles
1516

1617
# in a real app you'd want to not commit the .env
1718
# file as well, but since this is for a workshop
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# Custom test environment
2+
3+
## Questions to answer
4+
5+
- When do I need custom environment over global test setup (`globalSetup`)? What's the practical difference?
6+
- `globalSetup` runs _once_ for the whole test run. Test environment runs for each worker.
7+
- `globalSetup` runs on the _main thread_, outside of the test workers. This means you cannot modify `globalThis` from the global setup because it doesn't share the thread with the test worker. You can do that with `setupFiles` because of when they run.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Environment, builtinEnvironments } from 'vitest/environments'
2+
3+
export default <Environment>{
4+
name: 'custom-env',
5+
transformMode: 'ssr',
6+
async setup(global, options) {
7+
const { teardown } = await builtinEnvironments.node.setup(global, options)
8+
9+
return {
10+
teardown,
11+
}
12+
},
13+
}
14+
15+
/**
16+
17+
1. Polyfill something unavailable in the main environment.
18+
19+
2. Perform a test suite-wide side effect necessary for the tests to run.
20+
21+
3. Testing code that's not meant to run in Node/browser (e.g. edge, Cloudflare Workers, etc).
22+
23+
*/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# Applying environments
2+
3+
- https://vitest.dev/config/#environmentmatchglobs
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
# Profiling slow tests
2+
3+
## Step 0. Know your test runner
4+
5+
Know the steps that Vitest takes to run your test. Any step may introduce performance degradation. Know the main thread (where Vite and `globalSetup` runs), the forks (where test environment and `setupFiles` run), test transformation, test collection, and tests themselves.
6+
7+
## Step 1. Use aggregated test metrics:
8+
9+
```
10+
Duration 2.19s (transform 16ms, setup 0ms, collect 10ms, tests 2.00s, environment 0ms, prepare 46ms)
11+
// ^^^^^^^^^^^
12+
```
13+
14+
This is a good overlook at which areas of your test take how much time. Understanding what each area does sets up your map for future debugging.

exercises/05.performance/01.solution.profiling-slow-tests/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"scripts": {
55
"dev": "vite",
66
"test": "vitest",
7+
"test:profile": "vitest-profiler npm test",
78
"build": "vite build"
89
},
910
"devDependencies": {
1011
"vite": "^6.0.7",
11-
"vitest": "^3.0.5"
12+
"vitest": "^3.0.5",
13+
"vitest-profiler": "^0.1.7"
1214
}
1315
}

0 commit comments

Comments
 (0)