diff --git a/packages/vitest/rollup.config.js b/packages/vitest/rollup.config.js index 9194833a9074..79d833cdfaf4 100644 --- a/packages/vitest/rollup.config.js +++ b/packages/vitest/rollup.config.js @@ -1,6 +1,6 @@ import fs from 'fs' import { builtinModules } from 'module' -import { dirname, join, relative, resolve } from 'pathe' +import { dirname, join, normalize, relative, resolve } from 'pathe' import esbuild from 'rollup-plugin-esbuild' import dts from 'rollup-plugin-dts' import nodeResolve from '@rollup/plugin-node-resolve' @@ -106,7 +106,7 @@ export default ({ watch }) => defineConfig([ input: dtsEntries, output: { dir: 'dist', - entryFileNames: chunk => `${chunk.name.replace('src/', '')}.d.ts`, + entryFileNames: chunk => `${normalize(chunk.name).replace('src/', '')}.d.ts`, format: 'esm', }, external, diff --git a/test/coverage-test/package.json b/test/coverage-test/package.json index f8b11f8b0bd1..90fd498cbe73 100644 --- a/test/coverage-test/package.json +++ b/test/coverage-test/package.json @@ -2,9 +2,10 @@ "name": "@vitest/test-coverage", "private": true, "scripts": { - "test": "pnpm run test:c8 && pnpm run test:istanbul", + "test": "pnpm run test:c8 && pnpm run test:istanbul && pnpm run test:types", "test:c8": "node ./testing.mjs --provider c8", - "test:istanbul": "node ./testing.mjs --provider istanbul" + "test:istanbul": "node ./testing.mjs --provider istanbul", + "test:types": "vitest typecheck --run" }, "devDependencies": { "@vitejs/plugin-vue": "latest", diff --git a/test/coverage-test/test/configuration-options.test-d.ts b/test/coverage-test/test/configuration-options.test-d.ts new file mode 100644 index 000000000000..8bcac13da09d --- /dev/null +++ b/test/coverage-test/test/configuration-options.test-d.ts @@ -0,0 +1,133 @@ +import { assertType, test } from 'vitest' +import type { ResolvedCoverageOptions, Vitest } from 'vitest' +import type { defineConfig } from 'vitest/config' + +type NarrowToTestConfig = T extends { test?: any } ? NonNullable : never +type Configuration = NarrowToTestConfig<(Parameters[0])> +type Coverage = NonNullable + +test('providers, built-in', () => { + assertType({ provider: 'c8' }) + assertType({ provider: 'istanbul' }) + + // @ts-expect-error -- String options must be known built-in's + assertType({ provider: 'unknown-reporter' }) +}) + +test('providers, custom', () => { + assertType({ + provider: { + getProvider() { + return { + name: 'custom-provider', + initialize(_: Vitest) {}, + resolveOptions() { + return {} as ResolvedCoverageOptions + }, + clean(_: boolean) {}, + onBeforeFilesRun() {}, + onAfterSuiteRun({ coverage: _coverage }) {}, + reportCoverage() {}, + onFileTransform(_code: string, _id: string, ctx) { + ctx.getCombinedSourcemap() + }, + } + }, + takeCoverage() {}, + }, + }) +}) + +test('provider options, generic', () => { + assertType({ + provider: 'c8', + enabled: true, + include: ['string'], + }) + + assertType({ + provider: 'istanbul', + enabled: true, + include: ['string'], + }) +}) + +test('provider specific options, c8', () => { + assertType({ + provider: 'c8', + src: ['string'], + 100: true, + excludeNodeModules: false, + allowExternal: true, + }) + + assertType({ + provider: 'c8', + // @ts-expect-error -- Istanbul-only option is not allowed + ignoreClassMethods: ['string'], + }) +}) + +test('provider specific options, istanbul', () => { + assertType({ + provider: 'istanbul', + ignoreClassMethods: ['string'], + watermarks: { + statements: [80, 95], + functions: [80, 95], + branches: [80, 95], + lines: [80, 95], + }, + }) + + assertType({ + provider: 'istanbul', + // @ts-expect-error -- C8-only option is not allowed + src: ['string'], + }) +}) + +test('reporters, single', () => { + assertType({ reporter: 'clover' }) + assertType({ reporter: 'cobertura' }) + assertType({ reporter: 'html-spa' }) + assertType({ reporter: 'html' }) + assertType({ reporter: 'json-summary' }) + assertType({ reporter: 'json' }) + assertType({ reporter: 'lcov' }) + assertType({ reporter: 'lcovonly' }) + assertType({ reporter: 'none' }) + assertType({ reporter: 'teamcity' }) + assertType({ reporter: 'text-lcov' }) + assertType({ reporter: 'text-summary' }) + assertType({ reporter: 'text' }) + + // @ts-expect-error -- String reporters must be known built-in's + assertType({ reporter: 'unknown-reporter' }) +}) + +test('reporters, multiple', () => { + assertType({ + reporter: [ + 'clover', + 'cobertura', + 'html-spa', + 'html', + 'json-summary', + 'json', + 'lcov', + 'lcovonly', + 'none', + 'teamcity', + 'text-lcov', + 'text-summary', + 'text', + ], + }) + + // @ts-expect-error -- List of string reporters must be known built-in's + assertType({ reporter: ['unknown-reporter'] }) + + // @ts-expect-error -- ... and all reporters must be known + assertType({ reporter: ['html', 'json', 'unknown-reporter'] }) +})