Files
pnpm/installing/deps-installer/test/install/fixLockfile.ts
Brandon Cheng 41dc031a67 test: use resolution-mode=highest in tests (#10989)
* fix: configure default resolution-mode to highest in pkg-manager/core

* test: update catalog tests for resolution-mode=highest

* test: fix `--fix-lockfile` test for new resolution-mode default

```
  ● fix broken lockfile with --fix-lockfile

    expect(received).toBeTruthy()

    Received: undefined

      55 |   const lockfile: LockfileFile = readYamlFileSync(WANTED_LOCKFILE)
      56 |   expect(Object.keys(lockfile.packages as PackageSnapshots)).toHaveLength(2)
    > 57 |   expect(lockfile.packages?.['@types/semver@5.3.31']).toBeTruthy()
         |                                                       ^
      58 |   expect(lockfile.packages?.['@types/semver@5.3.31']?.resolution).toEqual({
      59 |     integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==',
      60 |   })

      at Object.<anonymous> (test/install/fixLockfile.ts:57:55)
```

* test: fix lockfile conflict test

  ● a lockfile v6 with merge conflicts is autofixed

    expect(received).toHaveProperty(path, value)

    Expected path: "version"

    Expected value: "100.1.0"
    Received value: "101.0.0"

      1284 |
      1285 |   const lockfile = project.readLockfile()
    > 1286 |   expect(lockfile.importers?.['.'].dependencies?.['@pnpm.e2e/dep-of-pkg-with-1-dep']).toHaveProperty('version', '100.1.0')
           |                                                                                       ^
      1287 | })
      1288 |
      1289 | test('a lockfile with duplicate keys is fixed', async () => {

      at Object.<anonymous> (test/lockfile.ts:1286:87)

* test: fix deploy shared lockfile test

  ● deploy with a shared lockfile that has peer dependencies suffix in workspace package dependency paths

    expect(received).toMatchObject(expected)

    - Expected  - 6
    + Received  + 1

    @@ -1,11 +1,11 @@
      Object {
        "importers": Object {
          "packages/project-0": Object {
            "dependencies": Object {
              "project-1": Object {
    -           "version": "file:packages/project-1(is-negative@1.0.0)(project-2@file:packages/project-2(is-positive@1.0.0))",
    +           "version": "file:packages/project-1(is-negative@2.1.0)(project-2@file:packages/project-2(is-positive@1.0.0))",
              },
              "project-2": Object {
                "version": "file:packages/project-2(is-positive@1.0.0)",
              },
            },
    @@ -31,13 +31,8 @@
              "type": "directory",
            },
          },
        },
        "snapshots": Object {
    -     "project-1@file:packages/project-1(is-negative@1.0.0)(project-2@file:packages/project-2(is-positive@1.0.0))": Object {
    -       "dependencies": Object {
    -         "project-2": "file:packages/project-2(is-positive@1.0.0)",
    -       },
    -     },
          "project-2@file:packages/project-2(is-positive@1.0.0)": Object {},
        },
      }

      950 |     workspaceDir: process.cwd(),
      951 |   })
    > 952 |   expect(assertProject('.').readLockfile()).toMatchObject({
          |                                             ^
      953 |     importers: {
      954 |       'packages/project-0': {
      955 |         dependencies: {

      at Object.<anonymous> (test/shared-lockfile.test.ts:952:45)

* test: fix injectLocalPackages test
2026-03-21 23:21:04 +01:00

266 lines
9.0 KiB
TypeScript

import path from 'node:path'
import { LOCKFILE_VERSION, WANTED_LOCKFILE } from '@pnpm/constants'
import { install, type MutatedProject, mutateModules } from '@pnpm/installing.deps-installer'
import type { LockfileFile, PackageSnapshots } from '@pnpm/lockfile.fs'
import { prepareEmpty, preparePackages } from '@pnpm/prepare'
import type { ProjectRootDir } from '@pnpm/types'
import { readYamlFileSync } from 'read-yaml-file'
import { writeYamlFileSync } from 'write-yaml-file'
import { testDefaults } from '../utils/index.js'
test('fix broken lockfile with --fix-lockfile', async () => {
prepareEmpty()
writeYamlFileSync(WANTED_LOCKFILE, {
dependencies: {
'@types/semver': {
specifier: '5.3.31',
version: '5.3.31',
},
},
devDependencies: {
fsevents: {
specifier: '2.3.2',
version: '2.3.2',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/@types/semver@5.3.31': {
// resolution: {
// integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==',
// },
},
'/core-js-pure@3.16.2': {
resolution: {
integrity: 'sha512-oxKe64UH049mJqrKkynWp6Vu0Rlm/BTXO/bJZuN2mmR3RtOFNepLlSWDd1eo16PzHpQAoNG97rLU1V/YxesJjw==',
},
// dev: true
},
},
}, { lineWidth: 1000 })
await install({
dependencies: {
'@types/semver': '5.3.31',
},
devDependencies: {
'core-js-pure': '3.16.2',
},
}, testDefaults({ fixLockfile: true }))
const lockfile: LockfileFile = readYamlFileSync(WANTED_LOCKFILE)
expect(Object.keys(lockfile.packages as PackageSnapshots)).toHaveLength(2)
expect(lockfile.packages?.['@types/semver@5.3.31']).toBeTruthy()
expect(lockfile.packages?.['@types/semver@5.3.31']?.resolution).toEqual({
integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==',
})
expect(lockfile.packages?.['core-js-pure@3.16.2']).toBeTruthy()
expect(lockfile.packages?.['core-js-pure@3.16.2']?.resolution).toEqual({
integrity: 'sha512-oxKe64UH049mJqrKkynWp6Vu0Rlm/BTXO/bJZuN2mmR3RtOFNepLlSWDd1eo16PzHpQAoNG97rLU1V/YxesJjw==',
})
})
test('--fix-lockfile should preserve all locked dependencies version', async () => {
preparePackages([
{
location: '.',
package: { name: 'root' },
},
{
location: 'project-1',
package: { name: 'project-1', dependencies: { '@babel/runtime-corejs3': '7.15.3' } },
},
{
location: 'project-2',
package: { name: 'project-2', dependencies: { '@babel/runtime-corejs3': '7.15.4' } },
},
])
const importers: MutatedProject[] = [
{
mutation: 'install',
rootDir: path.resolve('.') as ProjectRootDir,
},
{
mutation: 'install',
rootDir: path.resolve('project-1') as ProjectRootDir,
},
{
mutation: 'install',
rootDir: path.resolve('project-2') as ProjectRootDir,
},
]
/**
* project-1 depends on @babel/runtime-corejs3@7.15.3 while project-2 depends on @babel/runtime-corejs3@7.15.4,
* and @babel/runtime-corejs3@7.15.3 depends on core-js-pure@3.17.2 while @babel/runtime-corejs3@7.15.4 depends on core-js-pure@3.17.3
* --fix-lockfile should not change the locked dependency version and only adding missing fields in this scene
*/
writeYamlFileSync(WANTED_LOCKFILE, {
lockfileVersion: LOCKFILE_VERSION,
importers: {
'.': {},
'project-1': {
dependencies: {
'@babel/runtime-corejs3': {
specifier: '7.15.3',
version: '7.15.3',
},
},
},
'project-2': {
dependencies: {
'@babel/runtime-corejs3': {
specifier: '7.15.4',
version: '7.15.4',
},
},
},
},
packages: {
'@babel/runtime-corejs3@7.15.3': {
resolution: { integrity: 'sha512-30A3lP+sRL6ml8uhoJSs+8jwpKzbw8CqBvDc1laeptxPm5FahumJxirigcbD2qTs71Sonvj1cyZB0OKGAmxQ+A==' },
// engines: { node: '>=6.9.0' },
},
'@babel/runtime-corejs3@7.15.4': {
resolution: { integrity: 'sha512-lWcAqKeB624/twtTc3w6w/2o9RqJPaNBhPGK6DKLSiwuVWC7WFkypWyNg+CpZoyJH0jVzv1uMtXZ/5/lQOLtCg==' },
engines: { node: '>=6.9.0' },
},
'core-js-pure@3.17.2': {
resolution: { integrity: 'sha512-2VV7DlIbooyTI7Bh+yzOOWL9tGwLnQKHno7qATE+fqZzDKYr6llVjVQOzpD/QLZFgXDPb8T71pJokHEZHEYJhQ==' },
},
'core-js-pure@3.17.3': {
// resolution: { integrity: 'sha512-YusrqwiOTTn8058JDa0cv9unbXdIiIgcgI9gXso0ey4WgkFLd3lYlV9rp9n7nDCsYxXsMDTjA4m1h3T348mdlQ==' },
},
'regenerator-runtime@0.13.9': {
resolution: { integrity: 'sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==' },
},
},
snapshots: {
'@babel/runtime-corejs3@7.15.3': {
resolution: { integrity: 'sha512-30A3lP+sRL6ml8uhoJSs+8jwpKzbw8CqBvDc1laeptxPm5FahumJxirigcbD2qTs71Sonvj1cyZB0OKGAmxQ+A==' },
dependencies: {
'core-js-pure': '3.17.2',
'regenerator-runtime': '0.13.9',
},
dev: false,
},
'@babel/runtime-corejs3@7.15.4': {
dependencies: {
'core-js-pure': '3.17.3',
'regenerator-runtime': '0.13.9',
},
dev: false,
},
'core-js-pure@3.17.2': {
dev: false,
},
'core-js-pure@3.17.3': {
// resolution: { integrity: 'sha512-YusrqwiOTTn8058JDa0cv9unbXdIiIgcgI9gXso0ey4WgkFLd3lYlV9rp9n7nDCsYxXsMDTjA4m1h3T348mdlQ==' },
// dev: false
},
'regenerator-runtime@0.13.9': {
// dev: false
},
},
}, { lineWidth: 1000 })
await mutateModules(importers, testDefaults({
fixLockfile: true,
lockfileOnly: true,
allProjects: [
{
buildIndex: 0,
manifest: {
name: 'root',
version: '1.0.0',
},
rootDir: path.resolve('.') as ProjectRootDir,
},
{
buildIndex: 0,
manifest: {
name: 'project-1',
version: '1.0.0',
dependencies: {
'@babel/runtime-corejs3': '7.15.3',
},
},
rootDir: path.resolve('project-1') as ProjectRootDir,
},
{
buildIndex: 0,
manifest: {
name: 'project-3',
version: '1.0.0',
dependencies: {
'@babel/runtime-corejs3': '7.15.4',
},
},
rootDir: path.resolve('project-2') as ProjectRootDir,
},
],
}))
const lockfile: LockfileFile = readYamlFileSync(WANTED_LOCKFILE)
expect(Object.keys(lockfile.packages as PackageSnapshots)).toHaveLength(5)
expect(lockfile.packages?.['@babel/runtime-corejs3@7.15.3']).toBeTruthy()
expect(lockfile.packages?.['@babel/runtime-corejs3@7.15.3']?.resolution).toEqual({
integrity: 'sha512-30A3lP+sRL6ml8uhoJSs+8jwpKzbw8CqBvDc1laeptxPm5FahumJxirigcbD2qTs71Sonvj1cyZB0OKGAmxQ+A==',
})
expect(lockfile.packages?.['@babel/runtime-corejs3@7.15.3']?.engines).toEqual({
node: '>=6.9.0',
})
expect(lockfile.packages?.['@babel/runtime-corejs3@7.15.4']).toBeTruthy()
expect(lockfile.packages?.['@babel/runtime-corejs3@7.15.4']?.resolution).toEqual({
integrity: 'sha512-lWcAqKeB624/twtTc3w6w/2o9RqJPaNBhPGK6DKLSiwuVWC7WFkypWyNg+CpZoyJH0jVzv1uMtXZ/5/lQOLtCg==',
})
expect(lockfile.packages?.['@babel/runtime-corejs3@7.15.4']?.engines).toEqual({
node: '>=6.9.0',
})
expect(lockfile.packages?.['core-js-pure@3.17.2']).toBeTruthy()
expect(lockfile.packages?.['core-js-pure@3.17.2']?.resolution).toHaveProperty('integrity', 'sha512-2VV7DlIbooyTI7Bh+yzOOWL9tGwLnQKHno7qATE+fqZzDKYr6llVjVQOzpD/QLZFgXDPb8T71pJokHEZHEYJhQ==')
expect(lockfile.packages?.['core-js-pure@3.17.3']).toBeTruthy()
expect(lockfile.packages?.['core-js-pure@3.17.3']?.resolution).toEqual({
integrity: 'sha512-YusrqwiOTTn8058JDa0cv9unbXdIiIgcgI9gXso0ey4WgkFLd3lYlV9rp9n7nDCsYxXsMDTjA4m1h3T348mdlQ==',
})
expect(lockfile.packages?.['regenerator-runtime@0.13.9']).toBeTruthy()
expect(lockfile.packages?.['regenerator-runtime@0.13.9']?.resolution).toEqual({
integrity: 'sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==',
})
})
test(
'--fix-lockfile should install successfully when package has no dependencies but has peer dependencies with version like 1.0.0_@pnpm+y@1.0.0',
async () => {
prepareEmpty()
const packages = {
dependencies: {
// @pnpm.e2e/has-has-y-peer-peer has no dependencies but has peer dependencies @pnpm.e2e/has-y-peer
// the version of @pnpm.e2e/has-y-peer will be 1.0.0_@pnpm+y@1.0.0
// version 1.0.0_@pnpm+y@1.0.0 should be parsed correctly
'@pnpm.e2e/has-has-y-peer-peer': '1.0.0',
'@pnpm.e2e/has-y-peer': '^1.0.0',
'@pnpm/y': '^1.0.0',
},
}
// install first time to generate lock file
await install(packages, testDefaults())
// install second time to check whether install successfully with lockfileOnly
await install(packages, testDefaults({
fixLockfile: true,
}))
}
)