[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

test(coverage): format snapshots with `formatSummary` (#7482)

authored by

Ari Perkkiö and committed by
GitHub
(Feb 13, 2025, 5:12 PM +0200) cb48e64e 5e211811

+176 -160
+29
test/coverage-test/setup.ts
··· 1 + import type { CoverageMap, FileCoverage } from 'istanbul-lib-coverage' 2 + import { expect } from 'vitest' 3 + import { formatSummary } from './utils' 4 + 5 + expect.addSnapshotSerializer({ 6 + test: val => val.constructor.name === 'CoverageMap', 7 + serialize: (val: CoverageMap, config, indentation, depth, refs, printer) => { 8 + return printer(formatSummary(val.getCoverageSummary()), config, indentation, depth, refs) 9 + }, 10 + }) 11 + 12 + expect.addSnapshotSerializer({ 13 + test: val => val.constructor.name === 'FileCoverage', 14 + serialize: (val: FileCoverage, config, indentation, depth, refs, printer) => { 15 + return printer(formatSummary(val.toSummary()), config, indentation, depth, refs) 16 + }, 17 + }) 18 + 19 + expect.addSnapshotSerializer({ 20 + test: val => Array.isArray(val) && val.every(entry => entry.constructor.name === 'FileCoverage'), 21 + serialize: (val: FileCoverage[], config, indentation, depth, refs, printer) => { 22 + const summary = val.reduce((all, current) => ({ 23 + ...all, 24 + [current.path]: formatSummary(current.toSummary()), 25 + }), {}) 26 + 27 + return printer(summary, config, indentation, depth, refs) 28 + }, 29 + })
+8 -1
test/coverage-test/utils.ts
··· 1 - import type { FileCoverageData } from 'istanbul-lib-coverage' 1 + import type { CoverageSummary, FileCoverageData } from 'istanbul-lib-coverage' 2 2 import type { TestFunction } from 'vitest' 3 3 import type { UserConfig } from 'vitest/node' 4 4 import { readFileSync } from 'node:fs' ··· 87 87 export async function readCoverageMap(name = './coverage/coverage-final.json') { 88 88 const coverageJson = await readCoverageJson(name) 89 89 return libCoverage.createCoverageMap(coverageJson) 90 + } 91 + 92 + export function formatSummary(summary: CoverageSummary) { 93 + return (['branches', 'functions', 'lines', 'statements'] as const).reduce((all, current) => ({ 94 + ...all, 95 + [current]: `${summary[current].covered}/${summary[current].total} (${summary[current].pct}%)`, 96 + }), {}) 90 97 } 91 98 92 99 export function normalizeFilename(filename: string) {
+1
test/coverage-test/vitest.workspace.custom.ts
··· 10 10 const config = defineConfig({ 11 11 test: { 12 12 pool: 'threads', 13 + setupFiles: ['./setup.ts'], 13 14 }, 14 15 }) 15 16
+39 -5
test/coverage-test/test/changed.test.ts
··· 1 1 import { readFileSync, rmSync, writeFileSync } from 'node:fs' 2 2 import { resolve } from 'node:path' 3 3 import { beforeAll, expect } from 'vitest' 4 - import { readCoverageMap, runVitest, test } from '../utils' 4 + import { isV8Provider, readCoverageMap, runVitest, test } from '../utils' 5 5 6 6 // Note that this test may fail if you have new files in "vitest/test/coverage/src" 7 7 // and have not yet committed those ··· 49 49 ] 50 50 `) 51 51 52 - const uncoveredFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/new-uncovered-file.ts').toSummary() 53 - expect(uncoveredFile.lines.pct).toBe(0) 52 + const uncoveredFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/new-uncovered-file.ts') 53 + const changedFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/file-to-change.ts') 54 54 55 - const changedFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/file-to-change.ts').toSummary() 56 - expect(changedFile.lines.pct).toBeGreaterThanOrEqual(50) 55 + if (isV8Provider()) { 56 + expect([uncoveredFile, changedFile]).toMatchInlineSnapshot(` 57 + { 58 + "<process-cwd>/fixtures/src/file-to-change.ts": { 59 + "branches": "1/1 (100%)", 60 + "functions": "1/2 (50%)", 61 + "lines": "4/6 (66.66%)", 62 + "statements": "4/6 (66.66%)", 63 + }, 64 + "<process-cwd>/fixtures/src/new-uncovered-file.ts": { 65 + "branches": "1/1 (100%)", 66 + "functions": "1/1 (100%)", 67 + "lines": "0/3 (0%)", 68 + "statements": "0/3 (0%)", 69 + }, 70 + } 71 + `) 72 + } 73 + else { 74 + expect([uncoveredFile, changedFile]).toMatchInlineSnapshot(` 75 + { 76 + "<process-cwd>/fixtures/src/file-to-change.ts": { 77 + "branches": "0/0 (100%)", 78 + "functions": "1/2 (50%)", 79 + "lines": "1/2 (50%)", 80 + "statements": "1/2 (50%)", 81 + }, 82 + "<process-cwd>/fixtures/src/new-uncovered-file.ts": { 83 + "branches": "0/0 (100%)", 84 + "functions": "0/1 (0%)", 85 + "lines": "0/1 (0%)", 86 + "statements": "0/1 (0%)", 87 + }, 88 + } 89 + `) 90 + } 57 91 }, SKIP)
+10 -51
test/coverage-test/test/file-outside-vite.test.ts
··· 10 10 11 11 const coverageMap = await readCoverageMap() 12 12 const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/load-outside-vite.cjs') 13 - const summary = fileCoverage.toSummary() 14 13 15 14 if (isV8Provider()) { 16 - expect(summary).toMatchInlineSnapshot(` 15 + expect(fileCoverage).toMatchInlineSnapshot(` 17 16 { 18 - "branches": { 19 - "covered": 0, 20 - "pct": 100, 21 - "skipped": 0, 22 - "total": 0, 23 - }, 24 - "functions": { 25 - "covered": 0, 26 - "pct": 0, 27 - "skipped": 0, 28 - "total": 1, 29 - }, 30 - "lines": { 31 - "covered": 1, 32 - "pct": 100, 33 - "skipped": 0, 34 - "total": 1, 35 - }, 36 - "statements": { 37 - "covered": 1, 38 - "pct": 100, 39 - "skipped": 0, 40 - "total": 1, 41 - }, 17 + "branches": "0/0 (100%)", 18 + "functions": "0/1 (0%)", 19 + "lines": "1/1 (100%)", 20 + "statements": "1/1 (100%)", 42 21 } 43 22 `) 44 23 } 45 24 else { 46 - expect(summary).toMatchInlineSnapshot(` 25 + expect(fileCoverage).toMatchInlineSnapshot(` 47 26 { 48 - "branches": { 49 - "covered": 0, 50 - "pct": 100, 51 - "skipped": 0, 52 - "total": 0, 53 - }, 54 - "functions": { 55 - "covered": 0, 56 - "pct": 0, 57 - "skipped": 0, 58 - "total": 1, 59 - }, 60 - "lines": { 61 - "covered": 0, 62 - "pct": 0, 63 - "skipped": 0, 64 - "total": 1, 65 - }, 66 - "statements": { 67 - "covered": 0, 68 - "pct": 0, 69 - "skipped": 0, 70 - "total": 1, 71 - }, 27 + "branches": "0/0 (100%)", 28 + "functions": "0/1 (0%)", 29 + "lines": "0/1 (0%)", 30 + "statements": "0/1 (0%)", 72 31 } 73 32 `) 74 33 }
+2 -2
test/coverage-test/test/include-exclude.test.ts
··· 13 13 }) 14 14 15 15 const coverageMap = await readCoverageMap() 16 - expect(coverageMap.files()).toMatchInlineSnapshot(`[]`) 16 + expect(coverageMap.files()).toMatchInlineSnapshot(`{}`) 17 17 }) 18 18 19 19 test('overridden exclude should still apply defaults', async () => { ··· 28 28 }) 29 29 30 30 const coverageMap = await readCoverageMap() 31 - expect(coverageMap.files()).toMatchInlineSnapshot(`[]`) 31 + expect(coverageMap.files()).toMatchInlineSnapshot(`{}`) 32 32 }) 33 33 34 34 test('test file is excluded from report when excludes is not set', async () => {
+41 -12
test/coverage-test/test/isolation.test.ts
··· 1 1 import type { TestSpecification } from 'vitest/node' 2 2 import { expect, test } from 'vitest' 3 - import { readCoverageMap, runVitest } from '../utils' 3 + import { formatSummary, isV8Provider, readCoverageMap, runVitest } from '../utils' 4 4 5 5 const pools = ['forks'] 6 6 ··· 28 28 29 29 coverage: { 30 30 all: false, 31 - reporter: ['json', 'html'], 31 + reporter: 'json', 32 32 }, 33 33 34 34 browser: { ··· 37 37 }) 38 38 39 39 const coverageMap = await readCoverageMap() 40 - 41 40 const branches = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/branch.ts') 42 - expect(branches.toSummary().lines.pct).toBe(100) 43 - expect(branches.toSummary().statements.pct).toBe(100) 44 - expect(branches.toSummary().functions.pct).toBe(100) 45 - expect(branches.toSummary().branches.pct).toBe(100) 46 - 47 41 const math = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/math.ts') 48 - expect(math.toSummary().lines.pct).toBe(100) 49 - expect(math.toSummary().statements.pct).toBe(100) 50 - expect(math.toSummary().functions.pct).toBe(100) 51 - expect(math.toSummary().branches.pct).toBe(100) 42 + 43 + const summary = { 44 + [branches.path]: formatSummary(branches.toSummary()), 45 + [math.path]: formatSummary(math.toSummary()), 46 + } 47 + 48 + if (isV8Provider()) { 49 + expect(summary).toStrictEqual({ 50 + '<process-cwd>/fixtures/src/branch.ts': { 51 + branches: '3/3 (100%)', 52 + functions: '1/1 (100%)', 53 + lines: '6/6 (100%)', 54 + statements: '6/6 (100%)', 55 + }, 56 + '<process-cwd>/fixtures/src/math.ts': { 57 + branches: '4/4 (100%)', 58 + functions: '4/4 (100%)', 59 + lines: '12/12 (100%)', 60 + statements: '12/12 (100%)', 61 + }, 62 + }) 63 + } 64 + else { 65 + expect(summary).toStrictEqual({ 66 + '<process-cwd>/fixtures/src/branch.ts': { 67 + branches: '2/2 (100%)', 68 + functions: '1/1 (100%)', 69 + lines: '4/4 (100%)', 70 + statements: '4/4 (100%)', 71 + }, 72 + '<process-cwd>/fixtures/src/math.ts': { 73 + branches: '0/0 (100%)', 74 + functions: '4/4 (100%)', 75 + lines: '4/4 (100%)', 76 + statements: '4/4 (100%)', 77 + }, 78 + }, 79 + ) 80 + } 52 81 }) 53 82 } 54 83 }
+22 -3
test/coverage-test/test/setup-files.test.ts
··· 1 1 import { resolve } from 'node:path' 2 2 import { expect } from 'vitest' 3 - import { readCoverageMap, runVitest, test } from '../utils' 3 + import { isV8Provider, readCoverageMap, runVitest, test } from '../utils' 4 4 5 5 test('tests with multiple suites are covered (#3514)', async () => { 6 6 const { stdout } = await runVitest({ ··· 29 29 30 30 // Some valid coverage should be reported 31 31 const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/math.ts') 32 - expect(fileCoverage.toSummary().functions.covered).toBe(1) 33 - expect(fileCoverage.toSummary().functions.pct).toBeLessThanOrEqual(25) 32 + 33 + if (isV8Provider()) { 34 + expect(fileCoverage).toMatchInlineSnapshot(` 35 + { 36 + "branches": "1/1 (100%)", 37 + "functions": "1/4 (25%)", 38 + "lines": "6/12 (50%)", 39 + "statements": "6/12 (50%)", 40 + } 41 + `) 42 + } 43 + else { 44 + expect(fileCoverage).toMatchInlineSnapshot(` 45 + { 46 + "branches": "0/0 (100%)", 47 + "functions": "1/4 (25%)", 48 + "lines": "1/4 (25%)", 49 + "statements": "1/4 (25%)", 50 + } 51 + `) 52 + } 34 53 })
+21 -86
test/coverage-test/test/vue.test.ts
··· 22 22 23 23 test('coverage results matches snapshot', async () => { 24 24 const coverageMap = await readCoverageMap() 25 - const summary = coverageMap.getCoverageSummary() 26 25 27 - if (isV8Provider()) { 28 - const { branches, functions, lines, statements } = summary 29 - 30 - expect({ branches, functions }).toMatchInlineSnapshot(` 26 + if (isV8Provider() && isBrowser()) { 27 + expect(coverageMap).toMatchInlineSnapshot(` 31 28 { 32 - "branches": { 33 - "covered": 5, 34 - "pct": 83.33, 35 - "skipped": 0, 36 - "total": 6, 37 - }, 38 - "functions": { 39 - "covered": 3, 40 - "pct": 60, 41 - "skipped": 0, 42 - "total": 5, 43 - }, 29 + "branches": "5/6 (83.33%)", 30 + "functions": "3/5 (60%)", 31 + "lines": "40/48 (83.33%)", 32 + "statements": "40/48 (83.33%)", 44 33 } 45 34 `) 46 - 47 - // Lines and statements are not 100% identical between node and browser - not sure if it's Vue, Vite or Vitest issue 48 - if (isBrowser()) { 49 - expect({ lines, statements }).toMatchInlineSnapshot(` 50 - { 51 - "lines": { 52 - "covered": 40, 53 - "pct": 83.33, 54 - "skipped": 0, 55 - "total": 48, 56 - }, 57 - "statements": { 58 - "covered": 40, 59 - "pct": 83.33, 60 - "skipped": 0, 61 - "total": 48, 62 - }, 63 - } 64 - `) 65 - } 66 - else { 67 - expect({ lines, statements }).toMatchInlineSnapshot(` 68 - { 69 - "lines": { 70 - "covered": 35, 71 - "pct": 81.39, 72 - "skipped": 0, 73 - "total": 43, 74 - }, 75 - "statements": { 76 - "covered": 35, 77 - "pct": 81.39, 78 - "skipped": 0, 79 - "total": 43, 80 - }, 81 - } 82 - `) 83 - } 35 + } 36 + else if (isV8Provider()) { 37 + expect(coverageMap).toMatchInlineSnapshot(` 38 + { 39 + "branches": "5/6 (83.33%)", 40 + "functions": "3/5 (60%)", 41 + "lines": "35/43 (81.39%)", 42 + "statements": "35/43 (81.39%)", 43 + } 44 + `) 84 45 } 85 46 else { 86 - expect(summary).toMatchInlineSnapshot(` 47 + expect(coverageMap).toMatchInlineSnapshot(` 87 48 { 88 - "branches": { 89 - "covered": 5, 90 - "pct": 83.33, 91 - "skipped": 0, 92 - "total": 6, 93 - }, 94 - "branchesTrue": { 95 - "covered": 0, 96 - "pct": "Unknown", 97 - "skipped": 0, 98 - "total": 0, 99 - }, 100 - "functions": { 101 - "covered": 5, 102 - "pct": 71.42, 103 - "skipped": 0, 104 - "total": 7, 105 - }, 106 - "lines": { 107 - "covered": 13, 108 - "pct": 81.25, 109 - "skipped": 0, 110 - "total": 16, 111 - }, 112 - "statements": { 113 - "covered": 14, 114 - "pct": 82.35, 115 - "skipped": 0, 116 - "total": 17, 117 - }, 49 + "branches": "5/6 (83.33%)", 50 + "functions": "5/7 (71.42%)", 51 + "lines": "13/16 (81.25%)", 52 + "statements": "14/17 (82.35%)", 118 53 } 119 54 `) 120 55 }
+3
test/coverage-test/fixtures/configs/vitest.config.ts
··· 3 3 4 4 export default defineConfig({ 5 5 plugins: [vue()], 6 + optimizeDeps: { 7 + include: ["vue"] 8 + } 6 9 })