|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | +import { sanitizeKnipConfigPatterns } from "../src/utils/sanitize-knip-config-patterns.js"; |
| 3 | + |
| 4 | +describe("sanitizeKnipConfigPatterns", () => { |
| 5 | + it("removes empty string values at the top level", () => { |
| 6 | + const parsedConfig: Record<string, unknown> = { |
| 7 | + entry: "", |
| 8 | + project: "src/**/*.ts", |
| 9 | + }; |
| 10 | + sanitizeKnipConfigPatterns(parsedConfig); |
| 11 | + expect(parsedConfig).toEqual({ project: "src/**/*.ts" }); |
| 12 | + }); |
| 13 | + |
| 14 | + it("removes whitespace-only string values", () => { |
| 15 | + const parsedConfig: Record<string, unknown> = { |
| 16 | + entry: " ", |
| 17 | + ignore: "\n\t", |
| 18 | + project: "src/**/*.ts", |
| 19 | + }; |
| 20 | + sanitizeKnipConfigPatterns(parsedConfig); |
| 21 | + expect(parsedConfig).toEqual({ project: "src/**/*.ts" }); |
| 22 | + }); |
| 23 | + |
| 24 | + it("filters empty strings out of arrays", () => { |
| 25 | + const parsedConfig: Record<string, unknown> = { |
| 26 | + entry: ["src/index.ts", "", "src/main.ts"], |
| 27 | + }; |
| 28 | + sanitizeKnipConfigPatterns(parsedConfig); |
| 29 | + expect(parsedConfig).toEqual({ entry: ["src/index.ts", "src/main.ts"] }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("removes arrays that become empty after filtering", () => { |
| 33 | + const parsedConfig: Record<string, unknown> = { |
| 34 | + entry: ["", " "], |
| 35 | + project: ["src/**/*.ts"], |
| 36 | + }; |
| 37 | + sanitizeKnipConfigPatterns(parsedConfig); |
| 38 | + expect(parsedConfig).toEqual({ project: ["src/**/*.ts"] }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("recurses into nested plugin configs and workspaces", () => { |
| 42 | + const parsedConfig: Record<string, unknown> = { |
| 43 | + vite: { |
| 44 | + config: ["", "vite.config.ts"], |
| 45 | + entry: "", |
| 46 | + }, |
| 47 | + workspaces: { |
| 48 | + "packages/foo": { |
| 49 | + entry: ["", "src/index.ts"], |
| 50 | + ignore: "", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }; |
| 54 | + sanitizeKnipConfigPatterns(parsedConfig); |
| 55 | + expect(parsedConfig).toEqual({ |
| 56 | + vite: { config: ["vite.config.ts"] }, |
| 57 | + workspaces: { |
| 58 | + "packages/foo": { entry: ["src/index.ts"] }, |
| 59 | + }, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("preserves non-string entries inside arrays", () => { |
| 64 | + const parsedConfig: Record<string, unknown> = { |
| 65 | + ignoreDependencies: [/regex/, "valid", ""], |
| 66 | + }; |
| 67 | + sanitizeKnipConfigPatterns(parsedConfig); |
| 68 | + expect(parsedConfig).toEqual({ ignoreDependencies: [/regex/, "valid"] }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("leaves boolean and falsy non-string values untouched", () => { |
| 72 | + const parsedConfig: Record<string, unknown> = { |
| 73 | + vite: false, |
| 74 | + eslint: true, |
| 75 | + tags: [], |
| 76 | + includeEntryExports: false, |
| 77 | + }; |
| 78 | + sanitizeKnipConfigPatterns(parsedConfig); |
| 79 | + expect(parsedConfig).toEqual({ |
| 80 | + vite: false, |
| 81 | + eslint: true, |
| 82 | + tags: [], |
| 83 | + includeEntryExports: false, |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments