Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Improve error message when `createTester` base directory does not contain a package.json
11 changes: 10 additions & 1 deletion packages/compiler/src/testing/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@
};

const sl = await createSourceLoader(host);
const selfName = JSON.parse((await host.readFile(resolvePath(base, "package.json"))).text).name;
const packageJsonPath = resolvePath(base, "package.json");
let packageJsonContent: string;
try {
packageJsonContent = (await host.readFile(packageJsonPath)).text;
} catch (e) {
throw new Error(

Check failure on line 80 in packages/compiler/src/testing/tester.ts

View workflow job for this annotation

GitHub Actions / Lint

There is no `cause` attached to the symptom error being thrown
`createTester failed to read '${packageJsonPath}'. The first argument to createTester must be the library root directory containing a package.json. Got: '${base}'`,
);
}
const selfName = JSON.parse(packageJsonContent).name;
const moduleHost: ResolveModuleHost = {
realpath: async (x) => x,
stat: host.stat,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it } from "vitest";
import { expect, it } from "vitest";
import { CompilerHost } from "../../src/core/types.js";
import { createTestFileSystem, mockFile } from "../../src/testing/fs.js";
import { resolveVirtualPath } from "../../src/testing/test-utils.js";
Expand Down Expand Up @@ -50,3 +50,14 @@ it("subpath typespec export get added to the test host", async () => {
});
await Tester.compile(`import "mylib/subpath";`);
});

it("throws a clear error when base does not contain a package.json", async () => {
const fs = mkFs({});
const Tester = createTester(resolveVirtualPath("not-a-library-root"), {
host: fs,
libraries: [],
});
await expect(Tester.compile(``)).rejects.toThrowError(
/createTester failed to read.*package\.json.*first argument to createTester must be the library root/,
);
});
Loading