|
| 1 | +import { goTry } from "go-go-try"; |
| 2 | +import { dedent } from "ts-dedent"; |
| 3 | +import { ModuleKind, ModuleResolutionKind, Project, ScriptTarget } from "ts-morph"; |
| 4 | +import { afterEach, expect, test, vi } from "vitest"; |
| 5 | +import { extractDeclarations } from "./extract-declarations.ts"; |
| 6 | +import { getPackageDeclarations } from "./get-package-declarations.ts"; |
| 7 | + |
| 8 | +vi.mock("./extract-declarations", () => ({ |
| 9 | + extractDeclarations: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + vi.clearAllMocks(); |
| 14 | +}); |
| 15 | + |
| 16 | +test("success", async () => { |
| 17 | + const project = new Project({ |
| 18 | + useInMemoryFileSystem: true, |
| 19 | + compilerOptions: { |
| 20 | + lib: ["lib.esnext.full.d.ts"], |
| 21 | + target: ScriptTarget.ESNext, |
| 22 | + module: ModuleKind.ESNext, |
| 23 | + moduleResolution: ModuleResolutionKind.Bundler, |
| 24 | + }, |
| 25 | + }); |
| 26 | + const indexFile = project.createSourceFile( |
| 27 | + "index.ts", |
| 28 | + dedent` |
| 29 | + export {}; |
| 30 | + `, |
| 31 | + ); |
| 32 | + vi.mocked(extractDeclarations).mockResolvedValue([]); |
| 33 | + const [err, declarations] = await goTry( |
| 34 | + getPackageDeclarations({ |
| 35 | + pkgName: "foo", |
| 36 | + project, |
| 37 | + indexFile, |
| 38 | + maxDepth: 5, |
| 39 | + }), |
| 40 | + ); |
| 41 | + expect(err).toBeUndefined(); |
| 42 | + expect(declarations).toStrictEqual([]); |
| 43 | +}); |
| 44 | + |
| 45 | +test("failure", async () => { |
| 46 | + const project = new Project({ |
| 47 | + useInMemoryFileSystem: true, |
| 48 | + compilerOptions: { |
| 49 | + lib: ["lib.esnext.full.d.ts"], |
| 50 | + target: ScriptTarget.ESNext, |
| 51 | + module: ModuleKind.ESNext, |
| 52 | + moduleResolution: ModuleResolutionKind.Bundler, |
| 53 | + }, |
| 54 | + }); |
| 55 | + const indexFile = project.createSourceFile( |
| 56 | + "index.ts", |
| 57 | + dedent` |
| 58 | + export {}; |
| 59 | + `, |
| 60 | + ); |
| 61 | + vi.mocked(extractDeclarations).mockRejectedValue(new Error("test")); |
| 62 | + const [err, declarations] = await goTry( |
| 63 | + getPackageDeclarations({ |
| 64 | + pkgName: "foo", |
| 65 | + project, |
| 66 | + indexFile, |
| 67 | + maxDepth: 5, |
| 68 | + }), |
| 69 | + ); |
| 70 | + expect(err).toBeDefined(); |
| 71 | + expect(declarations).toBeUndefined(); |
| 72 | +}); |
0 commit comments