Files
pnpm/resolving/npm-resolver/test/clearCache.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

82 lines
2.5 KiB
TypeScript

import { afterEach, beforeEach, expect, test } from '@jest/globals'
import { createFetchFromRegistry } from '@pnpm/network.fetch'
import { createNpmResolver } from '@pnpm/resolving.npm-resolver'
import type { PackageMeta } from '@pnpm/resolving.registry.types'
import type { Registries } from '@pnpm/types'
import { temporaryDirectory } from 'tempy'
import { getMockAgent, setupMockAgent, teardownMockAgent } from './utils/index.js'
const registries: Registries = {
default: 'https://registry.npmjs.org/',
}
const fetch = createFetchFromRegistry({})
const getAuthHeader = () => undefined
const createResolveFromNpm = createNpmResolver.bind(null, fetch, getAuthHeader)
beforeEach(async () => {
await setupMockAgent()
})
afterEach(async () => {
await teardownMockAgent()
})
test('metadata is fetched again after calling clearCache()', async () => {
const name = 'test-package'
const meta: PackageMeta = {
name,
versions: {
'3.0.0': {
name,
version: '3.0.0',
// Generated locally through: echo '1.1.0-beta' | sha1sum
dist: { shasum: '8c6981d7f982c3e2986fda2f34282264a4db344c', tarball: `https://registry.npmjs.org/${name}/-/${name}-3.0.0.tgz` },
},
},
'dist-tags': {
latest: '3.0.0',
},
time: {
'3.0.0': '2020-02-01T00:00:00.000Z',
},
}
const mockPool = getMockAgent().get('https://registry.npmjs.org')
mockPool.intercept({ path: `/${name}`, method: 'GET' })
.reply(200, meta)
const cacheDir = temporaryDirectory()
const { resolveFromNpm, clearCache } = createResolveFromNpm({
cacheDir,
fullMetadata: true,
registries,
storeDir: temporaryDirectory(),
})
const res = await resolveFromNpm({ alias: name, bareSpecifier: 'latest' }, {})
expect(res?.id).toBe(`${name}@3.0.0`)
// Simulate publishing a new 3.1.0 version.
meta.versions['3.1.0'] = {
name,
version: '3.1.0',
dist: { shasum: '5f022945150b402cb3e470acc3818847b3dc5e00', tarball: `https://registry.npmjs.org/${name}/-/${name}-3.1.0.tgz` },
}
meta['dist-tags'].latest = '3.1.0'
mockPool.intercept({ path: `/${name}`, method: 'GET' })
.reply(200, meta)
// Until the cache is cleared, the resolver will still return 3.0.0.
const res2 = await resolveFromNpm({ alias: name, bareSpecifier: 'latest' }, {})
expect(res2?.id).toBe(`${name}@3.0.0`)
clearCache()
// After clearing cache, the resolver should start returning 3.1.0.
const res3 = await resolveFromNpm({ alias: name, bareSpecifier: 'latest' }, {})
expect(res3?.id).toBe(`${name}@3.1.0`)
})