Skip to content

Commit 0d7224c

Browse files
committed
04/04: add problem text
1 parent 63b9166 commit 0d7224c

File tree

17 files changed

+211
-22
lines changed

17 files changed

+211
-22
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Sharding
2+
3+
Our test files are running in parallel and our test cases are running concurrently, but we can do more with Vitest. Large test suites can still be slow even despite all of that parallelism simply because there are a lot of tests to run.
4+
5+
This is where it comes in handy to split our entire test suite into groups (or shards) using a technique called _sharding_.
6+
7+
With sharding, we can split a huge test suite in smaller pieces and run them individually and in parallel. It will mean _multiple Vitest processes_ running different portions of the test suite.
8+
9+
Sharing is particularly handy in CI where you can split a huge test suite into groups and run them in parallel jobs, giving you faster test runs and, as a result, faster iteration loop even when you've got a lot of tests across the product.
10+
11+
## Your task
12+
13+
Right now, you've got a _big_ test suite on your hands:
14+
15+
```
16+
✓ src/one.test.ts (5000 tests) 2645ms
17+
✓ src/two.test.ts (5000 tests) 2643ms
18+
19+
Test Files 2 passed (2)
20+
Tests 10000 passed (10000)
21+
Start at 11:31:23
22+
Duration 2.93s (transform 24ms, setup 0ms, collect 75ms, tests 5.29s, environment 0ms, prepare 68ms)
23+
```
24+
25+
These 10,000 tests take almost _3 seconds_ to complete. That won't do!
26+
27+
👨‍💼 In this exercise, your task is to split these tests into shards. This will mean writing a custom script that will run your tests at <InlineFile file="run-tests.sh" />. Open that file and complete it to enable sharding.
28+
29+
Once you're done, verify your setup by running that Bash script:
30+
31+
```
32+
./run-tests.sh
33+
```
34+
35+
Then, merge the test reports generated by Vitest using this command:
36+
37+
```
38+
npx vitest run --merge-reports
39+
```
40+
41+
Preview the merged reports to see the test summary from your sharded run.
42+
43+
Good luck!
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "module",
3+
"name": "exercises_04.performance_04.problem.sharding",
4+
"scripts": {
5+
"test": "vitest"
6+
},
7+
"devDependencies": {
8+
"vitest": "^3.1.1"
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=1/4 & \
2+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=2/4 & \
3+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=3/4 & \
4+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=4/4 & \
5+
wait
6+
npx vitest run --merge-reports
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { setTimeout } from 'node:timers/promises'
2+
3+
for (let i = 0; i < 5000; i++) {
4+
test.concurrent(`${i}`, async ({ expect }) => {
5+
await setTimeout(25)
6+
expect(i).toBe(i)
7+
})
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { setTimeout } from 'node:timers/promises'
2+
3+
for (let i = 0; i < 5000; i++) {
4+
test.concurrent(`${i}`, async ({ expect }) => {
5+
await setTimeout(25)
6+
expect(i).toBe(i)
7+
})
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"include": ["src/**/*"],
4+
"exclude": ["src/**/*.test.ts*"],
5+
"compilerOptions": {
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"jsx": "react-jsx"
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"useDefineForClassFields": true,
6+
"skipLibCheck": true,
7+
8+
/* Bundler mode */
9+
"moduleResolution": "bundler",
10+
"allowImportingTsExtensions": true,
11+
"isolatedModules": true,
12+
"moduleDetection": "force",
13+
"noEmit": true,
14+
"verbatimModuleSyntax": true,
15+
16+
/* Linting */
17+
"strict": true,
18+
"noUnusedLocals": false,
19+
"noUnusedParameters": false,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedSideEffectImports": true
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.app.json" },
5+
{ "path": "./tsconfig.node.json" },
6+
{ "path": "./tsconfig.test.json" }
7+
]
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"lib": ["ES2023"]
5+
},
6+
"include": ["vitest.config.ts"]
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.app.json",
3+
"include": ["src/**/*", "src/**/*.test.ts*"],
4+
"exclude": [],
5+
"compilerOptions": {
6+
"types": ["vitest/globals"]
7+
}
8+
}

0 commit comments

Comments
 (0)