diff --git a/API.md b/API.md
index 8e72d09..9c1c740 100644
--- a/API.md
+++ b/API.md
@@ -44,6 +44,7 @@ new Vitest(project: NodeProject, options?: VitestOptions)
| configureCoverageProvider
| *No description.* |
| configureCoverageReporters
| *No description.* |
| configureEnvironment
| *No description.* |
+| configureGlobals
| *No description.* |
---
@@ -141,6 +142,18 @@ public configureEnvironment(environment: Environment): void
---
+##### `configureGlobals`
+
+```typescript
+public configureGlobals(globals: boolean): void
+```
+
+###### `globals`Required
+
+- *Type:* boolean
+
+---
+
#### Static Functions
| **Name** | **Description** |
@@ -486,7 +499,7 @@ public readonly typecheckEnabled: boolean;
```
- *Type:* boolean
-- *Default:* true
+- *Default:* true (for TypeScript projects)
Enable typechecking alongside your regular tests.
diff --git a/src/index.ts b/src/index.ts
index 23e2193..00ef8a8 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -188,7 +188,7 @@ export interface VitestConfigOptions {
/**
* Enable typechecking alongside your regular tests. https://vitest.dev/config/#typecheck-enabled
*
- * @default true
+ * @default true (for TypeScript projects)
*/
readonly typecheckEnabled?: boolean;
@@ -281,7 +281,7 @@ 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.typecheckEnabled = options.config?.typecheckEnabled ?? this.isTypescriptProject();
this.typecheckChecker = options.config?.typecheckChecker ?? "tsc --noEmit";
this.typecheckTsconfig = options.config?.typecheckTsconfig ?? "tsconfig.dev.json";
this.passWithNoTests = options.config?.passWithNoTests ?? true;
@@ -296,6 +296,7 @@ export class Vitest extends Component {
project.addDevDeps(`vitest@${this.version}`);
this.configureCoverageProvider(this.coverageProvider);
+ this.updateTsConfig(this.globals);
this.addTestCommand();
this.synthesizeConfig();
}
@@ -342,6 +343,32 @@ export class Vitest extends Component {
this.synthesizeConfig();
}
+ public configureGlobals(globals: boolean): void {
+ this.globals = globals;
+ this.updateTsConfig(globals);
+ this.synthesizeConfig();
+ }
+
+ private updateTsConfig(globals: boolean): void {
+ if (!this.isTypescriptProject()) {
+ return;
+ }
+
+ const tsconfig = this.project.tryFindObjectFile(this.typecheckTsconfig);
+
+ if (!tsconfig) {
+ throw new Error("unable to find tsconfig");
+ }
+
+ if (globals) {
+ tsconfig.addToArray("compilerOptions.types", "vitest/globals");
+ }
+ }
+
+ private isTypescriptProject(): boolean {
+ return this.project.deps.tryGetDependency("typescript") !== undefined;
+ }
+
private addTestCommand(): void {
this.project.testTask.exec("vitest run", {
receiveArgs: true,
diff --git a/test/vitest.test.ts b/test/vitest.test.ts
index 65c090e..7de9778 100644
--- a/test/vitest.test.ts
+++ b/test/vitest.test.ts
@@ -1,10 +1,11 @@
-import { NodeProject } from "projen/lib/javascript";
+import { JsonFile } from "projen/lib/json";
+import { TypeScriptProject } from "projen/lib/typescript";
import { synthSnapshot } from "projen/lib/util/synth";
import { Vitest, Environment, CoverageProvider, CoverageReporter, Pool } from "../src";
describe("jest", () => {
test("throws when jest is enabled", () => {
- const project = new NodeProject({
+ const project = new TypeScriptProject({
name: "test-node-project",
mergify: false,
projenDevDependency: false,
@@ -17,10 +18,10 @@ describe("jest", () => {
});
describe("vitest", () => {
- let project: NodeProject;
+ let project: TypeScriptProject;
beforeEach(() => {
- project = new NodeProject({
+ project = new TypeScriptProject({
name: "test-node-project",
mergify: false,
projenDevDependency: false,
@@ -90,6 +91,15 @@ describe("vitest", () => {
});
test("initialize with custom options", () => {
+ new JsonFile(project, "tsconfig.custom.json", {
+ obj: {
+ compilerOptions: {
+ types: [],
+ },
+ },
+ readonly: false,
+ });
+
new Vitest(project, {
configFilePath: "custom.vitest.config.ts",
vitestVersion: "^3",
@@ -131,6 +141,42 @@ describe("vitest", () => {
expect(snapshot["package.json"].devDependencies.vitest).toBe("^3");
});
+ describe("globals", () => {
+ test("adds vitest/globals to tsconfig when globals enabled", () => {
+ new Vitest(project, {
+ config: {
+ globals: true,
+ },
+ });
+
+ const snapshot = synthSnapshot(project);
+ expect(snapshot["tsconfig.dev.json"].compilerOptions.types).toContain("vitest/globals");
+ });
+
+ test("does not add vitest/globals to tsconfig when globals disabled", () => {
+ new Vitest(project);
+
+ const snapshot = synthSnapshot(project);
+ expect(snapshot["tsconfig.dev.json"].compilerOptions.types || []).not.toContain("vitest/globals");
+ });
+
+ test("updates tsconfig when globals configuration changes (false --> true)", () => {
+ const vitest = new Vitest(project);
+
+ vitest.configureGlobals(true);
+ const snapshot = synthSnapshot(project);
+ expect(snapshot["tsconfig.dev.json"].compilerOptions.types).toContain("vitest/globals");
+ });
+
+ test("updates tsconfig when globals configuration changes (true --> false)", () => {
+ const vitest = new Vitest(project);
+
+ vitest.configureGlobals(false);
+ const snapshot = synthSnapshot(project);
+ expect(snapshot["tsconfig.dev.json"].compilerOptions.types || []).not.toContain("vitest/globals");
+ });
+ });
+
describe("of", () => {
test("returns vitest instance when found", () => {
const vitest = new Vitest(project);