Skip to content

feat: add intake method to blockVitest #2079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"input-from-file-json": "^0.5.4",
"input-from-script": "^0.5.4",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"lazy-value": "^3.0.0",
"lodash": "^4.17.21",
"npm-user": "^6.1.1",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

111 changes: 109 additions & 2 deletions src/blocks/blockVitest.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { testBlock } from "bingo-stratum-testers";
import { describe, expect, test, vi } from "vitest";
import { testBlock, testIntake } from "bingo-stratum-testers";
import { describe, expect, it, test, vi } from "vitest";

import { blockVitest } from "./blockVitest.js";
import { optionsBase } from "./options.fakes.js";
Expand Down Expand Up @@ -759,4 +759,111 @@ describe("blockVitest", () => {
}
`);
});

describe("intake", () => {
it("returns undefined when vitest.config.ts does not exist", () => {
const actual = testIntake(blockVitest, {
files: {
src: {},
},
});

expect(actual).toEqual(undefined);
});

it("returns undefined when vitest.config.ts does not contain the expected defineConfig", () => {
const actual = testIntake(blockVitest, {
files: {
"vitest.config.ts": [`invalid`],
},
});

expect(actual).toEqual(undefined);
});

it("returns undefined when vitest.config.ts passes a non-object to defineConfig", () => {
const actual = testIntake(blockVitest, {
files: {
"vitest.config.ts": [`defineConfig("invalid")`],
},
});

expect(actual).toEqual(undefined);
});

it("returns undefined when vitest.config.ts does not pass a test to defineConfig", () => {
const actual = testIntake(blockVitest, {
files: {
"vitest.config.ts": [`defineConfig({ other: true })`],
},
});

expect(actual).toEqual(undefined);
});

it("returns undefined when vitest.config.ts passes unknown test data to defineConfig", () => {
const actual = testIntake(blockVitest, {
files: {
"vitest.config.ts": [`defineConfig({ test: true })`],
},
});

expect(actual).toEqual(undefined);
});

it("returns undefined when vitest.config.ts passes invalid test syntax to defineConfig", () => {
const actual = testIntake(blockVitest, {
files: {
"vitest.config.ts": [`defineConfig({ test: { ! } })`],
},
});

expect(actual).toEqual(undefined);
});

it("returns undefined when vitest.config.ts passes invalid test data to defineConfig", () => {
const actual = testIntake(blockVitest, {
files: {
"vitest.config.ts": [
`defineConfig({ test: { coverage: 'invalid' } })`,
],
},
});

expect(actual).toEqual(undefined);
});

it("returns coverage and exclude when they exist in vitest.config.ts", () => {
const actual = testIntake(blockVitest, {
files: {
"vitest.config.ts": [
`import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
clearMocks: true,
coverage: {
all: true,
exclude: ["src/index.ts"],
include: ["src", "other"],
reporter: ["html", "lcov"],
},
exclude: ["lib", "node_modules"],
setupFiles: ["console-fail-test/setup"],
},
});
`,
],
},
});

expect(actual).toEqual({
coverage: {
exclude: ["src/index.ts"],
include: ["src", "other"],
},
exclude: ["lib", "node_modules"],
});
});
});
});
59 changes: 52 additions & 7 deletions src/blocks/blockVitest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import JSON5 from "json5";
import { z } from "zod";

import { base } from "../base.js";
Expand All @@ -15,22 +16,66 @@ import { blockRemoveFiles } from "./blockRemoveFiles.js";
import { blockRemoveWorkflows } from "./blockRemoveWorkflows.js";
import { blockTSup } from "./blockTSup.js";
import { blockVSCode } from "./blockVSCode.js";
import { intakeFile } from "./intake/intakeFile.js";

function tryParseJSON5(text: string) {
try {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return JSON5.parse(text) as Record<string, unknown> | undefined;
} catch {
return undefined;
}
}

const zCoverage = z.object({
exclude: z.array(z.string()).optional(),
include: z.array(z.string()).optional(),
});

const zExclude = z.array(z.string());

const zTest = z.object({
coverage: zCoverage,
exclude: zExclude,
});

export const blockVitest = base.createBlock({
about: {
name: "Vitest",
},
addons: {
actionSteps: z.array(zActionStep).default([]),
coverage: z
.object({
exclude: z.array(z.string()).optional(),
include: z.array(z.string()).optional(),
})
.default({}),
exclude: z.array(z.string()).default([]),
coverage: zCoverage.default({}),
exclude: zExclude.default([]),
flags: z.array(z.string()).default([]),
},
intake({ files }) {
const file = intakeFile(files, ["vitest.config.ts"]);
if (!file) {
return undefined;
}

const normalized = file[0].replaceAll(/[\n\r]/g, "");
const matched = /defineConfig\(\{(.+)\}\)\s*(?:;\s*)?$/u.exec(normalized);
if (!matched) {
return undefined;
}

const rawData = tryParseJSON5(`{${matched[1]}}`);
if (typeof rawData !== "object" || typeof rawData.test !== "object") {
return undefined;
}

const parsedData = zTest.safeParse(rawData.test).data;
if (!parsedData) {
return undefined;
}

return {
coverage: parsedData.coverage,
exclude: parsedData.exclude,
};
},
produce({ addons }) {
const { actionSteps, coverage, exclude = [] } = addons;
const excludeText = JSON.stringify(exclude);
Expand Down