-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcjs-compat.test.ts
More file actions
72 lines (65 loc) · 2.62 KB
/
cjs-compat.test.ts
File metadata and controls
72 lines (65 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Dual ESM/CJS build compatibility tests
*
* Verifies that both the ESM and CJS builds exist and work correctly,
* so consumers using either module system get a working package.
*
* See: https://github.com/github/copilot-sdk/issues/528
*/
import { describe, expect, it } from "vitest";
import { existsSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { join } from "node:path";
const distDir = join(import.meta.dirname, "../dist");
describe("Dual ESM/CJS build (#528)", () => {
it("ESM dist file should exist", () => {
expect(existsSync(join(distDir, "index.js"))).toBe(true);
});
it("CJS dist file should exist", () => {
expect(existsSync(join(distDir, "cjs/index.js"))).toBe(true);
});
it("CJS build is requireable and exports CopilotClient", () => {
const script = `
const sdk = require(${JSON.stringify(join(distDir, "cjs/index.js"))});
if (typeof sdk.CopilotClient !== 'function') {
console.error('CopilotClient is not a function');
process.exit(1);
}
console.log('CJS require: OK');
`;
const output = execFileSync(process.execPath, ["--eval", script], {
encoding: "utf-8",
timeout: 10000,
cwd: join(import.meta.dirname, ".."),
});
expect(output).toContain("CJS require: OK");
});
it("CJS build resolves bundled CLI path", () => {
const script = `
const sdk = require(${JSON.stringify(join(distDir, "cjs/index.js"))});
const client = new sdk.CopilotClient({ autoStart: false });
console.log('CJS CLI resolved: OK');
`;
const output = execFileSync(process.execPath, ["--eval", script], {
encoding: "utf-8",
timeout: 10000,
cwd: join(import.meta.dirname, ".."),
});
expect(output).toContain("CJS CLI resolved: OK");
});
it("ESM build resolves bundled CLI path", () => {
const esmPath = join(distDir, "index.js");
const script = `
import { pathToFileURL } from 'node:url';
const sdk = await import(pathToFileURL(${JSON.stringify(esmPath)}).href);
const client = new sdk.CopilotClient({ autoStart: false });
console.log('ESM CLI resolved: OK');
`;
const output = execFileSync(process.execPath, ["--input-type=module", "--eval", script], {
encoding: "utf-8",
timeout: 10000,
cwd: join(import.meta.dirname, ".."),
});
expect(output).toContain("ESM CLI resolved: OK");
});
});