|
| 1 | +import { goTry } from "go-go-try"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import { join } from "pathe"; |
| 4 | +import { expect, test } from "vitest"; |
| 5 | +import { getProject } from "./get-project.ts"; |
| 6 | +import { tempDir } from "./temp-dir.ts"; |
| 7 | + |
| 8 | +test("no cwd", async () => { |
| 9 | + await using dir = await tempDir(); |
| 10 | + const [err, _] = goTry(() => |
| 11 | + getProject({ |
| 12 | + indexFilePath: "./this-file-does-not-exist.ts", |
| 13 | + typeRoots: join(dir.path, "this-dir-does-not-exist"), |
| 14 | + }), |
| 15 | + ); |
| 16 | + expect(err).toBeDefined(); |
| 17 | +}); |
| 18 | + |
| 19 | +test("no index file", async () => { |
| 20 | + await using dir = await tempDir(); |
| 21 | + const [err, _] = goTry(() => |
| 22 | + getProject({ |
| 23 | + indexFilePath: "./this-file-does-not-exist.ts", |
| 24 | + typeRoots: dir.path, |
| 25 | + }), |
| 26 | + ); |
| 27 | + expect(err).toBeDefined(); |
| 28 | +}); |
| 29 | + |
| 30 | +test("with index file", async () => { |
| 31 | + await using dir = await tempDir(); |
| 32 | + const indexFilePath = join(dir.path, "./index.ts"); |
| 33 | + await fs.writeFile(indexFilePath, "export {};"); |
| 34 | + const [err, res] = goTry(() => |
| 35 | + getProject({ |
| 36 | + indexFilePath, |
| 37 | + typeRoots: dir.path, |
| 38 | + }), |
| 39 | + ); |
| 40 | + expect(err).toBeUndefined(); |
| 41 | + expect(res).toBeDefined(); |
| 42 | + expect(res!.project.getSourceFiles().map((sf) => sf.getBaseName())).toStrictEqual(["index.ts"]); |
| 43 | +}); |
| 44 | + |
| 45 | +test("with index file and other file", async () => { |
| 46 | + await using dir = await tempDir(); |
| 47 | + const indexFilePath = join(dir.path, "./index.ts"); |
| 48 | + const otherFilePath = join(dir.path, "./other.ts"); |
| 49 | + await fs.writeFile(indexFilePath, "export * from './other';"); |
| 50 | + await fs.writeFile(otherFilePath, "export const a = 1;"); |
| 51 | + const [err, res] = goTry(() => |
| 52 | + getProject({ |
| 53 | + indexFilePath, |
| 54 | + typeRoots: dir.path, |
| 55 | + }), |
| 56 | + ); |
| 57 | + expect(err).toBeUndefined(); |
| 58 | + expect(res).toBeDefined(); |
| 59 | + expect(res!.project.getSourceFiles().map((sf) => sf.getBaseName())).toStrictEqual([ |
| 60 | + "index.ts", |
| 61 | + "other.ts", |
| 62 | + ]); |
| 63 | +}); |
0 commit comments