Debian Codegraph @

resolve-exports/test/utils.ts

raw
1 import * as uvu from 'uvu';
2 import * as assert from 'uvu/assert';
3 import * as $ from '../src/utils';
4
5 import type * as t from 'resolve.exports';
6
7 function describe(
8 name: string,
9 cb: (it: uvu.Test) => void
10 ) {
11 let t = uvu.suite(name);
12 cb(t);
13 t.run();
14 }
15
16 describe('$.conditions', it => {
17 const EMPTY = {};
18
19 function run(o?: t.Options): string[] {
20 return [...$.conditions(o||{})];
21 }
22
23 it('should be a function', () => {
24 assert.type($.conditions, 'function');
25 });
26
27 it('should return `Set` instance', () => {
28 let output = $.conditions(EMPTY);
29 assert.instance(output, Set);
30 });
31
32 it('default conditions', () => {
33 assert.equal(
34 [ ...$.conditions(EMPTY) ],
35 ['default', 'import', 'node']
36 );
37 });
38
39 it('default conditions :: unsafe', () => {
40 assert.equal(
41 run({ unsafe: true }),
42 ['default']
43 );
44 });
45
46 it('option.browser', () => {
47 assert.equal(
48 run({ browser: true }),
49 ['default', 'import', 'browser']
50 );
51 });
52
53 // unsafe ignores all but conditions
54 it('option.browser :: unsafe', () => {
55 let output = run({
56 browser: true,
57 unsafe: true,
58 });
59 assert.equal(output, ['default']);
60 });
61
62 it('option.require', () => {
63 assert.equal(
64 run({ require: true }),
65 ['default', 'require', 'node']
66 );
67 });
68
69 // unsafe ignores all but conditions
70 it('option.require :: unsafe', () => {
71 let output = run({
72 require: true,
73 unsafe: true,
74 });
75 assert.equal(output, ['default']);
76 });
77
78 it('option.conditions', () => {
79 let output = run({ conditions: ['foo', 'bar'] });
80 assert.equal(output, ['default', 'foo', 'bar', 'import', 'node']);
81 });
82
83 it('option.conditions :: order', () => {
84 let output = run({ conditions: ['node', 'import', 'foobar'] });
85 assert.equal(output, ['default', 'node', 'import', 'foobar']);
86 });
87
88 it('option.conditions :: unsafe', () => {
89 let output = run({ unsafe: true, conditions: ['foo', 'bar'] });
90 assert.equal(output, ['default', 'foo', 'bar']);
91 });
92
93 it('option.conditions :: browser', () => {
94 let output = run({ browser: true, conditions: ['foo', 'bar'] });
95 assert.equal(output, ['default', 'foo', 'bar', 'import', 'browser']);
96 });
97
98 it('option.conditions :: browser :: order', () => {
99 let output = run({ browser: true, conditions: ['browser', 'foobar'] });
100 assert.equal(output, ['default', 'browser', 'foobar', 'import']);
101 });
102
103 it('option.conditions :: require', () => {
104 let output = run({ require: true, conditions: ['foo', 'bar'] });
105 assert.equal(output, ['default', 'foo', 'bar', 'require', 'node']);
106 });
107
108 it('option.conditions :: require :: order', () => {
109 let output = run({ require: true, conditions: ['require', 'foobar'] });
110 assert.equal(output, ['default', 'require', 'foobar', 'node']);
111 });
112 });
113
114 describe('$.toEntry', it => {
115 const PKG = 'PACKAGE';
116 const EXTERNAL = 'EXTERNAL';
117
118 function run(input: string, expect: string, externals?: boolean) {
119 // overloading not working -,-
120 let output = externals ? $.toEntry(PKG, input, true) : $.toEntry(PKG, input);
121 let msg = `"${input}" -> "${expect}"` + (externals ? ' (externals)' : '');
122 assert.is(output, expect, msg);
123 }
124
125 it('should be a function', () => {
126 assert.type($.toEntry, 'function');
127 });
128
129 it('PKG', () => {
130 run(PKG, '.');
131 run(PKG, '.', true);
132 });
133
134 it('.', () => {
135 run('.', '.');
136 run('.', '.', true);
137 });
138
139 it('./', () => {
140 run('./', './');
141 run('./', './', true);
142 });
143
144 it('#inner', () => {
145 run('#inner', '#inner');
146 run('#inner', '#inner', true);
147 });
148
149 it('./foo', () => {
150 run('./foo', './foo');
151 run('./foo', './foo', true);
152 });
153
154 it('foo', () => {
155 run('foo', './foo'); // forces path by default
156 run('foo', 'foo', true);
157 });
158
159 it('.ini', () => {
160 run('.ini', './.ini'); // forces path by default
161 run('.ini', '.ini', true);
162 });
163
164 // handle "import 'lib/lib';" case
165 it('./PKG', () => {
166 let input = './' + PKG;
167 run(input, input);
168 run(input, input, true);
169 });
170
171 it('PKG/subpath', () => {
172 let input = PKG + '/other';
173 run(input, './other');
174 run(input, './other', true);
175 });
176
177 it('PKG/#inner', () => {
178 let input = PKG + '/#inner';
179 run(input, '#inner');
180 run(input, '#inner', true);
181 });
182
183 it('PKG/.ini', () => {
184 let input = PKG + '/.ini';
185 run(input, './.ini');
186 run(input, './.ini', true);
187 });
188
189 it('EXTERNAL', () => {
190 run(EXTERNAL, './'+EXTERNAL); // forces path by default
191 run(EXTERNAL, EXTERNAL, true);
192 });
193 });
194
195 describe('$.injects', it => {
196 function run<T extends t.Path[]>(value: string, input: T, expect: T) {
197 let output = $.injects(input, value);
198 assert.is(output, undefined);
199 assert.equal(input, expect);
200 }
201
202 it('should be a function', () => {
203 assert.type($.injects, 'function');
204 });
205
206 it('should replace "*" character', () => {
207 run('bar', ['./foo*.jpg'], ['./foobar.jpg']);
208 });
209
210 it('should replace multiple "*" characters w/ same value', () => {
211 run('bar', ['./*/foo-*.jpg'], ['./bar/foo-bar.jpg']);
212 });
213
214 // for the "./features/" => "./src/features/" scenario
215 it('should append `value` if missing "*" character', () => {
216 run('app.js', ['./src/features/'], ['./src/features/app.js']);
217 });
218
219 it('should handle mixed array input', () => {
220 run('xyz',
221 ['./foo/', './esm/*.mjs', './build/*/index-*.js'],
222 ['./foo/xyz', './esm/xyz.mjs', './build/xyz/index-xyz.js'],
223 );
224 });
225 });
226
227 describe('$.loop', it => {
228 const FILE = './file.js';
229 const DEFAULT = './foobar.js';
230
231 type Expect = string | string[] | null | undefined;
232 function run(expect: Expect, map: t.Exports.Value, conditions?: string[]) {
233 let output = $.loop(map, new Set([ 'default', ...conditions||[] ]));
234 if (typeof expect == 'string') {
235 assert.ok(Array.isArray(output));
236 assert.is(output[0], expect);
237 assert.is(output.length, 1);
238 } else {
239 // Array, null, undefined
240 assert.equal(output, expect);
241 }
242 }
243
244 it('should be a function', () => {
245 assert.type($.loop, 'function');
246 });
247
248 it('string', () => {
249 run('./foo.mjs', './foo.mjs');
250 // @ts-expect-error
251 run('.', '.');
252 });
253
254 it('empties', () => {
255 // @ts-expect-error
256 run(undefined, '');
257 run(undefined, null);
258 run(undefined, []);
259 run(undefined, {});
260 });
261
262 it('{ default }', () => {
263 run(FILE, {
264 default: FILE,
265 });
266
267 run(FILE, {
268 other: './unknown.js',
269 default: FILE,
270 });
271
272 run(undefined, {
273 other: './unknown.js',
274 });
275
276 run(FILE, {
277 foo: './foo.js',
278 default: {
279 bar: './bar.js',
280 default: {
281 baz: './baz.js',
282 default: FILE,
283 }
284 }
285 });
286 });
287
288 it('{ custom }', () => {
289 let conditions = ['custom'];
290
291 run(DEFAULT, {
292 default: DEFAULT,
293 custom: FILE,
294 }, conditions);
295
296 run(FILE, {
297 custom: FILE,
298 default: DEFAULT,
299 }, conditions);
300
301 run(undefined, {
302 foo: './foo.js',
303 bar: './bar.js',
304 }, conditions);
305
306 run(FILE, {
307 foo: './foo.js',
308 custom: {
309 default: {
310 custom: FILE,
311 default: DEFAULT,
312 }
313 },
314 default: {
315 custom: './bar.js'
316 }
317 }, conditions);
318 });
319
320 it('[ string ]', () => {
321 run(
322 [DEFAULT, FILE],
323 [DEFAULT, FILE]
324 );
325
326 run(undefined, [
327 null,
328 ]);
329
330 run(
331 [DEFAULT, FILE],
332 [null, DEFAULT, FILE]
333 );
334
335 run(
336 [DEFAULT, FILE],
337 [DEFAULT, null, FILE]
338 );
339 });
340
341 it('[{ default }]', () => {
342 run([DEFAULT, FILE], [
343 {
344 default: DEFAULT,
345 },
346 FILE
347 ]);
348
349 run([FILE, DEFAULT], [
350 FILE,
351 null,
352 {
353 default: DEFAULT,
354 },
355 ]);
356
357 run([DEFAULT, FILE], [
358 {
359 default: {
360 default: {
361 default: DEFAULT,
362 }
363 }
364 },
365 null,
366 FILE
367 ]);
368
369 run([DEFAULT, FILE, './foo.js'], [
370 {
371 default: {
372 default: DEFAULT,
373 }
374 },
375 null,
376 {
377 default: {
378 default: DEFAULT,
379 }
380 },
381 FILE,
382 {
383 default: './foo.js'
384 }
385 ]);
386 });
387
388 it('{ [mixed] }', () => {
389 run([DEFAULT, FILE], {
390 default: [DEFAULT, FILE]
391 });
392
393 run([DEFAULT, FILE], {
394 default: [null, DEFAULT, FILE]
395 });
396
397 run([DEFAULT, FILE], {
398 default: [null, {
399 default: DEFAULT
400 }, FILE]
401 });
402
403 run([FILE, DEFAULT], {
404 default: {
405 custom: [{
406 default: [FILE, FILE, null, DEFAULT]
407 }, null, DEFAULT, FILE]
408 }
409 }, ['custom']);
410
411 run([DEFAULT, FILE], {
412 default: {
413 custom: [{
414 custom: [DEFAULT, null],
415 default: [FILE, FILE, null, DEFAULT]
416 }, null, DEFAULT, FILE]
417 }
418 }, ['custom']);
419 });
420 });