mirror of
https://github.com/pnpm/pnpm.git
synced 2026-07-25 15:07:06 -04:00
* 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.
282 lines
9.5 KiB
TypeScript
282 lines
9.5 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
import { beforeEach, expect, jest, test } from '@jest/globals'
|
|
import { prepareEmpty } from '@pnpm/prepare'
|
|
import { fixtures } from '@pnpm/test-fixtures'
|
|
import { writeYamlFileSync } from 'write-yaml-file'
|
|
|
|
import { DEFAULT_OPTS } from './utils/index.js'
|
|
|
|
const originalModule = await import('@pnpm/logger')
|
|
jest.unstable_mockModule('@pnpm/logger', () => {
|
|
return {
|
|
...originalModule,
|
|
globalWarn: jest.fn(),
|
|
}
|
|
})
|
|
|
|
const { globalWarn } = await import('@pnpm/logger')
|
|
const { add, install } = await import('@pnpm/installing.commands')
|
|
|
|
const f = fixtures(import.meta.dirname)
|
|
// Track cumulative patchedDependencies across multiple addPatch calls
|
|
let currentPatchedDependencies: Record<string, string> = {}
|
|
|
|
beforeEach(() => {
|
|
jest.mocked(globalWarn).mockClear()
|
|
currentPatchedDependencies = {}
|
|
})
|
|
|
|
function addPatch (key: string, patchFixture: string, patchDest: string): Record<string, string> {
|
|
fs.mkdirSync(path.dirname(patchDest), { recursive: true })
|
|
fs.copyFileSync(patchFixture, patchDest)
|
|
|
|
currentPatchedDependencies = {
|
|
...currentPatchedDependencies,
|
|
[key]: patchDest,
|
|
}
|
|
|
|
writeYamlFileSync('pnpm-workspace.yaml', {
|
|
packages: ['.'],
|
|
patchedDependencies: currentPatchedDependencies,
|
|
})
|
|
|
|
return currentPatchedDependencies
|
|
}
|
|
|
|
const unpatchedModulesDir = (v: 1 | 2 | 3) => `node_modules/.pnpm/@pnpm.e2e+console-log@${v}.0.0/node_modules`
|
|
const unpatchedFilePath = (v: 1 | 2 | 3) => `${unpatchedModulesDir(v)}/@pnpm.e2e/console-log/index.js`
|
|
const unpatchedFileContent = (v: 1 | 2 | 3) => fs.readFileSync(unpatchedFilePath(v), 'utf-8')
|
|
const patchedModulesDir = 'node_modules/.pnpm/@pnpm.e2e+depends-on-console-log@1.0.0/node_modules'
|
|
const patchedFilePath = (v: 1 | 2 | 3) => `${patchedModulesDir}/console-log-${v}/index.js`
|
|
const patchedFileContent = (v: 1 | 2 | 3) => fs.readFileSync(patchedFilePath(v), 'utf-8')
|
|
|
|
test('bare package name as a patchedDependencies key should apply to all versions if all are applicable', async () => {
|
|
const patchFixture = f.find('patchedDependencies/console-log-replace-1st-line.patch')
|
|
prepareEmpty()
|
|
|
|
await add.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
}, ['@pnpm.e2e/depends-on-console-log@1.0.0'])
|
|
fs.rmSync('pnpm-lock.yaml')
|
|
|
|
const patchedDependencies = addPatch('@pnpm.e2e/console-log', patchFixture, 'patches/console-log.patch')
|
|
|
|
await install.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
frozenLockfile: false,
|
|
patchedDependencies,
|
|
})
|
|
|
|
{
|
|
const text = patchedFileContent(1)
|
|
expect(text).not.toBe(unpatchedFileContent(1))
|
|
expect(text).toContain('FIRST LINE')
|
|
expect(text).not.toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text.trim().split('\n')).toHaveLength(2)
|
|
}
|
|
|
|
{
|
|
const text = patchedFileContent(2)
|
|
expect(text).not.toBe(unpatchedFileContent(2))
|
|
expect(text).toContain('FIRST LINE')
|
|
expect(text).not.toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text).toContain('third line')
|
|
expect(text.trim().split('\n')).toHaveLength(3)
|
|
}
|
|
|
|
{
|
|
const text = patchedFileContent(3)
|
|
expect(text).not.toBe(unpatchedFileContent(3))
|
|
expect(text).toContain('FIRST LINE')
|
|
expect(text).not.toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text).toContain('third line')
|
|
expect(text).toContain('fourth line')
|
|
expect(text.trim().split('\n')).toHaveLength(4)
|
|
}
|
|
|
|
expect(globalWarn).not.toHaveBeenCalledWith(expect.stringContaining('Could not apply patch'))
|
|
})
|
|
|
|
test('bare package name as a patchedDependencies key should apply to all possible versions and error on non-applicable versions', async () => {
|
|
const patchFixture = f.find('patchedDependencies/console-log-replace-3rd-line.patch')
|
|
prepareEmpty()
|
|
|
|
await add.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
}, ['@pnpm.e2e/depends-on-console-log@1.0.0'])
|
|
fs.rmSync('pnpm-lock.yaml')
|
|
|
|
const patchedDependencies = addPatch('@pnpm.e2e/console-log', patchFixture, 'patches/console-log.patch')
|
|
|
|
const promise = install.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
frozenLockfile: false,
|
|
patchedDependencies,
|
|
})
|
|
|
|
// the common patch does not apply to v1
|
|
await expect(promise).rejects.toThrow(`Could not apply patch ${path.resolve('patches/console-log.patch')}`)
|
|
expect(patchedFileContent(1)).toBe(unpatchedFileContent(1))
|
|
|
|
// the common patch applies to v2
|
|
{
|
|
const text = patchedFileContent(2)
|
|
expect(text).not.toBe(unpatchedFileContent(2))
|
|
expect(text).toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text).toContain('THIRD LINE')
|
|
expect(text).not.toContain('third line')
|
|
expect(text.trim().split('\n')).toHaveLength(3)
|
|
}
|
|
|
|
// the common patch applies to v3
|
|
{
|
|
const text = patchedFileContent(3)
|
|
expect(text).not.toBe(unpatchedFileContent(3))
|
|
expect(text).toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text).toContain('THIRD LINE')
|
|
expect(text).not.toContain('third line')
|
|
expect(text).toContain('fourth line')
|
|
expect(text.trim().split('\n')).toHaveLength(4)
|
|
}
|
|
})
|
|
|
|
test('package name with version is prioritized over bare package name as keys of patchedDependencies', async () => {
|
|
const commonPatchFixture = f.find('patchedDependencies/console-log-replace-1st-line.patch')
|
|
const specializedPatchFixture = f.find('patchedDependencies/console-log-replace-2nd-line.patch')
|
|
prepareEmpty()
|
|
|
|
await add.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
}, ['@pnpm.e2e/depends-on-console-log@1.0.0'])
|
|
fs.rmSync('pnpm-lock.yaml')
|
|
|
|
addPatch('@pnpm.e2e/console-log', commonPatchFixture, 'patches/console-log.patch')
|
|
const patchedDependencies = addPatch('@pnpm.e2e/console-log@2.0.0', specializedPatchFixture, 'patches/console-log@2.0.0.patch')
|
|
|
|
await install.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
frozenLockfile: false,
|
|
patchedDependencies,
|
|
})
|
|
|
|
// the common patch applies to v1
|
|
{
|
|
const text = patchedFileContent(1)
|
|
expect(text).not.toBe(unpatchedFileContent(1))
|
|
expect(text).toContain('FIRST LINE')
|
|
expect(text).not.toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text.trim().split('\n')).toHaveLength(2)
|
|
}
|
|
|
|
// the specialized patch applies to v2
|
|
{
|
|
const text = patchedFileContent(2)
|
|
expect(text).not.toBe(unpatchedFileContent(2))
|
|
expect(text).toContain('first line')
|
|
expect(text).toContain('SECOND LINE')
|
|
expect(text).not.toContain('second line')
|
|
expect(text).toContain('third line')
|
|
expect(text.trim().split('\n')).toHaveLength(3)
|
|
}
|
|
|
|
// the common patch applies to v3
|
|
{
|
|
const text = patchedFileContent(3)
|
|
expect(text).not.toBe(unpatchedFileContent(3))
|
|
expect(text).toContain('FIRST LINE')
|
|
expect(text).not.toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text).toContain('third line')
|
|
expect(text).toContain('fourth line')
|
|
expect(text.trim().split('\n')).toHaveLength(4)
|
|
}
|
|
|
|
expect(globalWarn).not.toHaveBeenCalledWith(expect.stringContaining('Could not apply patch'))
|
|
})
|
|
|
|
test('package name with version as a patchedDependencies key does not affect other versions', async () => {
|
|
const patchFixture2 = f.find('patchedDependencies/console-log-replace-2nd-line.patch')
|
|
const patchFixture3 = f.find('patchedDependencies/console-log-replace-4th-line.patch')
|
|
prepareEmpty()
|
|
|
|
await add.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
}, ['@pnpm.e2e/depends-on-console-log@1.0.0'])
|
|
fs.rmSync('pnpm-lock.yaml')
|
|
|
|
addPatch('@pnpm.e2e/console-log@2.0.0', patchFixture2, 'patches/console-log@2.0.0.patch')
|
|
const patchedDependencies = addPatch('@pnpm.e2e/console-log@3.0.0', patchFixture3, 'patches/console-log@3.0.0.patch')
|
|
|
|
await install.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
frozenLockfile: false,
|
|
patchedDependencies,
|
|
})
|
|
|
|
// v1 remains unpatched
|
|
expect(patchedFileContent(1)).toBe(unpatchedFileContent(1))
|
|
|
|
// patch 2 applies to v2
|
|
{
|
|
const text = patchedFileContent(2)
|
|
expect(text).not.toBe(unpatchedFileContent(2))
|
|
expect(text).toContain('first line')
|
|
expect(text).toContain('SECOND LINE')
|
|
expect(text).not.toContain('second line')
|
|
expect(text).toContain('third line')
|
|
expect(text.trim().split('\n')).toHaveLength(3)
|
|
}
|
|
|
|
// patch 3 applies to v3
|
|
{
|
|
const text = patchedFileContent(3)
|
|
expect(text).not.toBe(unpatchedFileContent(3))
|
|
expect(text).toContain('first line')
|
|
expect(text).toContain('second line')
|
|
expect(text).toContain('third line')
|
|
expect(text).toContain('FOURTH LINE')
|
|
expect(text).not.toContain('fourth line')
|
|
expect(text.trim().split('\n')).toHaveLength(4)
|
|
}
|
|
})
|
|
|
|
test('failure to apply patch with package name and version would cause throw an error', async () => {
|
|
const patchFixture = f.find('patchedDependencies/console-log-replace-4th-line.patch')
|
|
prepareEmpty()
|
|
|
|
await add.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
}, ['@pnpm.e2e/depends-on-console-log@1.0.0'])
|
|
fs.rmSync('pnpm-lock.yaml')
|
|
|
|
const patchedDependencies = addPatch('@pnpm.e2e/console-log@1.0.0', patchFixture, 'patches/console-log@1.0.0.patch')
|
|
|
|
const promise = install.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
frozenLockfile: false,
|
|
patchedDependencies,
|
|
})
|
|
await expect(promise).rejects.toHaveProperty(['message'], expect.stringContaining('Could not apply patch'))
|
|
await expect(promise).rejects.toHaveProperty(['message'], expect.stringContaining(path.resolve('patches/console-log@1.0.0.patch')))
|
|
|
|
expect(patchedFileContent(1)).toBe(unpatchedFileContent(1))
|
|
})
|