Files
pnpm/core/error/test/index.ts
Brandon Cheng 831f574330 fix: propagate error cause when throwing PnpmError in @pnpm/npm-resolver (#10990)
* fix: show error cause when failing to read metadata

* fix: correct changeset package name and add cause assertion tests

- Fix changeset to reference @pnpm/resolving.npm-resolver (not @pnpm/npm-resolver)
- Add PnpmError cause unit tests in @pnpm/error
- Fix npm-resolver tests to actually verify cause on thrown errors
  (.toThrow() only checks message, not cause/hint/code properties)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 00:59:52 +01:00

50 lines
2.0 KiB
TypeScript

import { FetchError, PnpmError } from '@pnpm/error'
test('PnpmError exposes cause when provided', () => {
const cause = new Error('original failure')
const error = new PnpmError('TEST_CODE', 'something went wrong', { cause })
expect(error.cause).toBe(cause)
expect(error.message).toBe('something went wrong')
expect(error.code).toBe('ERR_PNPM_TEST_CODE')
})
test('PnpmError cause is undefined when omitted', () => {
const error = new PnpmError('TEST_CODE', 'something went wrong')
expect(error.cause).toBeUndefined()
})
test('PnpmError cause works with non-Error values', () => {
const error = new PnpmError('TEST_CODE', 'something went wrong', { cause: 'string cause' })
expect(error.cause).toBe('string cause')
})
test('FetchError escapes auth tokens', () => {
const error = new FetchError(
{ url: 'https://foo.com', authHeaderValue: 'Bearer 00000000000000000000' },
{ status: 401, statusText: 'Unauthorized' }
)
expect(error.message).toBe('GET https://foo.com: Unauthorized - 401')
expect(error.hint).toBe('An authorization header was used: Bearer 0000[hidden]')
expect(error.request.authHeaderValue).toBe('Bearer 0000[hidden]')
})
test('FetchError escapes short auth tokens', () => {
const error = new FetchError(
{ url: 'https://foo.com', authHeaderValue: 'Bearer 0000000000' },
{ status: 401, statusText: 'Unauthorized' }
)
expect(error.message).toBe('GET https://foo.com: Unauthorized - 401')
expect(error.hint).toBe('An authorization header was used: Bearer [hidden]')
expect(error.request.authHeaderValue).toBe('Bearer [hidden]')
})
test('FetchError escapes non-standard auth header', () => {
const error = new FetchError(
{ url: 'https://foo.com', authHeaderValue: '0000000000' },
{ status: 401, statusText: 'Unauthorized' }
)
expect(error.message).toBe('GET https://foo.com: Unauthorized - 401')
expect(error.hint).toBe('An authorization header was used: [hidden]')
expect(error.request.authHeaderValue).toBe('[hidden]')
})