Files
pnpm/fetching/directory-fetcher/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

178 lines
6.1 KiB
TypeScript

/// <reference path="../../../__typings__/index.d.ts"/>
import fs from 'node:fs'
import path from 'node:path'
import { beforeAll, describe, expect, jest, test } from '@jest/globals'
import { fixtures } from '@pnpm/test-fixtures'
import { lexCompare } from '@pnpm/util.lex-comparator'
import { rimrafSync } from '@zkochan/rimraf'
const debug = jest.fn()
jest.unstable_mockModule('@pnpm/logger', () => {
return ({ globalWarn: jest.fn(), debug, logger: () => ({ debug }) })
})
const { createDirectoryFetcher } = await import('@pnpm/fetching.directory-fetcher')
const f = fixtures(import.meta.dirname)
test('fetch including only package files', async () => {
process.chdir(f.find('simple-pkg'))
const fetcher = createDirectoryFetcher({ includeOnlyPackageFiles: true })
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
})
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesMap.get('package.json')).toBe(path.resolve('package.json'))
// Only those files are included which would get published
expect(Array.from(fetchResult.filesMap.keys()).sort(lexCompare)).toStrictEqual([
'index.js',
'package.json',
])
})
test('fetch including all files', async () => {
process.chdir(f.find('simple-pkg'))
const fetcher = createDirectoryFetcher()
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
})
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesMap.get('package.json')).toBe(path.resolve('package.json'))
// Only those files are included which would get published
expect(Array.from(fetchResult.filesMap.keys()).sort(lexCompare)).toStrictEqual([
'index.js',
'package.json',
'test.js',
])
})
test('fetch a directory that has no package.json', async () => {
process.chdir(f.find('no-manifest'))
const fetcher = createDirectoryFetcher()
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
readManifest: true,
})
expect(fetchResult.manifest).toBeUndefined()
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesMap.get('index.js')).toBe(path.resolve('index.js'))
// Only those files are included which would get published
expect(Array.from(fetchResult.filesMap.keys()).sort(lexCompare)).toStrictEqual([
'index.js',
])
})
test('fetch does not fail on package with broken symlink', async () => {
jest.mocked(debug).mockClear()
process.chdir(f.find('pkg-with-broken-symlink'))
const fetcher = createDirectoryFetcher()
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
})
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesMap.get('package.json')).toBe(path.resolve('package.json'))
// Only those files are included which would get published
expect(Array.from(fetchResult.filesMap.keys()).sort(lexCompare)).toStrictEqual([
'index.js',
'package.json',
])
expect(debug).toHaveBeenCalledWith({ brokenSymlink: path.resolve('not-exists') })
})
test('fetch respects absolute directory regardless of lockfileDir', async () => {
const absDir = f.find('simple-pkg')
const fetcher = createDirectoryFetcher({ includeOnlyPackageFiles: true })
// lockfileDir is unrelated to the directory being fetched. When the
// stored directory is absolute (e.g. cross-drive `file:` deps on Windows)
// the fetcher must use the absolute path as-is rather than joining it
// onto lockfileDir.
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: absDir,
type: 'directory',
}, {
lockfileDir: f.find('no-manifest'),
})
expect(fetchResult.local).toBe(true)
expect(fetchResult.filesMap.get('package.json')).toBe(path.join(absDir, 'package.json'))
})
describe('fetch resolves symlinked files to their real locations', () => {
const indexJsPath = path.join(f.find('no-manifest'), 'index.js')
const srcPath = f.find('simple-pkg')
beforeAll(async () => {
process.chdir(f.find('pkg-with-symlinked-dir-and-files'))
rimrafSync('index.js')
fs.symlinkSync(indexJsPath, path.resolve('index.js'), 'file')
rimrafSync('src')
fs.symlinkSync(srcPath, path.resolve('src'), 'dir')
})
test('fetch resolves symlinked files to their real locations', async () => {
const fetcher = createDirectoryFetcher({ resolveSymlinks: true })
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
})
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesMap.get('package.json')).toBe(path.resolve('package.json'))
expect(fetchResult.filesMap.get('index.js')).toBe(indexJsPath)
expect(fetchResult.filesMap.get('src/index.js')).toBe(path.join(srcPath, 'index.js'))
})
test('fetch does not resolve symlinked files to their real locations by default', async () => {
const fetcher = createDirectoryFetcher()
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
})
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesMap.get('package.json')).toBe(path.resolve('package.json'))
expect(fetchResult.filesMap.get('index.js')).toBe(path.resolve('index.js'))
expect(fetchResult.filesMap.get('src/index.js')).toBe(path.resolve('src/index.js'))
})
})