Skip to content

Commit

Permalink
chore: add test coverage (#2317)
Browse files Browse the repository at this point in the history
- Add support for test coverage so we easily can detect code that never
have run during our rests
- Using vitest with v8
- Open the generated file `coverage/index.html` in a browser to explore

---------

Co-authored-by: barsnes <[email protected]>
  • Loading branch information
eirikbacker and Barsnes authored Aug 30, 2024
1 parent dbdf38f commit 1caae92
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
30 changes: 30 additions & 0 deletions .github/workflows/test-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: 'Test Coverage'
on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest

permissions:
# Required to checkout the code
contents: read
# Required to put a comment into the pull-request
pull-requests: write

steps:
- uses: actions/checkout@v4
- name: 'Install Node'
uses: actions/setup-node@v4
with:
node-version: '20.x'
cache: 'yarn'
- name: 'Install Deps'
run: yarn install --frozen-lockfile
- name: 'Test'
run: npx vitest --coverage.enabled true
- name: 'Report Coverage'
# Set if: always() to also generate the report if tests are failing
# Only works if you set `reportOnFailure: true` in your vite config as specified above
if: always()
uses: davelosert/vitest-coverage-report-action@v2
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"scripts": {
"test": "vitest",
"test:coverage": "vitest run --coverage",
"storybook": "yarn workspace @designsystemet/storybook dev",
"storefront": "yarn workspace storefront dev",
"theme": "yarn workspace theme dev",
Expand Down Expand Up @@ -43,6 +44,7 @@
"@types/node": "^22.1.0",
"@types/prettier": "^3.0.0",
"@vitejs/plugin-react-swc": "^3.7.0",
"@vitest/coverage-v8": "^2.0.5",
"@vitest/expect": "^2.0.5",
"clsx": "^2.1.1",
"copyfiles": "^2.4.1",
Expand Down
11 changes: 11 additions & 0 deletions vitest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ export default defineConfig({
globals: true,
environment: 'jsdom',
setupFiles: ['./test/vitest.setup.ts'],
coverage: {
provider: 'v8',
reporter: ['html', 'json-summary', 'json'],
reportOnFailure: true,
include: ['packages/react/src/**/*.{ts,tsx}'],
exclude: [
'**/*.{stories,e2e,docs,test}.{ts,tsx}',
'**/index.ts',
'**/types/**',
],
},
css: {
modules: {
classNameStrategy: 'non-scoped',
Expand Down
116 changes: 115 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,13 @@ __metadata:
languageName: node
linkType: hard

"@bcoe/v8-coverage@npm:^0.2.3":
version: 0.2.3
resolution: "@bcoe/v8-coverage@npm:0.2.3"
checksum: 10/1a1f0e356a3bb30b5f1ced6f79c413e6ebacf130421f15fac5fcd8be5ddf98aedb4404d7f5624e3285b700e041f9ef938321f3ca4d359d5b716f96afa120d88d
languageName: node
linkType: hard

"@biomejs/biome@npm:1.8.3":
version: 1.8.3
resolution: "@biomejs/biome@npm:1.8.3"
Expand Down Expand Up @@ -3620,6 +3627,13 @@ __metadata:
languageName: node
linkType: hard

"@istanbuljs/schema@npm:^0.1.2":
version: 0.1.3
resolution: "@istanbuljs/schema@npm:0.1.3"
checksum: 10/a9b1e49acdf5efc2f5b2359f2df7f90c5c725f2656f16099e8b2cd3a000619ecca9fc48cf693ba789cf0fd989f6e0df6a22bc05574be4223ecdbb7997d04384b
languageName: node
linkType: hard

"@jest/schemas@npm:^29.6.3":
version: 29.6.3
resolution: "@jest/schemas@npm:29.6.3"
Expand Down Expand Up @@ -3717,7 +3731,7 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/trace-mapping@npm:^0.3.20, @jridgewell/trace-mapping@npm:^0.3.24, @jridgewell/trace-mapping@npm:^0.3.25":
"@jridgewell/trace-mapping@npm:^0.3.20, @jridgewell/trace-mapping@npm:^0.3.23, @jridgewell/trace-mapping@npm:^0.3.24, @jridgewell/trace-mapping@npm:^0.3.25":
version: 0.3.25
resolution: "@jridgewell/trace-mapping@npm:0.3.25"
dependencies:
Expand Down Expand Up @@ -6009,6 +6023,28 @@ __metadata:
languageName: node
linkType: hard

"@vitest/coverage-v8@npm:^2.0.5":
version: 2.0.5
resolution: "@vitest/coverage-v8@npm:2.0.5"
dependencies:
"@ampproject/remapping": "npm:^2.3.0"
"@bcoe/v8-coverage": "npm:^0.2.3"
debug: "npm:^4.3.5"
istanbul-lib-coverage: "npm:^3.2.2"
istanbul-lib-report: "npm:^3.0.1"
istanbul-lib-source-maps: "npm:^5.0.6"
istanbul-reports: "npm:^3.1.7"
magic-string: "npm:^0.30.10"
magicast: "npm:^0.3.4"
std-env: "npm:^3.7.0"
test-exclude: "npm:^7.0.1"
tinyrainbow: "npm:^1.2.0"
peerDependencies:
vitest: 2.0.5
checksum: 10/bb774d1a52b85adf94dcf62dc9684c59bd6aba6f8d43ce4d4afa06e3ca85651ec217f74842c0c4a81ea0158f029e484055207869e5d741cfbc3119257399fb83
languageName: node
linkType: hard

"@vitest/expect@npm:1.6.0":
version: 1.6.0
resolution: "@vitest/expect@npm:1.6.0"
Expand Down Expand Up @@ -10033,6 +10069,13 @@ __metadata:
languageName: node
linkType: hard

"html-escaper@npm:^2.0.0":
version: 2.0.2
resolution: "html-escaper@npm:2.0.2"
checksum: 10/034d74029dcca544a34fb6135e98d427acd73019796ffc17383eaa3ec2fe1c0471dcbbc8f8ed39e46e86d43ccd753a160631615e4048285e313569609b66d5b7
languageName: node
linkType: hard

"html-tags@npm:^3.1.0, html-tags@npm:^3.3.1":
version: 3.3.1
resolution: "html-tags@npm:3.3.1"
Expand Down Expand Up @@ -10650,6 +10693,45 @@ __metadata:
languageName: node
linkType: hard

"istanbul-lib-coverage@npm:^3.0.0, istanbul-lib-coverage@npm:^3.2.2":
version: 3.2.2
resolution: "istanbul-lib-coverage@npm:3.2.2"
checksum: 10/40bbdd1e937dfd8c830fa286d0f665e81b7a78bdabcd4565f6d5667c99828bda3db7fb7ac6b96a3e2e8a2461ddbc5452d9f8bc7d00cb00075fa6a3e99f5b6a81
languageName: node
linkType: hard

"istanbul-lib-report@npm:^3.0.0, istanbul-lib-report@npm:^3.0.1":
version: 3.0.1
resolution: "istanbul-lib-report@npm:3.0.1"
dependencies:
istanbul-lib-coverage: "npm:^3.0.0"
make-dir: "npm:^4.0.0"
supports-color: "npm:^7.1.0"
checksum: 10/86a83421ca1cf2109a9f6d193c06c31ef04a45e72a74579b11060b1e7bb9b6337a4e6f04abfb8857e2d569c271273c65e855ee429376a0d7c91ad91db42accd1
languageName: node
linkType: hard

"istanbul-lib-source-maps@npm:^5.0.6":
version: 5.0.6
resolution: "istanbul-lib-source-maps@npm:5.0.6"
dependencies:
"@jridgewell/trace-mapping": "npm:^0.3.23"
debug: "npm:^4.1.1"
istanbul-lib-coverage: "npm:^3.0.0"
checksum: 10/569dd0a392ee3464b1fe1accbaef5cc26de3479eacb5b91d8c67ebb7b425d39fd02247d85649c3a0e9c29b600809fa60b5af5a281a75a89c01f385b1e24823a2
languageName: node
linkType: hard

"istanbul-reports@npm:^3.1.7":
version: 3.1.7
resolution: "istanbul-reports@npm:3.1.7"
dependencies:
html-escaper: "npm:^2.0.0"
istanbul-lib-report: "npm:^3.0.0"
checksum: 10/f1faaa4684efaf57d64087776018d7426312a59aa6eeb4e0e3a777347d23cd286ad18f427e98f0e3dee666103d7404c9d7abc5f240406a912fa16bd6695437fa
languageName: node
linkType: hard

"jackspeak@npm:^2.3.5":
version: 2.3.6
resolution: "jackspeak@npm:2.3.6"
Expand Down Expand Up @@ -11370,6 +11452,17 @@ __metadata:
languageName: node
linkType: hard

"magicast@npm:^0.3.4":
version: 0.3.4
resolution: "magicast@npm:0.3.4"
dependencies:
"@babel/parser": "npm:^7.24.4"
"@babel/types": "npm:^7.24.0"
source-map-js: "npm:^1.2.0"
checksum: 10/704f86639b01c8e063155408cb181d89d4444db3a4a473fb501107f30f19d9c39a159dd315ef9e54a22291c090170044efd9b49a9b3ab8d6deb948a9c99d90b3
languageName: node
linkType: hard

"make-dir@npm:^2.0.0, make-dir@npm:^2.1.0":
version: 2.1.0
resolution: "make-dir@npm:2.1.0"
Expand All @@ -11389,6 +11482,15 @@ __metadata:
languageName: node
linkType: hard

"make-dir@npm:^4.0.0":
version: 4.0.0
resolution: "make-dir@npm:4.0.0"
dependencies:
semver: "npm:^7.5.3"
checksum: 10/bf0731a2dd3aab4db6f3de1585cea0b746bb73eb5a02e3d8d72757e376e64e6ada190b1eddcde5b2f24a81b688a9897efd5018737d05e02e2a671dda9cff8a8a
languageName: node
linkType: hard

"make-fetch-happen@npm:^10.0.3":
version: 10.2.1
resolution: "make-fetch-happen@npm:10.2.1"
Expand Down Expand Up @@ -15043,6 +15145,7 @@ __metadata:
"@types/node": "npm:^22.1.0"
"@types/prettier": "npm:^3.0.0"
"@vitejs/plugin-react-swc": "npm:^3.7.0"
"@vitest/coverage-v8": "npm:^2.0.5"
"@vitest/expect": "npm:^2.0.5"
clsx: "npm:^2.1.1"
copyfiles: "npm:^2.4.1"
Expand Down Expand Up @@ -16154,6 +16257,17 @@ __metadata:
languageName: node
linkType: hard

"test-exclude@npm:^7.0.1":
version: 7.0.1
resolution: "test-exclude@npm:7.0.1"
dependencies:
"@istanbuljs/schema": "npm:^0.1.2"
glob: "npm:^10.4.1"
minimatch: "npm:^9.0.4"
checksum: 10/e6f6f4e1df2e7810e082e8d7dfc53be51a931e6e87925f5e1c2ef92cc1165246ba3bf2dae6b5d86251c16925683dba906bd41e40169ebc77120a2d1b5a0dbbe0
languageName: node
linkType: hard

"theme@workspace:apps/theme":
version: 0.0.0-use.local
resolution: "theme@workspace:apps/theme"
Expand Down

0 comments on commit 1caae92

Please sign in to comment.