lintStagedConfig.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { readFileSync, rmSync } from "node:fs";
  2. import { afterEach, describe, expect, it } from "vitest";
  3. import lintStagedConfig from "../../../lint-staged.config.mjs";
  4. const createdManifestPaths: string[] = [];
  5. function extractManifestPath(command: string): string {
  6. const match = command.match(/--files-manifest\s+(.+)$/u);
  7. if (!match) {
  8. throw new Error(`command did not include --files-manifest: ${command}`);
  9. }
  10. return match[1]!.replace(/^['"]|['"]$/gu, "");
  11. }
  12. function readSingleCommandManifest(result: string | string[]) {
  13. // 即使旧拆批实现导致断言失败,也要清理本次测试生成的全部 manifest。
  14. const commands = typeof result === "string" ? [result] : result;
  15. const paths = commands.map(extractManifestPath);
  16. createdManifestPaths.push(...paths);
  17. expect(typeof result).toBe("string");
  18. return JSON.parse(readFileSync(paths[0]!, "utf8"));
  19. }
  20. describe("lint-staged config", () => {
  21. afterEach(() => {
  22. for (const manifestPath of createdManifestPaths.splice(0)) {
  23. rmSync(manifestPath, { force: true });
  24. }
  25. });
  26. it("passes shell-sensitive Windows filenames through a manifest", () => {
  27. const sensitiveFile = "packages/ui/src/foo%PATH% caret^ amp& paren(x).test.ts";
  28. const command = lintStagedConfig["packages/**/*.{js,jsx,ts,tsx,json}"]([sensitiveFile]);
  29. const manifest = readSingleCommandManifest(command);
  30. expect(command).toContain("node scripts/run-lint-staged-vitest.mjs related");
  31. expect(command).toContain("--files-manifest");
  32. expect(command).not.toContain(sensitiveFile);
  33. expect(manifest).toEqual({
  34. mode: "related",
  35. files: [sensitiveFile],
  36. });
  37. });
  38. it("runs all changed tests in one vitest command", () => {
  39. const testFiles = Array.from(
  40. { length: 21 },
  41. (_, index) => `packages/ui/test/changed-${index}.test.ts`,
  42. );
  43. const commands = lintStagedConfig["packages/*/test/**/*.test.ts"](testFiles);
  44. expect(readSingleCommandManifest(commands)).toEqual({ mode: "run", files: testFiles });
  45. });
  46. it("selects related tests for all sources in one vitest command", () => {
  47. const sourceFiles = Array.from(
  48. { length: 17 },
  49. (_, index) => `packages/ui/src/changed-${index}.ts`,
  50. );
  51. const commands = lintStagedConfig["packages/**/*.{js,jsx,ts,tsx,json}"](sourceFiles);
  52. expect(readSingleCommandManifest(commands)).toEqual({ mode: "related", files: sourceFiles });
  53. });
  54. it("keeps E2E excluded and separates changed tests from sources", () => {
  55. const files = [
  56. "packages\\ui\\src\\example.ts",
  57. "packages\\ui\\test\\example.test.ts",
  58. "packages\\desktop\\test\\e2e\\example.test.ts",
  59. ];
  60. expect(
  61. readSingleCommandManifest(lintStagedConfig["packages/*/test/**/*.test.ts"](files)),
  62. ).toEqual({ mode: "run", files: ["packages/ui/test/example.test.ts"] });
  63. expect(
  64. readSingleCommandManifest(lintStagedConfig["packages/**/*.{js,jsx,ts,tsx,json}"](files)),
  65. ).toEqual({ mode: "related", files: ["packages/ui/src/example.ts"] });
  66. });
  67. it("does not launch Vitest for empty or E2E-only groups", () => {
  68. for (const callback of Object.values(lintStagedConfig)) {
  69. expect(callback([])).toEqual([]);
  70. expect(callback(["packages/desktop/test/e2e/example.test.ts"])).toEqual([]);
  71. }
  72. });
  73. });