Skip to content

Commit b387386

Browse files
author
Maël Nison
committed
Adds tests
1 parent f520df3 commit b387386

File tree

2 files changed

+115
-10
lines changed

2 files changed

+115
-10
lines changed

packages/pkg-tests/pkg-tests-specs/sources/pnp.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,108 @@ module.exports = makeTemporaryEnv => {
460460
),
461461
);
462462

463+
test(
464+
`it should support the 'paths' option from require.resolve (same dependency tree)`,
465+
makeTemporaryEnv(
466+
{
467+
private: true,
468+
workspaces: [`workspace-*`],
469+
},
470+
{
471+
plugNPlay: true,
472+
},
473+
async ({path, run, source}) => {
474+
await writeJson(`${path}/workspace-a/package.json`, {
475+
name: `workspace-a`,
476+
version: `1.0.0`,
477+
dependencies: {[`no-deps`]: `1.0.0`},
478+
});
479+
480+
await writeJson(`${path}/workspace-b/package.json`, {
481+
name: `workspace-b`,
482+
version: `1.0.0`,
483+
dependencies: {[`no-deps`]: `2.0.0`, [`one-fixed-dep`]: `1.0.0`},
484+
});
485+
486+
await run(`install`);
487+
488+
await expect(
489+
source(
490+
`require(require.resolve('no-deps', {paths: ${JSON.stringify([
491+
`${path}/workspace-a`,
492+
`${path}/workspace-b`,
493+
])}}))`,
494+
),
495+
).resolves.toMatchObject({
496+
name: `no-deps`,
497+
version: `1.0.0`,
498+
});
499+
},
500+
),
501+
);
502+
503+
// Skipped because not supported (we can't require files from within other dependency trees, since we couldn't
504+
// reconcile them together: dependency tree A could think that package X has deps Y@1 while dependency tree B
505+
// could think that X has deps Y@2 instead. Since they would share the same location on the disk, PnP wouldn't
506+
// be able to tell which one should be used)
507+
test.skip(
508+
`it should support the 'paths' option from require.resolve (different dependency trees)`,
509+
makeTemporaryEnv(
510+
{
511+
dependencies: {},
512+
},
513+
{
514+
plugNPlay: true,
515+
},
516+
async ({path, run, source}) => {
517+
await run(`install`);
518+
519+
const tmpA = await createTemporaryFolder();
520+
const tmpB = await createTemporaryFolder();
521+
522+
await writeJson(`${tmpA}/package.json`, {
523+
dependencies: {[`no-deps`]: `1.0.0`},
524+
});
525+
526+
await writeJson(`${tmpB}/package.json`, {
527+
dependencies: {[`no-deps`]: `2.0.0`, [`one-fixed-dep`]: `1.0.0`},
528+
});
529+
530+
await run(`install`, {
531+
cwd: tmpA,
532+
});
533+
534+
await run(`install`, {
535+
cwd: tmpB,
536+
});
537+
538+
await expect(
539+
source(`require(require.resolve('no-deps', {paths: ${JSON.stringify([tmpA, tmpB])}}))`),
540+
).resolves.toMatchObject({
541+
name: `no-deps`,
542+
version: `1.0.0`,
543+
});
544+
},
545+
),
546+
);
547+
548+
test(
549+
`using require.resolve with unsupported options should throw`,
550+
makeTemporaryEnv(
551+
{
552+
dependencies: {[`no-deps`]: `1.0.0`},
553+
},
554+
{
555+
plugNPlay: true,
556+
},
557+
async ({path, run, source}) => {
558+
await run(`install`);
559+
560+
await expect(source(`require.resolve('no-deps', {foobar: 42})`)).rejects.toBeTruthy();
561+
},
562+
),
563+
);
564+
463565
test(
464566
`it should load the index.js file when loading from a folder`,
465567
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {

src/util/generate-pnp-map-api.tpl.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -646,15 +646,18 @@ exports.setup = function setup() {
646646
if (!enableNativeHooks) {
647647
return originalModuleResolveFilename.call(Module, request, parent, isMain, options);
648648
}
649-
649+
650650
let issuers;
651-
651+
652652
if (options) {
653-
const optionNames = new Set(Object.keys(optionNames));
653+
const optionNames = new Set(Object.keys(options));
654654
optionNames.delete('paths');
655-
656-
if (options.size > 0) {
657-
throw makeError(`UNSUPPORTED`, `Some options passed to require() aren't supported by PnP yet (${Array.from(optionNames).join(', ')})`);
655+
656+
if (optionNames.size > 0) {
657+
throw makeError(
658+
`UNSUPPORTED`,
659+
`Some options passed to require() aren't supported by PnP yet (${Array.from(optionNames).join(', ')})`,
660+
);
658661
}
659662

660663
if (options.paths) {
@@ -665,15 +668,15 @@ exports.setup = function setup() {
665668
if (!issuers) {
666669
const issuerModule = getIssuerModule(parent);
667670
const issuer = issuerModule ? issuerModule.filename : `${process.cwd()}/`;
668-
671+
669672
issuers = [issuer];
670673
}
671-
674+
672675
let firstError;
673676

674677
for (const issuer of issuers) {
675678
let resolution;
676-
679+
677680
try {
678681
resolution = exports.resolveRequest(request, issuer);
679682
} catch (error) {
@@ -683,7 +686,7 @@ exports.setup = function setup() {
683686

684687
return resolution !== null ? resolution : request;
685688
}
686-
689+
687690
throw firstError;
688691
};
689692

0 commit comments

Comments
 (0)