forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(coverage): custom provider tests for vitest-dev#2797
- Loading branch information
1 parent
edf1dd4
commit 461e96f
Showing
7 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`custom json report 1`] = ` | ||
{ | ||
"calls": [ | ||
"initialize with context", | ||
"clean with force", | ||
"onBeforeFilesRun", | ||
"onAfterSuiteRun with {}", | ||
"onAfterSuiteRun with {}", | ||
"onAfterSuiteRun with {}", | ||
"reportCoverage with {\\"allTestsRun\\":true}", | ||
], | ||
"transformedFiles": [ | ||
"<process-cwd>/src/Counter/Counter.component.ts", | ||
"<process-cwd>/src/Counter/Counter.component.ts?vue&type=script&src=true&lang.ts", | ||
"<process-cwd>/src/Counter/Counter.vue", | ||
"<process-cwd>/src/Counter/index.ts", | ||
"<process-cwd>/src/Defined.vue", | ||
"<process-cwd>/src/Defined.vue?vue&type=style&index=0&scoped=fdf5cd5f&lang.css", | ||
"<process-cwd>/src/Hello.vue", | ||
"<process-cwd>/src/another-setup.ts", | ||
"<process-cwd>/src/implicitElse.ts", | ||
"<process-cwd>/src/importEnv.ts", | ||
"<process-cwd>/src/index.mts", | ||
"<process-cwd>/src/utils.ts", | ||
], | ||
} | ||
`; |
12 changes: 12 additions & 0 deletions
12
test/coverage-test/coverage-report-tests/custom.report.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Custom coverage provider specific test cases | ||
*/ | ||
|
||
import { readFileSync } from 'fs' | ||
import { expect, test } from 'vitest' | ||
|
||
test('custom json report', async () => { | ||
const report = readFileSync('./coverage/custom-coverage-provider-report.json', 'utf-8') | ||
|
||
expect(JSON.parse(report)).toMatchSnapshot() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs' | ||
import type { AfterSuiteRunMeta, CoverageProvider, CoverageProviderModule, ReportContext, ResolvedCoverageOptions, Vitest } from 'vitest' | ||
import { coverageConfigDefaults } from 'vitest/config' | ||
import { normalizeFilename } from './coverage-report-tests/utils' | ||
|
||
/** | ||
* Provider that simply keeps track of the function that were called | ||
*/ | ||
class CustomCoverageProvider implements CoverageProvider { | ||
name = 'custom-coverage-provider' | ||
|
||
calls: string[] = [] | ||
transformedFiles: string[] = [] | ||
|
||
initialize(ctx: Vitest) { | ||
this.calls.push(`initialize ${ctx ? 'with' : 'without'} context`) | ||
} | ||
|
||
clean(force: boolean) { | ||
this.calls.push(`clean ${force ? 'with' : 'without'} force`) | ||
} | ||
|
||
onBeforeFilesRun() { | ||
this.calls.push('onBeforeFilesRun') | ||
} | ||
|
||
onAfterSuiteRun(meta: AfterSuiteRunMeta) { | ||
this.calls.push(`onAfterSuiteRun with ${JSON.stringify(meta)}`) | ||
} | ||
|
||
reportCoverage(reportContext?: ReportContext) { | ||
this.calls.push(`reportCoverage with ${JSON.stringify(reportContext)}`) | ||
|
||
const jsonReport = JSON.stringify({ | ||
calls: this.calls, | ||
transformedFiles: this.transformedFiles.sort(), | ||
}, null, 2) | ||
|
||
if (existsSync('./coverage')) | ||
rmSync('./coverage', { maxRetries: 10, recursive: true }) | ||
|
||
mkdirSync('./coverage') | ||
writeFileSync('./coverage/custom-coverage-provider-report.json', jsonReport, 'utf-8') | ||
} | ||
|
||
onFileTransform(code: string, id: string) { | ||
const filename = normalizeFilename(id) | ||
|
||
if (/\/src\//.test(filename)) | ||
this.transformedFiles.push(filename) | ||
|
||
return { code } | ||
} | ||
|
||
resolveOptions(): ResolvedCoverageOptions { | ||
return { | ||
...coverageConfigDefaults, | ||
enabled: true, | ||
|
||
// TODO: Fix. This does not pass to workers | ||
provider: new CustomCoverageProviderModule(), | ||
|
||
} | ||
} | ||
} | ||
|
||
export default class CustomCoverageProviderModule implements CoverageProviderModule { | ||
getProvider(): CoverageProvider { | ||
return new CustomCoverageProvider() | ||
} | ||
|
||
takeCoverage() { | ||
return { customCoverage: 'Custom coverage report from CustomCoverageProviderModule' } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters