Skip to content

Commit

Permalink
test(coverage): format snapshots with formatSummary (#7482)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Feb 13, 2025
1 parent 5e21181 commit cb48e64
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 160 deletions.
3 changes: 3 additions & 0 deletions test/coverage-test/fixtures/configs/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
plugins: [vue()],
optimizeDeps: {
include: ["vue"]
}
})
29 changes: 29 additions & 0 deletions test/coverage-test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { CoverageMap, FileCoverage } from 'istanbul-lib-coverage'
import { expect } from 'vitest'
import { formatSummary } from './utils'

expect.addSnapshotSerializer({
test: val => val.constructor.name === 'CoverageMap',
serialize: (val: CoverageMap, config, indentation, depth, refs, printer) => {
return printer(formatSummary(val.getCoverageSummary()), config, indentation, depth, refs)
},
})

expect.addSnapshotSerializer({
test: val => val.constructor.name === 'FileCoverage',
serialize: (val: FileCoverage, config, indentation, depth, refs, printer) => {
return printer(formatSummary(val.toSummary()), config, indentation, depth, refs)
},
})

expect.addSnapshotSerializer({
test: val => Array.isArray(val) && val.every(entry => entry.constructor.name === 'FileCoverage'),
serialize: (val: FileCoverage[], config, indentation, depth, refs, printer) => {
const summary = val.reduce((all, current) => ({
...all,
[current.path]: formatSummary(current.toSummary()),
}), {})

return printer(summary, config, indentation, depth, refs)
},
})
44 changes: 39 additions & 5 deletions test/coverage-test/test/changed.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { beforeAll, expect } from 'vitest'
import { readCoverageMap, runVitest, test } from '../utils'
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'

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

const uncoveredFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/new-uncovered-file.ts').toSummary()
expect(uncoveredFile.lines.pct).toBe(0)
const uncoveredFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/new-uncovered-file.ts')
const changedFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/file-to-change.ts')

const changedFile = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/file-to-change.ts').toSummary()
expect(changedFile.lines.pct).toBeGreaterThanOrEqual(50)
if (isV8Provider()) {
expect([uncoveredFile, changedFile]).toMatchInlineSnapshot(`
{
"<process-cwd>/fixtures/src/file-to-change.ts": {
"branches": "1/1 (100%)",
"functions": "1/2 (50%)",
"lines": "4/6 (66.66%)",
"statements": "4/6 (66.66%)",
},
"<process-cwd>/fixtures/src/new-uncovered-file.ts": {
"branches": "1/1 (100%)",
"functions": "1/1 (100%)",
"lines": "0/3 (0%)",
"statements": "0/3 (0%)",
},
}
`)
}
else {
expect([uncoveredFile, changedFile]).toMatchInlineSnapshot(`
{
"<process-cwd>/fixtures/src/file-to-change.ts": {
"branches": "0/0 (100%)",
"functions": "1/2 (50%)",
"lines": "1/2 (50%)",
"statements": "1/2 (50%)",
},
"<process-cwd>/fixtures/src/new-uncovered-file.ts": {
"branches": "0/0 (100%)",
"functions": "0/1 (0%)",
"lines": "0/1 (0%)",
"statements": "0/1 (0%)",
},
}
`)
}
}, SKIP)
61 changes: 10 additions & 51 deletions test/coverage-test/test/file-outside-vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,24 @@ test('does not crash when file outside Vite is loaded (#5639)', async () => {

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

if (isV8Provider()) {
expect(summary).toMatchInlineSnapshot(`
expect(fileCoverage).toMatchInlineSnapshot(`
{
"branches": {
"covered": 0,
"pct": 100,
"skipped": 0,
"total": 0,
},
"functions": {
"covered": 0,
"pct": 0,
"skipped": 0,
"total": 1,
},
"lines": {
"covered": 1,
"pct": 100,
"skipped": 0,
"total": 1,
},
"statements": {
"covered": 1,
"pct": 100,
"skipped": 0,
"total": 1,
},
"branches": "0/0 (100%)",
"functions": "0/1 (0%)",
"lines": "1/1 (100%)",
"statements": "1/1 (100%)",
}
`)
}
else {
expect(summary).toMatchInlineSnapshot(`
expect(fileCoverage).toMatchInlineSnapshot(`
{
"branches": {
"covered": 0,
"pct": 100,
"skipped": 0,
"total": 0,
},
"functions": {
"covered": 0,
"pct": 0,
"skipped": 0,
"total": 1,
},
"lines": {
"covered": 0,
"pct": 0,
"skipped": 0,
"total": 1,
},
"statements": {
"covered": 0,
"pct": 0,
"skipped": 0,
"total": 1,
},
"branches": "0/0 (100%)",
"functions": "0/1 (0%)",
"lines": "0/1 (0%)",
"statements": "0/1 (0%)",
}
`)
}
Expand Down
4 changes: 2 additions & 2 deletions test/coverage-test/test/include-exclude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('default exclude should ignore test files', async () => {
})

const coverageMap = await readCoverageMap()
expect(coverageMap.files()).toMatchInlineSnapshot(`[]`)
expect(coverageMap.files()).toMatchInlineSnapshot(`{}`)
})

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

const coverageMap = await readCoverageMap()
expect(coverageMap.files()).toMatchInlineSnapshot(`[]`)
expect(coverageMap.files()).toMatchInlineSnapshot(`{}`)
})

