From 7970f358c1fdd86fb230359eac348f093ec38fe4 Mon Sep 17 00:00:00 2001 From: Niko Virtala Date: Sun, 29 Dec 2024 19:58:04 +0200 Subject: [PATCH] feat: typecheck --- API.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 36 +++++++++++++++++++++++++++++++++- test/vitest.test.ts | 5 +++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index 18ca5ab..2da2bbb 100644 --- a/API.md +++ b/API.md @@ -275,6 +275,9 @@ const vitestConfigOptions: VitestConfigOptions = { ... } | include | string[] | A list of glob patterns that match your test files. | | isolate | boolean | Run tests in an isolated environment. This option has no effect on vmThreads pool. | | pool | Pool | Pool used to run tests in. | +| typecheckChecker | string | Tool to use for type checking. Checker should implement the same output format as `tsc`. | +| typecheckEnabled | boolean | Enable typechecking alongside your regular tests. | +| typecheckTsconfig | string | Path to custom tsconfig, relative to the project root. | --- @@ -429,6 +432,51 @@ https://vitest.dev/config/#pool --- +##### `typecheckChecker`Optional + +```typescript +public readonly typecheckChecker: string; +``` + +- *Type:* string +- *Default:* "tsc --noEmit" + +Tool to use for type checking. Checker should implement the same output format as `tsc`. + +https://vitest.dev/config/#typecheck-checker + +--- + +##### `typecheckEnabled`Optional + +```typescript +public readonly typecheckEnabled: boolean; +``` + +- *Type:* boolean +- *Default:* true + +Enable typechecking alongside your regular tests. + +https://vitest.dev/config/#typecheck-enabled + +--- + +##### `typecheckTsconfig`Optional + +```typescript +public readonly typecheckTsconfig: string; +``` + +- *Type:* string +- *Default:* "tsconfig.dev.json" + +Path to custom tsconfig, relative to the project root. + +https://vitest.dev/config/#typecheck-tsconfig + +--- + ### VitestOptions #### Initializer diff --git a/src/index.ts b/src/index.ts index f7d2d80..62c1f2c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -184,6 +184,29 @@ export interface VitestConfigOptions { * @default "coverage" */ readonly coverageDirectory?: string; + + /** + * Enable typechecking alongside your regular tests. https://vitest.dev/config/#typecheck-enabled + * + * @default true + */ + readonly typecheckEnabled?: boolean; + + /** + * Tool to use for type checking. Checker should implement the same output format as `tsc`. + * + * https://vitest.dev/config/#typecheck-checker + * + * @default "tsc --noEmit" + */ + readonly typecheckChecker?: string; + + /** + * Path to custom tsconfig, relative to the project root. https://vitest.dev/config/#typecheck-tsconfig + * + * @default "tsconfig.dev.json" + */ + readonly typecheckTsconfig?: string; } export interface VitestOptions { @@ -219,6 +242,9 @@ export class Vitest extends Component { private readonly isolate: boolean; private readonly pool: Pool; private readonly coverageEnabled: boolean; + private readonly typecheckEnabled: boolean; + private readonly typecheckChecker: string; + private readonly typecheckTsconfig: string; private environment: string; private globals: boolean; private coverageProvider: CoverageProvider; @@ -239,6 +265,9 @@ export class Vitest extends Component { this.isolate = options.config?.isolate ?? true; this.pool = options.config?.pool ?? Pool.FORKS; this.coverageEnabled = options.config?.coverageEnabled ?? true; + this.typecheckEnabled = options.config?.typecheckEnabled ?? true; + this.typecheckChecker = options.config?.typecheckChecker ?? "tsc --noEmit"; + this.typecheckTsconfig = options.config?.typecheckTsconfig ?? "tsconfig.dev.json"; this.environment = options.config?.environment ?? Environment.NODE; this.globals = options.config?.globals ?? false; this.coverageProvider = options.config?.coverageProvider ?? CoverageProvider.V8; @@ -343,7 +372,12 @@ export class Vitest extends Component { lines.push(` globals: ${this.globals},`); lines.push(` include: ${JSON.stringify(Array.from(this.include))},`); lines.push(` isolate: ${this.isolate},`); - lines.push(` pool: "${this.pool}", `); + lines.push(` pool: "${this.pool}",`); + lines.push(" typecheck: {"); + lines.push(` checker: "${this.typecheckChecker}",`); + lines.push(` enabled: ${this.typecheckEnabled},`); + lines.push(` tsconfig: "${this.typecheckTsconfig}",`); + lines.push(" },"); return lines; } diff --git a/test/vitest.test.ts b/test/vitest.test.ts index 4249075..07dbf4d 100644 --- a/test/vitest.test.ts +++ b/test/vitest.test.ts @@ -104,6 +104,9 @@ describe("vitest", () => { coverageDirectory: "custom-coverage", include: ["**/*.test.ts"], exclude: ["**/*.spec.js"], + typecheckEnabled: false, + typecheckChecker: "tsc", + typecheckTsconfig: "tsconfig.custom.json", }, }); @@ -114,6 +117,8 @@ describe("vitest", () => { expect(snapshot["custom.vitest.config.ts"]).toContain('pool: "threads"'); expect(snapshot["custom.vitest.config.ts"]).toContain("globals: false"); expect(snapshot["custom.vitest.config.ts"]).toContain("enabled: false"); + expect(snapshot["custom.vitest.config.ts"]).toContain('checker: "tsc"'); + expect(snapshot["custom.vitest.config.ts"]).toContain('tsconfig: "tsconfig.custom.json"'); expect(snapshot["custom.vitest.config.ts"]).toContain('provider: "istanbul"'); expect(snapshot["custom.vitest.config.ts"]).toContain('reporter: ["html"]'); expect(snapshot["custom.vitest.config.ts"]).toContain('reportsDirectory: "custom-coverage"');