Files
pnpm/installing/commands/test/fetch.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

270 lines
7.5 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import { expect, test } from '@jest/globals'
import { STORE_VERSION } from '@pnpm/constants'
import { fetch, install } from '@pnpm/installing.commands'
import { prepare } from '@pnpm/prepare'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import { closeAllStoreIndexes } from '@pnpm/store.index'
import { fixtures } from '@pnpm/test-fixtures'
import { finishWorkers } from '@pnpm/worker'
import { rimrafSync } from '@zkochan/rimraf'
const REGISTRY_URL = `http://localhost:${REGISTRY_MOCK_PORT}`
const DEFAULT_OPTIONS = {
argv: {
original: [],
},
bail: false,
bin: 'node_modules/.bin',
cliOptions: {},
deployAllFiles: false,
excludeLinksFromLockfile: false,
extraEnv: {},
include: {
dependencies: true,
devDependencies: true,
optionalDependencies: true,
},
lock: true,
preferWorkspacePackages: true,
pnpmfile: ['.pnpmfile.cjs'],
pnpmHomeDir: '',
configByUri: {},
registries: {
default: REGISTRY_URL,
},
rootProjectManifestDir: '',
sort: true,
userConfig: {},
workspaceConcurrency: 1,
virtualStoreDirMaxLength: process.platform === 'win32' ? 60 : 120,
}
test('fetch dependencies', async () => {
const project = prepare({
dependencies: { 'is-positive': '1.0.0' },
devDependencies: { 'is-negative': '1.0.0' },
})
const storeDir = path.resolve('store')
await install.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
linkWorkspacePackages: true,
storeDir,
})
rimrafSync(path.resolve(project.dir(), 'node_modules'))
rimrafSync(path.resolve(project.dir(), './package.json'))
project.storeHasNot('is-negative')
project.storeHasNot('is-positive')
await fetch.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
storeDir,
})
project.storeHas('is-positive')
project.storeHas('is-negative')
})
test('fetch production dependencies', async () => {
const project = prepare({
dependencies: { 'is-positive': '1.0.0' },
devDependencies: { 'is-negative': '1.0.0' },
})
const storeDir = path.resolve('store')
await install.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
linkWorkspacePackages: true,
storeDir,
})
rimrafSync(path.resolve(project.dir(), 'node_modules'))
rimrafSync(path.resolve(project.dir(), './package.json'))
project.storeHasNot('is-negative')
project.storeHasNot('is-positive')
await fetch.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dev: true,
dir: process.cwd(),
storeDir,
})
project.storeHasNot('is-negative')
project.storeHas('is-positive')
})
test('fetch only dev dependencies', async () => {
const project = prepare({
dependencies: { 'is-positive': '1.0.0' },
devDependencies: { 'is-negative': '1.0.0' },
})
const storeDir = path.resolve('store')
await install.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
linkWorkspacePackages: true,
storeDir,
})
rimrafSync(path.resolve(project.dir(), 'node_modules'))
rimrafSync(path.resolve(project.dir(), './package.json'))
project.storeHasNot('is-negative')
project.storeHasNot('is-positive')
await fetch.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dev: true,
dir: process.cwd(),
storeDir,
})
project.storeHas('is-negative')
project.storeHasNot('is-positive')
})
// Regression test for https://github.com/pnpm/pnpm/issues/10460
// pnpm fetch should skip local file: protocol dependencies
// because they won't be available in Docker builds
test('fetch skips file: protocol dependencies that do not exist', async () => {
const project = prepare({
dependencies: {
'is-positive': '1.0.0',
'@local/pkg': 'file:./local-pkg',
},
})
const storeDir = path.resolve('store')
const localPkgDir = path.resolve(project.dir(), 'local-pkg')
// Create the local package for initial install to generate lockfile
fs.mkdirSync(localPkgDir, { recursive: true })
fs.writeFileSync(
path.join(localPkgDir, 'package.json'),
JSON.stringify({ name: '@local/pkg', version: '1.0.0' })
)
// Create a lockfile with the file: dependency
await install.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
linkWorkspacePackages: true,
storeDir,
})
rimrafSync(path.resolve(project.dir(), 'node_modules'))
rimrafSync(path.resolve(project.dir(), './package.json'))
// Remove the local package directory to simulate Docker build scenario
rimrafSync(localPkgDir)
project.storeHasNot('is-positive')
// This should not throw an error even though the file: dependency doesn't exist
await fetch.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
storeDir,
})
project.storeHas('is-positive')
})
test('fetch populates global virtual store links/', async () => {
prepare({
dependencies: {
'is-positive': '1.0.0',
},
devDependencies: {
'is-negative': '1.0.0',
},
})
const storeDir = path.resolve('store')
const globalVirtualStoreDir = path.join(storeDir, STORE_VERSION, 'links')
// Generate the lockfile only — no need for a full install
await install.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
linkWorkspacePackages: true,
lockfileOnly: true,
storeDir,
})
// Drain workers and close SQLite connections before removing the store (required on Windows)
await finishWorkers()
closeAllStoreIndexes()
// Remove the store — simulate a cold start with only the lockfile
rimrafSync(storeDir)
// Fetch with enableGlobalVirtualStore — should populate links/
await fetch.handler({
...DEFAULT_OPTIONS,
cacheDir: path.resolve('cache'),
dir: process.cwd(),
storeDir,
enableGlobalVirtualStore: true,
})
// The global virtual store links/ directory should exist and contain packages
expect(fs.existsSync(globalVirtualStoreDir)).toBeTruthy()
const entries = fs.readdirSync(globalVirtualStoreDir)
expect(entries.length).toBeGreaterThan(0)
})
test('fetch applies patches to dependencies when patchedDependencies key is bare package name', async () => {
const f = fixtures(import.meta.dirname)
const project = prepare({
dependencies: { '@pnpm.e2e/console-log': '1.0.0' },
})
fs.mkdirSync('patches', { recursive: true })
fs.copyFileSync(f.find('patchedDependencies/console-log-replace-1st-line.patch'), 'patches/console-log.patch')
const patchedDependencies = { '@pnpm.e2e/console-log': 'patches/console-log.patch' }
const cacheDir = path.resolve(project.dir(), 'cache')
const storeDir = path.resolve(project.dir(), 'store')
await install.handler({
...DEFAULT_OPTIONS,
cacheDir,
dir: project.dir(),
linkWorkspacePackages: false,
lockfileOnly: true,
storeDir,
patchedDependencies,
})
await fetch.handler({
...DEFAULT_OPTIONS,
cacheDir,
dir: project.dir(),
storeDir,
patchedDependencies,
})
const virtualStoreDir = path.resolve(project.dir(), 'node_modules', '.pnpm')
const consoleLogDirs = fs.readdirSync(virtualStoreDir).filter(d => d.startsWith('@pnpm.e2e+console-log@'))
expect(consoleLogDirs.length).toBeGreaterThan(0)
const patchedIndexJsAfterFetch = fs.readFileSync(path.join(virtualStoreDir, consoleLogDirs[0], 'node_modules/@pnpm.e2e/console-log/index.js'), 'utf8')
expect(patchedIndexJsAfterFetch).toContain('FIRST LINE')
})