Files
pnpm/pnpm11/installing/dedupe/check/test/dedupeDiffCheck.ts
Zoltan Kochan 9df134be6e fix(dedupe): sort check snapshot changes (#13451)
Sort dedupe check snapshot changes before rendering them so output does not depend on resolver insertion order. This aligns the TypeScript report with Rust pnpm, which already reports canonical lockfile-key order.

Related to pnpm/pnpm#13305.
2026-07-28 01:45:35 +02:00

189 lines
4.8 KiB
TypeScript

import { describe, expect, it } from '@jest/globals'
import {
calcDedupeCheckIssues,
DedupeCheckIssuesError,
dedupeDiffCheck,
} from '@pnpm/installing.dedupe.check'
import type { LockfileObject } from '@pnpm/lockfile.types'
import type { DepPath, ProjectId } from '@pnpm/types'
describe('dedupeDiffCheck', () => {
it('should have no changes for same lockfile', () => {
const lockfile: LockfileObject = {
importers: {
['.' as ProjectId]: {
specifiers: {},
},
},
lockfileVersion: 'testLockfileVersion',
}
expect(() => {
dedupeDiffCheck(lockfile, lockfile)
}).not.toThrow()
})
it('throws DedupeCheckIssuesError on changes', () => {
const before: LockfileObject = {
importers: {
['packages/a' as ProjectId]: {
specifiers: {
'is-positive': '^3.0.0',
},
dependencies: {
'is-positive': '3.0.0',
},
},
['packages/b' as ProjectId]: {
specifiers: {
'is-positive': '^3.1.0',
},
dependencies: {
'is-positive': '3.1.0',
},
},
},
packages: {
['is-positive@3.0.0' as DepPath]: {
resolution: {
integrity: 'sha512-JDkaKp5jWv24ZaFuYDKTcBrC/wBOHdjhzLDkgrrkJD/j7KqqXsGcAkex336qHoOFEajMy7bYqUgm0KH9/MzQvw==',
},
engines: {
node: '>=0.10.0',
},
},
['is-positive@3.1.0' as DepPath]: {
resolution: {
integrity: 'sha1-hX21hKG6XRyymAUn/DtsQ103sP0=',
},
engines: {
node: '>=0.10.0',
},
},
},
lockfileVersion: 'testLockfileVersion',
}
const after: LockfileObject = {
importers: {
['packages/a' as ProjectId]: {
specifiers: {
'is-positive': '^3.0.0',
},
dependencies: {
'is-positive': '3.1.0',
},
},
['packages/b' as ProjectId]: {
specifiers: {
'is-positive': '^3.1.0',
},
dependencies: {
'is-positive': '3.1.0',
},
},
},
packages: {
['is-positive@3.1.0' as DepPath]: {
resolution: {
integrity: 'sha1-hX21hKG6XRyymAUn/DtsQ103sP0=',
},
engines: {
node: '>=0.10.0',
},
},
},
lockfileVersion: 'testLockfileVersion',
}
expect(() => {
dedupeDiffCheck(before, after)
}).toThrow(new DedupeCheckIssuesError({
importerIssuesByImporterId: {
added: [],
removed: [],
updated: {
'packages/a': {
'is-positive': { type: 'updated', prev: '3.0.0', next: '3.1.0' },
},
},
},
packageIssuesByDepPath: {
added: [],
removed: ['/is-positive@3.0.0'],
updated: {},
},
}))
})
it('sorts snapshot changes independently of resolution order', () => {
const before: LockfileObject = {
importers: {
['.' as ProjectId]: {
specifiers: {},
},
},
packages: {
['z@1.0.0' as DepPath]: {
resolution: { integrity: 'sha512-z' },
},
['a@1.0.0' as DepPath]: {
resolution: { integrity: 'sha512-a' },
dependencies: {
zDependency: '1.0.0',
},
optionalDependencies: {
aDependency: '1.0.0',
},
},
},
lockfileVersion: 'testLockfileVersion',
}
const after: LockfileObject = {
importers: {
['.' as ProjectId]: {
specifiers: {},
},
},
packages: {
['z-new@1.0.0' as DepPath]: {
resolution: { integrity: 'sha512-z-new' },
},
['a-new@1.0.0' as DepPath]: {
resolution: { integrity: 'sha512-a-new' },
},
['z@1.0.0' as DepPath]: {
resolution: { integrity: 'sha512-z' },
dependencies: {
dependency: '2.0.0',
},
},
['a@1.0.0' as DepPath]: {
resolution: { integrity: 'sha512-a' },
dependencies: {
zDependency: '2.0.0',
},
optionalDependencies: {
aDependency: '2.0.0',
},
},
},
lockfileVersion: 'testLockfileVersion',
}
expect(calcDedupeCheckIssues(before, after).packageIssuesByDepPath).toEqual({
added: ['a-new@1.0.0', 'z-new@1.0.0'],
removed: [],
updated: {
'a@1.0.0': {
aDependency: { type: 'updated', prev: '1.0.0', next: '2.0.0' },
zDependency: { type: 'updated', prev: '1.0.0', next: '2.0.0' },
},
'z@1.0.0': {
dependency: { type: 'added', next: '2.0.0' },
},
},
})
})
})