-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathesm.test.ts
126 lines (118 loc) · 3.3 KB
/
esm.test.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { expect, describe, it } from "vitest";
import {
genImport,
genExport,
genDynamicImport,
genSafeVariableName,
} from "../src";
import { genTestTitle } from "./_utils";
const genImportTests = [
{
names: undefined,
code: 'import "pkg";',
},
{ names: "foo", code: 'import foo from "pkg";' },
{ names: ["foo"], code: 'import { foo } from "pkg";' },
{
names: "foo",
code: `import foo from 'pkg';`,
options: { singleQuotes: true },
},
{
names: [{ name: "foo", as: "foo" }],
code: 'import { foo } from "pkg";',
},
{
names: [{ name: "foo", as: "bar" }],
code: 'import { foo as bar } from "pkg";',
},
{ names: { name: "*", as: "bar" }, code: 'import * as bar from "pkg";' },
{
names: [{ name: "default", as: "Test" }],
code: 'import { default as Test } from "pkg";',
},
{
names: ["foo"],
code: 'import { foo } from "pkg" assert { type: "json" };',
options: { assert: { type: "json" } },
},
{
names: ["foo"],
code: 'import { foo } from "pkg" with { type: "json" };',
options: { attributes: { type: "json" } },
},
];
describe("genImport", () => {
for (const t of genImportTests) {
it(genTestTitle(t.code), () => {
const code = genImport("pkg", t.names, t.options);
expect(code).to.equal(t.code);
});
}
});
const genExportTests = [
{ names: undefined, code: 'export "pkg";' },
{ names: "foo", code: 'export foo from "pkg";' },
{ names: ["foo"], code: 'export { foo } from "pkg";' },
{
names: [{ name: "foo", as: "bar" }],
code: 'export { foo as bar } from "pkg";',
},
{ names: { name: "*", as: "bar" }, code: 'export * as bar from "pkg";' },
{ names: ["default"], code: 'export { default } from "pkg";' },
{
names: ["foo"],
code: 'export { foo } from "pkg" assert { type: "json" };',
options: { assert: { type: "json" } },
},
];
describe("genExport", () => {
for (const t of genExportTests) {
it(genTestTitle(t.code), () => {
const code = genExport("pkg", t.names, t.options);
expect(code).to.equal(t.code);
});
}
});
const genDynamicImportTests = [
{ code: '() => import("pkg")' },
{ opts: { wrapper: false }, code: 'import("pkg")' },
{
opts: { interopDefault: true },
code: '() => import("pkg").then(m => m.default || m)',
},
{
opts: { comment: 'webpackChunkName: "chunks/dynamic"' },
code: '() => import("pkg" /* webpackChunkName: "chunks/dynamic" */)',
},
{
opts: { assert: { type: "json" } },
code: '() => import("pkg", { assert: { type: "json" } })',
},
{
opts: { attributes: { type: "json" } },
code: '() => import("pkg", { with: { type: "json" } })',
},
];
describe("genDynamicImport", () => {
for (const t of genDynamicImportTests) {
it(genTestTitle(t.code), () => {
const code = genDynamicImport("pkg", t.opts);
expect(code).to.equal(t.code);
});
}
});
const genSafeVariableNameTests = [
{ key: "valid_import", code: "valid_import" },
{ key: "for", code: "_for" },
{ key: "with space", code: "with_32space" },
{ key: "123 numbers", code: "_123_32numbers" },
];
describe("genSafeVariableName", () => {
for (const t of genSafeVariableNameTests) {
it(genTestTitle(t.code), () => {
const code = genSafeVariableName(t.key);
expect(code).to.equal(t.code);
});
}
});