rendererTypecheckCoverage.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { readFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import ts from "typescript";
  4. import { expect, it } from "vitest";
  5. it("根类型检查覆盖 Renderer 的远程服务接口装配", async () => {
  6. const root = resolve(import.meta.dirname, "../../..");
  7. const packageJson = JSON.parse(await readFile(resolve(root, "package.json"), "utf8"));
  8. const buildCommand = packageJson.scripts.typecheck
  9. .split("&&")
  10. .map((command: string) => command.trim())
  11. .find((command: string) => command.startsWith("tsc -b "));
  12. expect(buildCommand).toBeDefined();
  13. const pending: string[] = buildCommand.slice("tsc -b ".length).split(/\s+/u);
  14. const visited = new Set<string>();
  15. const checkedFiles = new Set<string>();
  16. const host: ts.ParseConfigFileHost = {
  17. ...ts.sys,
  18. onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
  19. throw new Error(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
  20. },
  21. };
  22. while (pending.length > 0) {
  23. const entry = resolve(root, pending.pop()!);
  24. const configPath = entry.endsWith(".json") ? entry : resolve(entry, "tsconfig.json");
  25. if (visited.has(configPath)) continue;
  26. visited.add(configPath);
  27. // TypeScript 配置解析器只提供同步文件系统接口;与真实 tsc 共用配置继承和 include 规则。
  28. const config = ts.getParsedCommandLineOfConfigFile(configPath, {}, host);
  29. expect(config, configPath).toBeDefined();
  30. config!.fileNames.forEach((file) => checkedFiles.add(resolve(file)));
  31. config!.projectReferences?.forEach((reference) => pending.push(reference.path));
  32. }
  33. // 缺少 Renderer 工程时,服务接口已新增方法而远程占位未实现也会被根检查漏过。
  34. expect(checkedFiles).toContain(
  35. resolve(root, "packages/desktop/src/renderer/src/remoteWorkspaceSessionServices.ts"),
  36. );
  37. });