Skip to content

Commit a070b9a

Browse files
committed
feat: add path-type based tests on main resolve
1 parent 503ee0e commit a070b9a

File tree

1 file changed

+280
-0
lines changed

1 file changed

+280
-0
lines changed

test/resolve.test.js

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,283 @@ describe("resolve", () => {
319319
});
320320
});
321321
});
322+
323+
const isWin32 = process.platform === "win32";
324+
325+
describe(`resolve based on path-type: ${isWin32 ? "win32" : "posix"} (${
326+
path.sep
327+
})`, () => {
328+
// path.join/path.resolve work based on process.platform,
329+
// and we only have to actualize independently passed paths with platform-based separator
330+
// to make sure that it works the same on different platforms
331+
332+
testResolve(
333+
"absolute path",
334+
fixtures,
335+
path.join(fixtures, "main1.js"),
336+
path.join(fixtures, "main1.js")
337+
);
338+
339+
testResolve(
340+
"file with .js",
341+
fixtures,
342+
`.${path.sep}main1.js`,
343+
path.join(fixtures, "main1.js")
344+
);
345+
testResolve(
346+
"file without extension",
347+
fixtures,
348+
`.${path.sep}main1`,
349+
path.join(fixtures, "main1.js")
350+
);
351+
testResolve(
352+
"another file with .js",
353+
fixtures,
354+
`.${path.sep}a.js`,
355+
path.join(fixtures, "a.js")
356+
);
357+
testResolve(
358+
"another file without extension",
359+
fixtures,
360+
`.${path.sep}a`,
361+
path.join(fixtures, "a.js")
362+
);
363+
testResolve(
364+
"file in module with .js",
365+
fixtures,
366+
`m1${path.sep}a.js`,
367+
path.join(fixtures, "node_modules", "m1", "a.js")
368+
);
369+
testResolve(
370+
"file in module without extension",
371+
fixtures,
372+
`m1${path.sep}a`,
373+
path.join(fixtures, "node_modules", "m1", "a.js")
374+
);
375+
testResolve(
376+
"another file in module without extension",
377+
fixtures,
378+
`complexm${path.sep}step1`,
379+
path.join(fixtures, "node_modules", "complexm", "step1.js")
380+
);
381+
testResolve(
382+
"from submodule to file in sibling module",
383+
path.join(fixtures, "node_modules", "complexm"),
384+
`m2${path.sep}b.js`,
385+
path.join(fixtures, "node_modules", "m2", "b.js")
386+
);
387+
testResolve(
388+
"from submodule to file in sibling of parent module",
389+
path.join(fixtures, "node_modules", "complexm", "web_modules", "m1"),
390+
`m2${path.sep}b.js`,
391+
path.join(fixtures, "node_modules", "m2", "b.js")
392+
);
393+
testResolve(
394+
"from nested directory to overwritten file in module",
395+
path.join(fixtures, "multiple_modules"),
396+
`m1${path.sep}a.js`,
397+
path.join(fixtures, "multiple_modules", "node_modules", "m1", "a.js")
398+
);
399+
testResolve(
400+
"from nested directory to not overwritten file in module",
401+
path.join(fixtures, "multiple_modules"),
402+
`m1${path.sep}b.js`,
403+
path.join(fixtures, "node_modules", "m1", "b.js")
404+
);
405+
406+
testResolve(
407+
"file with query",
408+
fixtures,
409+
`.${path.sep}main1.js?query`,
410+
path.join(fixtures, "main1.js") + "?query"
411+
);
412+
testResolve(
413+
"file with fragment",
414+
fixtures,
415+
`.${path.sep}main1.js#fragment`,
416+
path.join(fixtures, "main1.js") + "#fragment"
417+
);
418+
testResolve(
419+
"file with fragment and query",
420+
fixtures,
421+
`.${path.sep}main1.js#fragment?query`,
422+
path.join(fixtures, "main1.js") + "#fragment?query"
423+
);
424+
testResolve(
425+
"file with query and fragment",
426+
fixtures,
427+
`.${path.sep}main1.js?#fragment`,
428+
path.join(fixtures, "main1.js") + "?#fragment"
429+
);
430+
431+
testResolve(
432+
"file in module with query",
433+
fixtures,
434+
`m1${path.sep}a?query`,
435+
path.join(fixtures, "node_modules", "m1", "a.js") + "?query"
436+
);
437+
testResolve(
438+
"file in module with fragment",
439+
fixtures,
440+
`m1${path.sep}a#fragment`,
441+
path.join(fixtures, "node_modules", "m1", "a.js") + "#fragment"
442+
);
443+
testResolve(
444+
"file in module with fragment and query",
445+
fixtures,
446+
`m1${path.sep}a#fragment?query`,
447+
path.join(fixtures, "node_modules", "m1", "a.js") + "#fragment?query"
448+
);
449+
testResolve(
450+
"file in module with query and fragment",
451+
fixtures,
452+
`m1${path.sep}a?#fragment`,
453+
path.join(fixtures, "node_modules", "m1", "a.js") + "?#fragment"
454+
);
455+
456+
testResolveContext(
457+
"context for fixtures",
458+
fixtures,
459+
`.${path.sep}`,
460+
fixtures
461+
);
462+
testResolveContext(
463+
"context for fixtures/lib",
464+
fixtures,
465+
`.${path.sep}lib`,
466+
path.join(fixtures, "lib")
467+
);
468+
testResolveContext(
469+
"context for fixtures with ..",
470+
fixtures,
471+
`.${path.sep}lib${path.sep}..${path.sep}..${path.sep}fixtures${path.sep}.${path.sep}lib${path.sep}..`,
472+
fixtures
473+
);
474+
475+
testResolveContext(
476+
"context for fixtures with query",
477+
fixtures,
478+
`.${path.sep}?query`,
479+
fixtures + "?query"
480+
);
481+
482+
testResolve(
483+
"differ between directory and file, resolve file",
484+
fixtures,
485+
`.${path.sep}dirOrFile`,
486+
path.join(fixtures, "dirOrFile.js")
487+
);
488+
testResolve(
489+
"differ between directory and file, resolve directory",
490+
fixtures,
491+
`.${path.sep}dirOrFile${path.sep}`,
492+
path.join(fixtures, "dirOrFile", "index.js")
493+
);
494+
495+
testResolve(
496+
"find node_modules outside of node_modules",
497+
path.join(fixtures, "browser-module", "node_modules"),
498+
`m1${path.sep}a`,
499+
path.join(fixtures, "node_modules", "m1", "a.js")
500+
);
501+
502+
testResolve(
503+
"don't crash on main field pointing to self",
504+
fixtures,
505+
`.${path.sep}main-field-self`,
506+
path.join(fixtures, "main-field-self", "index.js")
507+
);
508+
509+
testResolve(
510+
"don't crash on main field pointing to self",
511+
fixtures,
512+
`.${path.sep}main-field-self2`,
513+
path.join(fixtures, "main-field-self2", "index.js")
514+
);
515+
516+
testResolve(
517+
"handle fragment edge case (no fragment)",
518+
fixtures,
519+
`.${path.sep}no#fragment${path.sep}#${path.sep}#`,
520+
path.join(fixtures, "no\0#fragment/\0#", "\0#.js")
521+
);
522+
523+
testResolve(
524+
"handle fragment edge case (fragment)",
525+
fixtures,
526+
`.${path.sep}no#fragment${path.sep}#${path.sep}`,
527+
path.join(fixtures, "no.js") + `#fragment${path.sep}#${path.sep}`
528+
);
529+
530+
testResolve(
531+
"handle fragment escaping",
532+
fixtures,
533+
`.${path.sep}no\0#fragment${path.sep}\0#${path.sep}\0##fragment`,
534+
path.join(fixtures, "no\0#fragment/\0#", "\0#.js") + "#fragment"
535+
);
536+
537+
it("should correctly resolve", function (done) {
538+
const issue238 = path.resolve(fixtures, "issue-238");
539+
540+
issue238Resolve(
541+
path.resolve(issue238, "./src/common"),
542+
"config/myObjectFile",
543+
function (err, filename) {
544+
if (err) done(err);
545+
expect(filename).toBeDefined();
546+
expect(filename).toEqual(
547+
path.resolve(issue238, "./src/common/config/myObjectFile.js")
548+
);
549+
done();
550+
}
551+
);
552+
});
553+
554+
it("should correctly resolve with preferRelative", function (done) {
555+
preferRelativeResolve(fixtures, "main1.js", function (err, filename) {
556+
if (err) done(err);
557+
expect(filename).toBeDefined();
558+
expect(filename).toEqual(path.join(fixtures, "main1.js"));
559+
done();
560+
});
561+
});
562+
563+
it(`should correctly resolve with preferRelative (separator: ${path.sep})`, function (done) {
564+
preferRelativeResolve(
565+
fixtures,
566+
`m1${path.sep}a.js`,
567+
function (err, filename) {
568+
if (err) done(err);
569+
expect(filename).toBeDefined();
570+
expect(filename).toEqual(
571+
path.join(fixtures, "node_modules", "m1", "a.js")
572+
);
573+
done();
574+
}
575+
);
576+
});
577+
578+
it("should not crash when passing undefined as path", done => {
579+
// @ts-expect-error testing invalid arguments
580+
resolve(fixtures, undefined, err => {
581+
expect(err).toBeInstanceOf(Error);
582+
done();
583+
});
584+
});
585+
586+
it(`should not crash when passing undefined as context (separator: ${path.sep})`, done => {
587+
// @ts-expect-error testing invalid arguments
588+
resolve({}, undefined, `.${path.sep}test${path.sep}resolve.js`, err => {
589+
expect(err).toBeInstanceOf(Error);
590+
done();
591+
});
592+
});
593+
594+
it("should not crash when passing undefined everywhere", done => {
595+
// @ts-expect-error testing invalid arguments
596+
resolve(undefined, undefined, undefined, undefined, err => {
597+
expect(err).toBeInstanceOf(Error);
598+
done();
599+
});
600+
});
601+
});

0 commit comments

Comments
 (0)