Skip to content

Commit 713a00d

Browse files
fix: deduplicate Vitest excludes entries (#2083)
## PR Checklist - [x] Addresses an existing open issue: fixes #2082 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Also fixes a yet-to-be-hit-by-users bug where only having `"node_modules"` there results in `[, "node_modules"]`. 🎁
1 parent d0f4cd8 commit 713a00d

File tree

2 files changed

+253
-5
lines changed

2 files changed

+253
-5
lines changed

src/blocks/blockVitest.test.ts

+249-3
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe("blockVitest", () => {
236236
include: undefined,
237237
reporter: ["html", "lcov"],
238238
},
239-
exclude: [, "node_modules"],
239+
exclude: ["node_modules"],
240240
setupFiles: ["console-fail-test/setup"],
241241
},
242242
});
@@ -504,7 +504,7 @@ describe("blockVitest", () => {
504504
include: undefined,
505505
reporter: ["html", "lcov"],
506506
},
507-
exclude: [, "node_modules"],
507+
exclude: ["node_modules"],
508508
setupFiles: ["console-fail-test/setup"],
509509
},
510510
});
@@ -750,7 +750,253 @@ describe("blockVitest", () => {
750750
include: ["src/"],
751751
reporter: ["html", "lcov"],
752752
},
753-
exclude: ["lib/", "node_modules"],
753+
exclude: ["lib/","node_modules"],
754+
setupFiles: ["console-fail-test/setup"],
755+
},
756+
});
757+
",
758+
},
759+
}
760+
`);
761+
});
762+
763+
test("with duplicate excludes addons", () => {
764+
const creation = testBlock(blockVitest, {
765+
addons: {
766+
coverage: {
767+
exclude: ["other"],
768+
include: ["src/"],
769+
},
770+
exclude: ["lib/", "node_modules", "node_modules"],
771+
flags: ["--typecheck"],
772+
},
773+
options: optionsBase,
774+
});
775+
776+
expect(creation).toMatchInlineSnapshot(`
777+
{
778+
"addons": [
779+
{
780+
"addons": {
781+
"ignores": [
782+
"coverage",
783+
],
784+
},
785+
"block": [Function],
786+
},
787+
{
788+
"addons": {
789+
"sections": {
790+
"Testing": {
791+
"contents": "
792+
[Vitest](https://vitest.dev) is used for tests.
793+
You can run it locally on the command-line:
794+
795+
\`\`\`shell
796+
pnpm run test
797+
\`\`\`
798+
799+
Add the \`--coverage\` flag to compute test coverage and place reports in the \`coverage/\` directory:
800+
801+
\`\`\`shell
802+
pnpm run test --coverage
803+
\`\`\`
804+
805+
Note that [console-fail-test](https://github.com/JoshuaKGoldberg/console-fail-test) is enabled for all test runs.
806+
Calls to \`console.log\`, \`console.warn\`, and other console methods will cause a test to fail.
807+
808+
809+
",
810+
},
811+
},
812+
},
813+
"block": [Function],
814+
},
815+
{
816+
"addons": {
817+
"extensions": [
818+
{
819+
"extends": [
820+
"vitest.configs.recommended",
821+
],
822+
"files": [
823+
"**/*.test.*",
824+
],
825+
"rules": [
826+
{
827+
"entries": {
828+
"@typescript-eslint/no-unsafe-assignment": "off",
829+
},
830+
},
831+
],
832+
},
833+
],
834+
"ignores": [
835+
"coverage",
836+
"**/*.snap",
837+
],
838+
"imports": [
839+
{
840+
"source": "@vitest/eslint-plugin",
841+
"specifier": "vitest",
842+
},
843+
],
844+
"settings": {
845+
"vitest": {
846+
"typecheck": true,
847+
},
848+
},
849+
},
850+
"block": [Function],
851+
},
852+
{
853+
"addons": {
854+
"files": {
855+
"greet.test.ts": "import { describe, expect, it, vi } from "vitest";
856+
857+
import { greet } from "./greet.js";
858+
859+
const message = "Yay, testing!";
860+
861+
describe(greet, () => {
862+
it("logs to the console once when message is provided as a string", () => {
863+
const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);
864+
865+
greet(message);
866+
867+
expect(logger).toHaveBeenCalledWith(message);
868+
expect(logger).toHaveBeenCalledTimes(1);
869+
});
870+
871+
it("logs to the console once when message is provided as an object", () => {
872+
const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);
873+
874+
greet({ message });
875+
876+
expect(logger).toHaveBeenCalledWith(message);
877+
expect(logger).toHaveBeenCalledTimes(1);
878+
});
879+
880+
it("logs once when times is not provided in an object", () => {
881+
const logger = vi.fn();
882+
883+
greet({ logger, message });
884+
885+
expect(logger).toHaveBeenCalledWith(message);
886+
expect(logger).toHaveBeenCalledTimes(1);
887+
});
888+
889+
it("logs a specified number of times when times is provided", () => {
890+
const logger = vi.fn();
891+
const times = 7;
892+
893+
greet({ logger, message, times });
894+
895+
expect(logger).toHaveBeenCalledWith(message);
896+
expect(logger).toHaveBeenCalledTimes(7);
897+
});
898+
});
899+
",
900+
},
901+
},
902+
"block": [Function],
903+
},
904+
{
905+
"addons": {
906+
"ignores": [
907+
"/coverage",
908+
],
909+
},
910+
"block": [Function],
911+
},
912+
{
913+
"addons": {
914+
"jobs": [
915+
{
916+
"name": "Test",
917+
"steps": [
918+
{
919+
"run": "pnpm run test --coverage",
920+
},
921+
],
922+
},
923+
],
924+
},
925+
"block": [Function],
926+
},
927+
{
928+
"addons": {
929+
"properties": {
930+
"devDependencies": {
931+
"@vitest/coverage-v8": "3.0.9",
932+
"@vitest/eslint-plugin": "1.1.38",
933+
"console-fail-test": "0.5.0",
934+
"vitest": "3.0.9",
935+
},
936+
"scripts": {
937+
"test": "vitest --typecheck",
938+
},
939+
},
940+
},
941+
"block": [Function],
942+
},
943+
{
944+
"addons": {
945+
"ignores": [
946+
"/coverage",
947+
],
948+
},
949+
"block": [Function],
950+
},
951+
{
952+
"addons": {
953+
"entry": [
954+
"!src/**/*.test.*",
955+
],
956+
},
957+
"block": [Function],
958+
},
959+
{
960+
"addons": {
961+
"debuggers": [
962+
{
963+
"args": [
964+
"run",
965+
"\${relativeFile}",
966+
],
967+
"autoAttachChildProcesses": true,
968+
"console": "integratedTerminal",
969+
"name": "Debug Current Test File",
970+
"program": "\${workspaceRoot}/node_modules/vitest/vitest.mjs",
971+
"request": "launch",
972+
"skipFiles": [
973+
"<node_internals>/**",
974+
"**/node_modules/**",
975+
],
976+
"smartStep": true,
977+
"type": "node",
978+
},
979+
],
980+
"extensions": [
981+
"vitest.explorer",
982+
],
983+
},
984+
"block": [Function],
985+
},
986+
],
987+
"files": {
988+
"vitest.config.ts": "import { defineConfig } from "vitest/config";
989+
990+
export default defineConfig({
991+
test: {
992+
clearMocks: true,
993+
coverage: {
994+
all: true,
995+
exclude: ["other"],
996+
include: ["src/"],
997+
reporter: ["html", "lcov"],
998+
},
999+
exclude: ["lib/","node_modules"],
7541000
setupFiles: ["console-fail-test/setup"],
7551001
},
7561002
});

src/blocks/blockVitest.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export const blockVitest = base.createBlock({
7878
},
7979
produce({ addons }) {
8080
const { actionSteps, coverage, exclude = [] } = addons;
81-
const excludeText = JSON.stringify(exclude);
81+
const excludeText = JSON.stringify(
82+
Array.from(new Set(["node_modules", ...exclude])).sort(),
83+
);
8284

8385
return {
8486
addons: [
@@ -242,7 +244,7 @@ export default defineConfig({
242244
}include: ${JSON.stringify(coverage.include)},
243245
reporter: ["html", "lcov"],
244246
},
245-
exclude: [${excludeText.slice(1, excludeText.length - 1)}, "node_modules"],
247+
exclude: [${excludeText.slice(1, excludeText.length - 1)}],
246248
setupFiles: ["console-fail-test/setup"],
247249
},
248250
});

0 commit comments

Comments
 (0)