test('test file is excluded from report when excludes is not set', async () => {
Expand Down
53 changes: 41 additions & 12 deletions test/coverage-test/test/isolation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TestSpecification } from 'vitest/node'
import { expect, test } from 'vitest'
import { readCoverageMap, runVitest } from '../utils'
import { formatSummary, isV8Provider, readCoverageMap, runVitest } from '../utils'

const pools = ['forks']

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

coverage: {
all: false,
reporter: ['json', 'html'],
reporter: 'json',
},

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

const coverageMap = await readCoverageMap()

const branches = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/branch.ts')
expect(branches.toSummary().lines.pct).toBe(100)
expect(branches.toSummary().statements.pct).toBe(100)
expect(branches.toSummary().functions.pct).toBe(100)
expect(branches.toSummary().branches.pct).toBe(100)

const math = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/math.ts')
expect(math.toSummary().lines.pct).toBe(100)
expect(math.toSummary().statements.pct).toBe(100)
expect(math.toSummary().functions.pct).toBe(100)
expect(math.toSummary().branches.pct).toBe(100)

const summary = {
[branches.path]: formatSummary(branches.toSummary()),
[math.path]: formatSummary(math.toSummary()),
}

if (isV8Provider()) {
expect(summary).toStrictEqual({
'<process-cwd>/fixtures/src/branch.ts': {
branches: '3/3 (100%)',
functions: '1/1 (100%)',
lines: '6/6 (100%)',
statements: '6/6 (100%)',
},
'<process-cwd>/fixtures/src/math.ts': {
branches: '4/4 (100%)',
functions: '4/4 (100%)',
lines: '12/12 (100%)',
statements: '12/12 (100%)',
},
})
}
else {
expect(summary).toStrictEqual({
'<process-cwd>/fixtures/src/branch.ts': {
branches: '2/2 (100%)',
functions: '1/1 (100%)',
lines: '4/4 (100%)',
statements: '4/4 (100%)',
},
'<process-cwd>/fixtures/src/math.ts': {
branches: '0/0 (100%)',
functions: '4/4 (100%)',
lines: '4/4 (100%)',
statements: '4/4 (100%)',
},
},
)
}
})
}
}
Expand Down
25 changes: 22 additions & 3 deletions test/coverage-test/test/setup-files.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'node:path'
import { expect } from 'vitest'
import { readCoverageMap, runVitest, test } from '../utils'
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'

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

// Some valid coverage should be reported
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/math.ts')
expect(fileCoverage.toSummary().functions.covered).toBe(1)
expect(fileCoverage.toSummary().functions.pct).toBeLessThanOrEqual(25)

if (isV8Provider()) {
expect(fileCoverage).toMatchInlineSnapshot(`
{
"branches": "1/1 (100%)",
"functions": "1/4 (25%)",
"lines": "6/12 (50%)",
"statements": "6/12 (50%)",
}
`)
}
else {
expect(fileCoverage).toMatchInlineSnapshot(`
{
"branches": "0/0 (100%)",
"functions": "1/4 (25%)",
"lines": "1/4 (25%)",
"statements": "1/4 (25%)",
}
`)
}
})
Loading

0 comments on commit cb48e64

Please sign in to comment.