mirror of
https://github.com/pnpm/pnpm.git
synced 2026-05-18 22:02:53 -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.
266 lines
8.2 KiB
TypeScript
266 lines
8.2 KiB
TypeScript
import { describe, expect, test } from '@jest/globals'
|
|
import type { CustomResolver } from '@pnpm/hooks.types'
|
|
import type { LockfileObject } from '@pnpm/lockfile.types'
|
|
|
|
import { checkCustomResolverForceResolve } from '../../src/install/checkCustomResolverForceResolve.js'
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
type AnyPackages = any
|
|
|
|
function lockfileWithPackages (packages?: Record<string, object>): LockfileObject {
|
|
return {
|
|
lockfileVersion: '9.0',
|
|
importers: {},
|
|
packages: packages as AnyPackages,
|
|
}
|
|
}
|
|
|
|
const TEST_PKG_SNAPSHOT = {
|
|
resolution: { tarball: 'http://example.com/test-pkg-1.0.0.tgz', integrity: 'sha512-test' },
|
|
}
|
|
|
|
describe('checkCustomResolverForceResolve', () => {
|
|
test('returns false when no custom resolvers provided', async () => {
|
|
const result = await checkCustomResolverForceResolve([], lockfileWithPackages())
|
|
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
test('returns false when lockfile has no packages', async () => {
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: () => true,
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve([resolver], lockfileWithPackages())
|
|
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
test('returns false when custom resolver has no shouldRefreshResolution', async () => {
|
|
const resolver: CustomResolver = {
|
|
canResolve: () => true,
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
test('returns false when shouldRefreshResolution returns false', async () => {
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: () => false,
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
test('returns true when shouldRefreshResolution returns true', async () => {
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: () => true,
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
test('shouldRefreshResolution is called independently of canResolve', async () => {
|
|
// canResolve returning false should NOT prevent shouldRefreshResolution from
|
|
// being called -- they operate on different paths.
|
|
const resolver: CustomResolver = {
|
|
canResolve: () => false,
|
|
shouldRefreshResolution: () => true,
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
test('returns true when any resolver among multiple returns true', async () => {
|
|
const resolver1: CustomResolver = {
|
|
shouldRefreshResolution: () => false,
|
|
}
|
|
const resolver2: CustomResolver = {
|
|
shouldRefreshResolution: () => true,
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve(
|
|
[resolver1, resolver2],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
test('handles async shouldRefreshResolution', async () => {
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: async () => {
|
|
await new Promise(resolve => setTimeout(resolve, 10))
|
|
return true
|
|
},
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
test('runs checks in parallel', async () => {
|
|
const callOrder: string[] = []
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: async (depPath) => {
|
|
const delays: Record<string, number> = { 'pkg1@1.0.0': 30, 'pkg2@1.0.0': 20 }
|
|
const delay = delays[depPath] ?? 10
|
|
await new Promise(resolve => setTimeout(resolve, delay))
|
|
callOrder.push(depPath)
|
|
return false
|
|
},
|
|
}
|
|
|
|
await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({
|
|
'pkg1@1.0.0': {
|
|
resolution: { tarball: 'http://example.com/pkg1-1.0.0.tgz', integrity: 'sha512-test1' },
|
|
},
|
|
'pkg2@1.0.0': {
|
|
resolution: { tarball: 'http://example.com/pkg2-1.0.0.tgz', integrity: 'sha512-test2' },
|
|
},
|
|
'pkg3@1.0.0': {
|
|
resolution: { tarball: 'http://example.com/pkg3-1.0.0.tgz', integrity: 'sha512-test3' },
|
|
},
|
|
})
|
|
)
|
|
|
|
// If parallel, pkg3 finishes first (10ms), then pkg2 (20ms), then pkg1 (30ms)
|
|
expect(callOrder).toEqual(['pkg3@1.0.0', 'pkg2@1.0.0', 'pkg1@1.0.0'])
|
|
})
|
|
|
|
test('passes depPath and pkgSnapshot to shouldRefreshResolution', async () => {
|
|
let receivedDepPath: string | undefined
|
|
let receivedPkgSnapshot: unknown
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: (depPath, pkgSnapshot) => {
|
|
receivedDepPath = depPath
|
|
receivedPkgSnapshot = pkgSnapshot
|
|
return false
|
|
},
|
|
}
|
|
|
|
await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
|
|
expect(receivedDepPath).toBe('test-pkg@1.0.0')
|
|
expect(receivedPkgSnapshot).toEqual(TEST_PKG_SNAPSHOT)
|
|
})
|
|
|
|
test('shouldRefreshResolution can filter by depPath to match specific packages', async () => {
|
|
// Resolver uses shouldRefreshResolution to do its own filtering -- this is
|
|
// the expected pattern now that canResolve is not used as a gate.
|
|
const resolver: CustomResolver = {
|
|
canResolve: (wantedDependency) => wantedDependency.alias === 'indirect-pkg',
|
|
shouldRefreshResolution: (depPath) => depPath.startsWith('indirect-pkg@'),
|
|
}
|
|
const lockfile: LockfileObject = {
|
|
lockfileVersion: '9.0',
|
|
importers: {
|
|
'.': {
|
|
specifiers: { 'direct-pkg': '1.0.0' },
|
|
dependencies: { 'direct-pkg': '1.0.0' },
|
|
},
|
|
} as AnyPackages,
|
|
packages: {
|
|
'direct-pkg@1.0.0': {
|
|
resolution: { tarball: 'http://example.com/direct-pkg-1.0.0.tgz', integrity: 'sha512-test1' },
|
|
dependencies: { 'indirect-pkg': '2.0.0' },
|
|
},
|
|
'indirect-pkg@2.0.0': {
|
|
resolution: { tarball: 'http://example.com/indirect-pkg-2.0.0.tgz', integrity: 'sha512-test2' },
|
|
},
|
|
} as AnyPackages,
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve([resolver], lockfile)
|
|
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
test('shouldRefreshResolution can inspect pkgSnapshot resolution type', async () => {
|
|
// A resolver that uses the resolution type to decide whether to force
|
|
// re-resolution -- this is the pattern for custom protocol resolvers.
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: (_depPath, pkgSnapshot) => {
|
|
const resolution = pkgSnapshot.resolution as Record<string, unknown>
|
|
return resolution.type === 'custom:cdn'
|
|
},
|
|
}
|
|
|
|
const result = await checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({
|
|
'foo@1.0.0': {
|
|
resolution: { type: 'custom:cdn', source: 'foo' },
|
|
},
|
|
'bar@2.0.0': {
|
|
resolution: { integrity: 'sha512-regular' },
|
|
},
|
|
})
|
|
)
|
|
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
test('re-throws errors from shouldRefreshResolution', async () => {
|
|
const resolver: CustomResolver = {
|
|
shouldRefreshResolution: () => {
|
|
throw new Error('resolver crashed')
|
|
},
|
|
}
|
|
|
|
await expect(
|
|
checkCustomResolverForceResolve(
|
|
[resolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
).rejects.toThrow('resolver crashed')
|
|
})
|
|
|
|
test('re-throws even when other resolvers return false', async () => {
|
|
const goodResolver: CustomResolver = {
|
|
shouldRefreshResolution: () => false,
|
|
}
|
|
const badResolver: CustomResolver = {
|
|
shouldRefreshResolution: () => {
|
|
throw new Error('unexpected failure')
|
|
},
|
|
}
|
|
|
|
await expect(
|
|
checkCustomResolverForceResolve(
|
|
[goodResolver, badResolver],
|
|
lockfileWithPackages({ 'test-pkg@1.0.0': TEST_PKG_SNAPSHOT })
|
|
)
|
|
).rejects.toThrow('unexpected failure')
|
|
})
|
|
})
|