-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.test.ts
144 lines (132 loc) · 3.64 KB
/
utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { describe, it, expect } from "vitest";
import {
isNodeBuiltin,
sanitizeFilePath,
getProtocol,
parseNodeModulePath,
lookupNodeModuleSubpath,
} from "../src";
describe("isNodeBuiltin", () => {
const cases = {
fs: true,
fake: false,
"node:fs": true,
"node:fake": false,
"fs/promises": true,
"fs/fake": true, // invalid import
};
for (const id in cases) {
it(`'${id}': ${cases[id]}`, () => {
expect(isNodeBuiltin(id)).to.equal(cases[id]);
});
}
it("undefined", () => {
expect(isNodeBuiltin()).to.equal(false);
});
});
describe("sanitizeFilePath", () => {
const cases = {
"C:/te#st/[...slug].jsx": "C:/te_st/_...slug_.jsx",
"C:\\te#st\\[...slug].jsx": "C:/te_st/_...slug_.jsx",
"/te#st/[...slug].jsx": "/te_st/_...slug_.jsx",
"/te#st/[].jsx": "/te_st/_.jsx",
'\0a_b*c:d\u007Fe<f>g#h"i{j}k|l^m[n]o`p.jsx':
"_a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p.jsx",
"Foo.vue?vue&type=script&setup=true": "Foo.vue",
"Foo.vue&vue&type=script&setup=true&generic=T%20extends%20any%2C%20O%20extends%20T%3CZ%7Ca%3E&lang":
"Foo.vue_vue_type_script_setup_true_generic_T_extends_any__O_extends_T_Z_a__lang",
"": "",
};
for (const id in cases) {
it(`'${id}': ${cases[id]}`, () => {
expect(sanitizeFilePath(id)).to.equal(cases[id]);
});
}
it("undefined", () => {
expect(sanitizeFilePath()).to.equal("");
});
});
describe("getProtocol", () => {
it("no protocol", () => {
expect(getProtocol("/src/a.ts")).to.equal(undefined);
expect(getProtocol("C:/src/a.ts")).to.equal(undefined);
});
it("file protocol", () => {
expect(getProtocol("file://src/a.ts")).to.equal("file");
expect(getProtocol("file://C:/src/a.ts")).to.equal("file");
expect(getProtocol("file:///C:/src/a.ts")).to.equal("file");
});
});
describe("parseNodeModulePath", () => {
const tests = [
{
input: "/foo/bar",
output: {},
},
{
input: "/src/a/node_modules/thing",
output: {
dir: "/src/a/node_modules/",
name: "thing",
},
},
{
input: "/src/a/node_modules/thing/dist/index.mjs",
output: {
dir: "/src/a/node_modules/",
name: "thing",
subpath: "./dist/index.mjs",
},
},
{
input: "C:\\src\\a\\node_modules\\thing\\dist\\index.mjs",
output: {
dir: "C:/src/a/node_modules/",
name: "thing",
subpath: "./dist/index.mjs",
},
},
];
for (const t of tests) {
it(t.input, () => {
expect(parseNodeModulePath(t.input)).toMatchObject(t.output);
});
}
});
describe("lookupNodeModuleSubpath", () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const r = (p: string) => new URL(p, import.meta.url).toString();
const tests = [
{
name: "resolves with exports field",
input: r("fixture/package/node_modules/subpaths/lib/subpath.mjs"),
output: "./subpath",
},
{
name: "resolves with fallback subpath guess",
input: r("fixture/package/node_modules/alien/lib/subpath.json5"),
output: "./lib/subpath.json5",
},
{
name: "ignores invalid paths",
input: r("/foo/bar/lib/subpath.mjs"),
output: undefined,
},
{
name: "resolves main export",
input: r("fixture/package/node_modules/subpaths/foo/bar.mjs"),
output: "./foo/bar.mjs",
},
{
name: "resolves main export",
input: r("fixture/package/node_modules/subpaths/"),
output: "./",
},
];
for (const t of tests) {
it(t.name, async () => {
const result = await lookupNodeModuleSubpath(t.input);
expect(result).toBe(t.output);
});
}
});