Skip to content

Commit 596b6f8

Browse files
committed
01/03: add solution code
1 parent 2c60050 commit 596b6f8

File tree

7 files changed

+830
-15
lines changed

7 files changed

+830
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ data.db
1313
__screenshots__
1414
*.sqlite
1515
test-profiles
16+
coverage
1617

1718
# in a real app you'd want to not commit the .env
1819
# file as well, but since this is for a workshop
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
11
# Code coverage
2+
3+
1. Install `@vitest/coverage-v8`.
4+
1. Set `test.coverage.enabled` to `true`.
5+
1. List explicit values in `test.coverage.include` to make Vitest faster.
6+
7+
Then, to preview the coverage:
8+
9+
1. Install `@vitest/ui`.
10+
1. Run Vitest in the UI mode: `npx vitest --ui` (or set `test.ui` to `true`).
11+
1. Make sure to include `html` in the `test.coverage.reporters` list (you can still use other reporters).
12+
1. In the UI, go to the "Coverage" tab. See the coverage.
13+
14+
## Related materials
15+
16+
- [**Making sense of code coverage**](https://www.epicweb.dev/making-use-of-code-coverage)
17+
- [Coverage in Vitest](https://main.vitest.dev/guide/coverage.html)
18+
- [Vitest UI](https://main.vitest.dev/guide/ui.html)

exercises/01.setup/03.solution.code-coverage/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"build": "vite build"
88
},
99
"devDependencies": {
10+
"@vitest/coverage-v8": "^3.1.1",
11+
"@vitest/ui": "^3.1.1",
1012
"vite": "^6.0.7",
1113
"vitest": "^3.0.5"
1214
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { fn } from './fn'
2+
3+
test('returns "great" if given a number greater than 10', () => {
4+
expect(fn(42)).toBe('great')
5+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function fn(input: number) {
2+
if (input > 10) {
3+
return 'great'
4+
}
5+
6+
return 'okay'
7+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { defineConfig } from 'vitest/config'
22

33
export default defineConfig({
4-
server: {
5-
port: process.env.PORT ? Number(process.env.PORT) : undefined,
6-
},
74
test: {
85
globals: true,
6+
coverage: {
7+
enabled: true,
8+
include: ['src/**/*.ts'],
9+
reporter: ['html'],
10+
},
911
},
1012
})

0 commit comments

Comments
 (0)