Files
pnpm/lockfile/pruner/test/index.ts
Zoltan Kochan 187049055f chore: upgrade @typescript/native-preview to 7.0.0-dev.20260421.2 (#11332)
* chore: upgrade @typescript/native-preview to 7.0.0-dev.20260421.2

- Add explicit `types: ["node"]` to the shared tsconfig because tsgo
  20260421 no longer auto-acquires `@types/*` from `node_modules`.
- Refactor test files to explicitly import jest globals (`describe`,
  `it`, `test`, `expect`, `beforeEach`, etc.) from `@jest/globals`
  instead of relying on `@types/jest` ambient declarations. Under the
  new tsgo build, `import { jest } from '@jest/globals'` shadows the
  ambient `jest` namespace, breaking `@types/jest`'s `declare var
  describe: jest.Describe;` globals.
- Add `@jest/globals` to each package's devDependencies where tests
  now import from it, and add `@types/node` to packages that need it
  but were relying on hoisted resolution.
- Replace `fail()` calls with `throw new Error(...)` since `fail` is
  no longer globally available.

* chore: fix remaining tsgo type-strictness errors

- Strip `as <PnpmType>` casts on objects passed to toMatchObject /
  toStrictEqual / toEqual; @jest/globals rejects the typed objects
  (which include AsymmetricMatchers) vs. the repo-specific type.
- Type `jest.fn<...>()` explicitly where the mock's signature matters
  for toHaveBeenCalledWith.
- Replace `beforeEach(() => X)` with `beforeEach(() => { X })` so the
  return value is void, as the stricter jest typing requires.
- Use `expect.objectContaining({...})` in one place where the full
  expected object triggered stricter type resolution.
- Cast `prompt.mock.calls` arg through `as unknown as Record<...>[]`
  for patch.test.ts's nested-array matchers.
- Fix off-by-one `<reference path>` in pnpm/test/getConfig.test.ts
  that only surfaced now.
- Move `@jest/globals` from devDependencies to dependencies in the
  two `__utils__` packages that import it from `src/`.
- Clean up unused imports from the @jest/globals migration.

* chore: address Copilot review on #11332

- Move misplaced `@jest/globals` imports to the top import block in
  checkEngine, run.ts, and workspace/root-finder tests where the
  script dropped them below executable code.
- Replace `try { await x(); throw new Error('should have thrown') } catch`
  in bins/linker, lockfile/fs, and resolving/local-resolver tests with
  `await expect(x()).rejects.toMatchObject({...})`. The old pattern
  swallowed an unrelated `throw` if the under-test call silently
  succeeded, which would fail on the catch-block assertion with a
  misleading message.
2026-04-21 22:50:40 +02:00

846 lines
19 KiB
TypeScript

/// <reference path="../../../__typings__/local.d.ts"/>
import { expect, test } from '@jest/globals'
import { LOCKFILE_VERSION, WANTED_LOCKFILE } from '@pnpm/constants'
import {
pruneLockfile,
pruneSharedLockfile,
} from '@pnpm/lockfile.pruner'
import type { DepPath, ProjectId } from '@pnpm/types'
import yaml from 'yaml-tag'
const DEFAULT_OPTS = {
warn (_msg: string) {
// ignore
},
}
test('remove one redundant package', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@2.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {
'is-positive': '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'is-positive@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
test('remove redundant linked package', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-positive': 'link:../is-positive',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
specifiers: {},
},
},
lockfileVersion: LOCKFILE_VERSION,
})
})
test('keep all', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-negative': '1.0.0',
'is-positive': '1.0.0',
},
specifiers: {
'is-negative': '^1.0.0',
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['is-negative@1.0.0' as DepPath]: {
dependencies: {
'is-positive': '2.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@2.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {
'is-negative': '^1.0.0',
'is-positive': '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
dependencies: {
'is-negative': '1.0.0',
'is-positive': '1.0.0',
},
specifiers: {
'is-negative': '^1.0.0',
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'is-negative@1.0.0': {
dependencies: {
'is-positive': '2.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'is-positive@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'is-positive@2.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
test('optional dependency should have optional = true', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'parent-of-foo': '1.0.0',
'pkg-with-good-optional': '1.0.0',
},
optionalDependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
'parent-of-foo': '1.0.0',
'pkg-with-good-optional': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['foo-child@1.0.0' as DepPath]: {
optional: true,
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['foo@1.0.0' as DepPath]: {
dependencies: {
'foo-child': '1.0.0',
},
optional: true,
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['parent-of-foo@1.0.0' as DepPath]: {
dependencies: {
foo: '1.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['pkg-with-good-optional@1.0.0' as DepPath]: {
optionalDependencies: {
foo: '1.0.0',
'is-positive': '1.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {
'parent-of-foo': '1.0.0',
'pkg-with-good-optional': '^1.0.0',
},
optionalDependencies: {
'is-positive': '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
dependencies: {
'parent-of-foo': '1.0.0',
'pkg-with-good-optional': '1.0.0',
},
optionalDependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
'parent-of-foo': '1.0.0',
'pkg-with-good-optional': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'foo-child@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'foo@1.0.0': {
dependencies: {
'foo-child': '1.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'is-positive@1.0.0': {
optional: true,
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'parent-of-foo@1.0.0': {
dependencies: {
foo: '1.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'pkg-with-good-optional@1.0.0': {
optionalDependencies: {
foo: '1.0.0',
'is-positive': '1.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
test('optional dependency should not have optional = true if used not only as optional', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-positive': '1.0.0',
'pkg-with-good-optional': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
'pkg-with-good-optional': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['pkg-with-good-optional@1.0.0' as DepPath]: {
optionalDependencies: {
'is-positive': '1.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {
'is-positive': '^1.0.0',
'pkg-with-good-optional': '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
dependencies: {
'is-positive': '1.0.0',
'pkg-with-good-optional': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
'pkg-with-good-optional': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'is-positive@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'pkg-with-good-optional@1.0.0': {
optionalDependencies: {
'is-positive': '1.0.0',
},
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
test('subdependency is both optional and dev', () => {
expect(pruneLockfile(yaml`
importers:
.:
dependencies:
prod-parent: 1.0.0
devDependencies:
parent: 1.0.0
specifiers:
parent: ^1.0.0
prod-parent: ^1.0.0
lockfileVersion: 5
packages:
parent@1.0.0:
optionalDependencies:
subdep: 1.0.0
subdep2: 1.0.0
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
prod-parent@1.0.0:
dependencies:
subdep2: 1.0.0
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
subdep@1.0.0:
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
subdep2@1.0.0:
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
`, {
name: 'foo',
version: '1.0.0',
dependencies: {
'prod-parent': '^1.0.0',
},
devDependencies: {
parent: '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual(yaml`
importers:
.:
dependencies:
prod-parent: 1.0.0
devDependencies:
parent: 1.0.0
specifiers:
parent: ^1.0.0
prod-parent: ^1.0.0
lockfileVersion: 5
packages:
parent@1.0.0:
optionalDependencies:
subdep: 1.0.0
subdep2: 1.0.0
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
prod-parent@1.0.0:
dependencies:
subdep2: 1.0.0
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
subdep@1.0.0:
optional: true
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
subdep2@1.0.0:
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
`)
})
test('optional = true is removed if dependency is used both as optional and prod dependency', () => {
expect(pruneLockfile(yaml`
importers:
.:
dependencies:
foo: inflight@1.0.6
optionalDependencies:
inflight: 1.0.6
specifiers:
foo: 'npm:inflight@^1.0.6'
inflight: ^1.0.6
lockfileVersion: 5
packages:
inflight@1.0.6:
optional: true
dependencies:
once: 1.4.0
wrappy: 1.0.2
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
once@1.4.0:
optional: true
dependencies:
wrappy: 1.0.2
resolution:
integrity: sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
wrappy@1.0.2:
optional: true
resolution:
integrity: sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
`, {
name: 'foo',
version: '1.0.0',
dependencies: {
foo: 'npm:inflight@^1.0.6',
},
optionalDependencies: {
inflight: '^1.0.6',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual(yaml`
importers:
.:
dependencies:
foo: inflight@1.0.6
optionalDependencies:
inflight: 1.0.6
specifiers:
foo: 'npm:inflight@^1.0.6'
inflight: ^1.0.6
lockfileVersion: 5
packages:
inflight@1.0.6:
dependencies:
once: 1.4.0
wrappy: 1.0.2
resolution:
integrity: sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
once@1.4.0:
dependencies:
wrappy: 1.0.2
resolution:
integrity: sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
wrappy@1.0.2:
resolution:
integrity: sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
`)
})
test('remove dependencies that are not in the package', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-positive': '1.0.0',
},
devDependencies: {
'is-negative': '1.0.0',
},
optionalDependencies: {
fsevents: '1.0.0',
},
specifiers: {
fsevents: '^1.0.0',
'is-negative': '^1.0.0',
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['fsevents@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-negative@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
specifiers: {},
},
},
lockfileVersion: LOCKFILE_VERSION,
})
})
test(`ignore dependencies that are in package.json but are not in ${WANTED_LOCKFILE}`, () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {
'is-negative': '^1.0.0',
'is-positive': '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'is-positive@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
// this test may be redundant
test('keep lockfileMinorVersion, if present', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: '5.2',
packages: {
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {
'is-positive': '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: '5.2',
packages: {
'is-positive@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
test('keep linked package even if it is not in package.json', () => {
expect(pruneLockfile({
importers: {
['.' as ProjectId]: {
dependencies: {
'is-negative': '1.0.0',
'is-positive': 'link:../is-positive',
},
specifiers: {
'is-negative': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['is-negative@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'foo',
version: '1.0.0',
dependencies: {
'is-negative': '^1.0.0',
},
}, '.' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'.': {
dependencies: {
'is-negative': '1.0.0',
'is-positive': 'link:../is-positive',
},
specifiers: {
'is-negative': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'is-negative@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
test("prune: don't remove package used by another importer", () => {
expect(pruneLockfile({
importers: {
['packages/package-1' as ProjectId]: {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
['packages/package-2' as ProjectId]: {
dependencies: {
'is-negative': '1.0.0',
},
specifiers: {
'is-negative': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['is-negative@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@2.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, {
name: 'project-2',
version: '1.0.0',
dependencies: { 'is-negative': '^1.0.0' },
}, 'packages/package-2' as ProjectId, DEFAULT_OPTS)).toStrictEqual({
importers: {
'packages/package-1': {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
'packages/package-2': {
dependencies: {
'is-negative': '1.0.0',
},
specifiers: {
'is-negative': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'is-negative@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
'is-positive@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})
test('pruneSharedLockfile: remove one redundant package', () => {
expect(pruneSharedLockfile({
importers: {
['packages/package-1' as ProjectId]: {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
['is-positive@1.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
['is-positive@2.0.0' as DepPath]: {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
}, DEFAULT_OPTS)).toStrictEqual({
importers: {
'packages/package-1': {
dependencies: {
'is-positive': '1.0.0',
},
specifiers: {
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'is-positive@1.0.0': {
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
},
},
})
})