Skip to content

Commit cb48e64

Browse files
authored
test(coverage): format snapshots with formatSummary (#7482)
1 parent 5e21181 commit cb48e64

10 files changed

+176
-160
lines changed

test/coverage-test/fixtures/configs/vitest.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ import { defineConfig } from 'vitest/config'
33

44
export default defineConfig({
55
plugins: [vue()],
6+
optimizeDeps: {
7+
include: ["vue"]
8+
}
69
})

test/coverage-test/setup.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
})

test/coverage-test/test/changed.test.ts

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
22
import { resolve } from 'node:path'
33
import { beforeAll, expect } from 'vitest'
4-
import { readCoverageMap, runVitest, test } from '../utils'
4+
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'
55

66
// Note that this test may fail if you have new files in "vitest/test/coverage/src"
77
// and have not yet committed those
@@ -49,9 +49,43 @@ test('{ changed: "HEAD" }', async () => {
4949
]
5050
`)
5151

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')
5454

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+
}
5791
}, SKIP)

test/coverage-test/test/file-outside-vite.test.ts

+10-51
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,24 @@ test('does not crash when file outside Vite is loaded (#5639)', async () => {
1010

1111
const coverageMap = await readCoverageMap()
1212
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/load-outside-vite.cjs')
13-
const summary = fileCoverage.toSummary()
1413

1514
if (isV8Provider()) {
16-
expect(summary).toMatchInlineSnapshot(`
15+
expect(fileCoverage).toMatchInlineSnapshot(`
1716
{
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%)",
4221
}
4322
`)
4423
}
4524
else {
46-
expect(summary).toMatchInlineSnapshot(`
25+
expect(fileCoverage).toMatchInlineSnapshot(`
4726
{
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%)",
7231
}
7332
`)
7433
}

test/coverage-test/test/include-exclude.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('default exclude should ignore test files', async () => {
1313
})
1414

1515
const coverageMap = await readCoverageMap()
16-
expect(coverageMap.files()).toMatchInlineSnapshot(`[]`)
16+
expect(coverageMap.files()).toMatchInlineSnapshot(`{}`)
1717
})
1818

1919
test('overridden exclude should still apply defaults', async () => {
@@ -28,7 +28,7 @@ test('overridden exclude should still apply defaults', async () => {
2828
})
2929

3030
const coverageMap = await readCoverageMap()
31-
expect(coverageMap.files()).toMatchInlineSnapshot(`[]`)
31+
expect(coverageMap.files()).toMatchInlineSnapshot(`{}`)
3232
})
3333

3434
test('test file is excluded from report when excludes is not set', async () => {

test/coverage-test/test/isolation.test.ts

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TestSpecification } from 'vitest/node'
22
import { expect, test } from 'vitest'
3-
import { readCoverageMap, runVitest } from '../utils'
3+
import { formatSummary, isV8Provider, readCoverageMap, runVitest } from '../utils'
44

55
const pools = ['forks']
66

@@ -28,7 +28,7 @@ for (const isolate of [true, false]) {
2828

2929
coverage: {
3030
all: false,
31-
reporter: ['json', 'html'],
31+
reporter: 'json',
3232
},
3333

3434
browser: {
@@ -37,18 +37,47 @@ for (const isolate of [true, false]) {
3737
})
3838

3939
const coverageMap = await readCoverageMap()
40-
4140
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-
4741
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+
}
5281
})
5382
}
5483
}

test/coverage-test/test/setup-files.test.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolve } from 'node:path'
22
import { expect } from 'vitest'
3-
import { readCoverageMap, runVitest, test } from '../utils'
3+
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'
44

55
test('tests with multiple suites are covered (#3514)', async () => {
66
const { stdout } = await runVitest({
@@ -29,6 +29,25 @@ test('tests with multiple suites are covered (#3514)', async () => {
2929

3030
// Some valid coverage should be reported
3131
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+
}
3453
})

0 commit comments

Comments
 (0)