forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage-exports.test.ts
More file actions
74 lines (61 loc) · 2.09 KB
/
Copy pathpackage-exports.test.ts
File metadata and controls
74 lines (61 loc) · 2.09 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
73
74
import { createRequire } from 'node:module';
import { readFile, stat } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
interface ConditionalExport {
types: string;
import: string;
require: string;
}
interface PackageJson {
name: string;
main: string;
module: string;
types: string;
exports: Record<string, ConditionalExport | string>;
}
const packageRoot = new URL('../', import.meta.url);
const packageJson = JSON.parse(
await readFile(new URL('package.json', packageRoot), 'utf8'),
) as PackageJson;
const require = createRequire(import.meta.url);
async function expectFile(target: string): Promise<void> {
const targetUrl = new URL(target, packageRoot);
const targetStat = await stat(targetUrl);
expect(targetStat.isFile(), `${target} should be a file`).toBe(true);
}
async function expectNonEmptyDeclaration(target: string): Promise<void> {
expect(target).toMatch(/\.d\.(?:ts|cts)$/u);
await expectFile(target);
await expect(readFile(new URL(target, packageRoot), 'utf8')).resolves.toMatch(
/\S/u,
);
}
describe('package entry points', () => {
it('resolves the top-level main, module, and types files', async () => {
await expectFile(packageJson.main);
await expectFile(packageJson.module);
await expectNonEmptyDeclaration(packageJson.types);
await import(new URL(packageJson.module, packageRoot).href);
expect(() =>
require(fileURLToPath(new URL(packageJson.main, packageRoot))),
).not.toThrow();
});
for (const [subpath, entry] of Object.entries(packageJson.exports)) {
it(`resolves the ${subpath} export`, async () => {
if (typeof entry === 'string') {
await expectFile(entry);
return;
}
await expectFile(entry.import);
await expectFile(entry.require);
await expectNonEmptyDeclaration(entry.types);
const specifier =
subpath === '.'
? packageJson.name
: `${packageJson.name}/${subpath.slice(2)}`;
await import(specifier);
expect(() => require(specifier)).not.toThrow();
});
}
});