Files
pnpm/workspace/commands/test/init.test.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

145 lines
4.9 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import { expect, test } from '@jest/globals'
import { prepare, prepareEmpty } from '@pnpm/prepare'
import type { ProjectManifest } from '@pnpm/types'
import { init } from '@pnpm/workspace.commands'
import { loadJsonFileSync } from 'load-json-file'
test('init a new package.json', async () => {
prepareEmpty()
await init.handler({ cliOptions: {} })
const manifest = loadJsonFileSync(path.resolve('package.json'))
expect(manifest).toBeTruthy()
})
test('throws an error if a package.json exists in the current directory', async () => {
prepare({})
await expect(
init.handler({ cliOptions: {} })
).rejects.toThrow('package.json already exists')
})
test('init a new package.json with author and license settings', async () => {
prepareEmpty()
await init.handler({
cliOptions: {},
initAuthorEmail: 'xxxxxx@pnpm.com',
initAuthorName: 'pnpm',
initAuthorUrl: 'https://www.github.com/pnpm',
initLicense: 'MIT',
initVersion: '2.0.0',
})
const manifest: Record<string, string> = loadJsonFileSync(path.resolve('package.json'))
expect(manifest.version).toBe('2.0.0')
expect(manifest.author).toBe('pnpm <xxxxxx@pnpm.com> (https://www.github.com/pnpm)')
expect(manifest.license).toBe('MIT')
})
test('throw an error if params are passed to the init command', async () => {
prepare({})
await expect(
init.handler({ cliOptions: {} }, ['react-app'])
).rejects.toThrow('init command does not accept any arguments')
})
test('init a new package.json if a package.json exists in the parent directory', async () => {
prepare({})
fs.mkdirSync('empty-dir1')
process.chdir('./empty-dir1')
await init.handler({ cliOptions: {} })
const manifest = loadJsonFileSync(path.resolve('package.json'))
expect(manifest).toBeTruthy()
})
test('init a new package.json if a package.json exists in the current directory but specifies --dir option', async () => {
prepare({})
fs.mkdirSync('empty-dir2')
await init.handler({
cliOptions: {
dir: './empty-dir2',
},
})
const manifest = loadJsonFileSync(path.resolve('empty-dir2/package.json'))
expect(manifest).toBeTruthy()
})
test('init a new package.json with init-package-manager=true', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, initPackageManager: true })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest).toBeTruthy()
expect(manifest).not.toHaveProperty('packageManager')
expect(manifest.devEngines?.packageManager).toEqual({
name: 'pnpm',
version: expect.stringMatching(/^\^\d+\.\d+\.\d+/),
onFail: 'download',
})
})
test('init a new package.json with init-package-manager=false', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, initPackageManager: false })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest).toBeTruthy()
expect(manifest).not.toHaveProperty('packageManager')
expect(manifest).not.toHaveProperty('devEngines')
})
test('init a new package.json in a workspace subpackage does not add devEngines', async () => {
prepareEmpty()
const workspaceDir = process.cwd()
const subpackageDir = path.join(workspaceDir, 'packages/foo')
fs.mkdirSync(subpackageDir, { recursive: true })
await init.handler({
cliOptions: { dir: subpackageDir },
initPackageManager: true,
workspaceDir,
})
const manifest = loadJsonFileSync<ProjectManifest>(path.join(subpackageDir, 'package.json'))
expect(manifest).toBeTruthy()
expect(manifest).not.toHaveProperty('devEngines')
expect(manifest).not.toHaveProperty('packageManager')
})
test('init a new package.json at the workspace root adds devEngines', async () => {
prepareEmpty()
const workspaceDir = process.cwd()
await init.handler({
cliOptions: {},
initPackageManager: true,
workspaceDir,
})
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest.devEngines?.packageManager).toEqual({
name: 'pnpm',
version: expect.stringMatching(/^\^\d+\.\d+\.\d+/),
onFail: 'download',
})
})
test('init a new package.json with init-type=module', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, initType: 'module' })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest.type).toBe('module')
})
test('init a new package.json with --bare', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, bare: true })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest).not.toHaveProperty(['name'])
expect(manifest).not.toHaveProperty(['version'])
expect(manifest).not.toHaveProperty(['description'])
expect(manifest).not.toHaveProperty(['main'])
expect(manifest).not.toHaveProperty(['keywords'])
expect(manifest).not.toHaveProperty(['author'])
expect(manifest).not.toHaveProperty(['license'])
})