Skip to content

Commit 9fc5d22

Browse files
authored
Merge pull request #275 from snyk/chore/pnpm-alias-tests
chore: add pnpm tests for aliases
2 parents 828daad + 2272fee commit 9fc5d22

File tree

19 files changed

+17040
-0
lines changed

19 files changed

+17040
-0
lines changed

test/jest/dep-graph-builders/aliases.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { readFileSync } from 'fs';
33
import {
44
buildDepTreeFromFiles,
55
parseNpmLockV2Project,
6+
parsePnpmProject,
67
parseYarnLockV1Project,
78
parseYarnLockV2Project,
89
} from '../../../lib/';
@@ -454,3 +455,112 @@ describe('Testing aliases for npm', () => {
454455
expect(() => JSON.parse(JSON.stringify(newDepGraph))).not.toThrow();
455456
});
456457
});
458+
459+
describe.each(['pnpm-lock-v5', 'pnpm-lock-v6', 'pnpm-lock-v9'])(
460+
'Testing aliases for pnpm %s',
461+
(lockFileVersionPath) => {
462+
it('match aliased package', async () => {
463+
const fixtureName = 'pnpm-lock';
464+
const pkgJsonContent = readFileSync(
465+
join(
466+
__dirname,
467+
`./fixtures/aliases/pnpm/${lockFileVersionPath}/${fixtureName}/package.json`,
468+
),
469+
'utf8',
470+
);
471+
const pkgLockContent = readFileSync(
472+
join(
473+
__dirname,
474+
`./fixtures/aliases/pnpm/${lockFileVersionPath}/${fixtureName}/pnpm-lock.yaml`,
475+
),
476+
'utf8',
477+
);
478+
479+
const newDepGraph = await parsePnpmProject(
480+
pkgJsonContent,
481+
pkgLockContent,
482+
{
483+
includeDevDeps: true,
484+
includePeerDeps: true,
485+
includeOptionalDeps: true,
486+
strictOutOfSync: true,
487+
pruneWithinTopLevelDeps: true,
488+
},
489+
);
490+
491+
expect(newDepGraph).toBeDefined;
492+
expect(() => JSON.parse(JSON.stringify(newDepGraph))).not.toThrow();
493+
expect(JSON.stringify(newDepGraph)).toContain('@yao-pkg/pkg');
494+
expect(JSON.stringify(newDepGraph)).not.toContain('"pkg"');
495+
});
496+
it('match aliased package also used in transitive deps', async () => {
497+
const fixtureName = 'pnpm-lock-with-npm-alias-also-on-transitive';
498+
const pkgJsonContent = readFileSync(
499+
join(
500+
__dirname,
501+
`./fixtures/aliases/pnpm/${lockFileVersionPath}/${fixtureName}/package.json`,
502+
),
503+
'utf8',
504+
);
505+
const pkgLockContent = readFileSync(
506+
join(
507+
__dirname,
508+
`./fixtures/aliases/pnpm/${lockFileVersionPath}/${fixtureName}/pnpm-lock.yaml`,
509+
),
510+
'utf8',
511+
);
512+
513+
const newDepGraph = await parsePnpmProject(
514+
pkgJsonContent,
515+
pkgLockContent,
516+
{
517+
includeDevDeps: true,
518+
includePeerDeps: true,
519+
includeOptionalDeps: true,
520+
strictOutOfSync: true,
521+
pruneWithinTopLevelDeps: true,
522+
},
523+
);
524+
525+
expect(newDepGraph).toBeDefined;
526+
expect(() => JSON.parse(JSON.stringify(newDepGraph))).not.toThrow();
527+
expect(JSON.stringify(newDepGraph)).toContain('@yao-pkg/pkg');
528+
expect(JSON.stringify(newDepGraph)).not.toContain('"pkg"');
529+
});
530+
531+
it('ignore aliased package in transitive deps not throwing out of sync error', async () => {
532+
const fixtureName = 'pnpm-lock-with-npm-prefixed-sub-dep-version';
533+
const pkgJsonContent = readFileSync(
534+
join(
535+
__dirname,
536+
`./fixtures/aliases/pnpm/${lockFileVersionPath}/${fixtureName}/package.json`,
537+
),
538+
'utf8',
539+
);
540+
const pkgLockContent = readFileSync(
541+
join(
542+
__dirname,
543+
`./fixtures/aliases/pnpm/${lockFileVersionPath}/${fixtureName}/pnpm-lock.yaml`,
544+
),
545+
'utf8',
546+
);
547+
548+
const newDepGraph = await parsePnpmProject(
549+
pkgJsonContent,
550+
pkgLockContent,
551+
{
552+
includeDevDeps: true,
553+
includePeerDeps: true,
554+
includeOptionalDeps: true,
555+
strictOutOfSync: true,
556+
pruneWithinTopLevelDeps: true,
557+
},
558+
);
559+
560+
expect(newDepGraph).toBeDefined;
561+
expect(() => JSON.parse(JSON.stringify(newDepGraph))).not.toThrow();
562+
expect(JSON.stringify(newDepGraph)).toContain('@yao-pkg/pkg');
563+
expect(JSON.stringify(newDepGraph)).not.toContain('"pkg"');
564+
});
565+
},
566+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "npmalias",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"author": "",
9+
"license": "ISC",
10+
"description": "",
11+
"dependencies": {
12+
"ms": "^2.1.3",
13+
"pkg": "npm:@yao-pkg/pkg@^6.4.1",
14+
"semantic-release": "^22.0.10"
15+
}
16+
}

0 commit comments

Comments
 (0)