Skip to content

Commit

Permalink
feat(globals): configure globals to tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nikovirtala committed Dec 30, 2024
1 parent 110d5e7 commit 48e8c4d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
15 changes: 14 additions & 1 deletion API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down
54 changes: 50 additions & 4 deletions test/vitest.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 48e8c4d

Please sign in to comment.