You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.2 KiB

import { describe, expect, it } from 'vitest';
import {
HTTP_ERR_ABORT,
HTTP_ERR_BODY_VALIDATION,
HTTP_ERR_NETWORK,
HTTP_ERR_TIMEOUT
} from '../errors.ts';
import {
HttpAbortError,
HttpBodyValidationError,
HttpNetworkError,
HttpTimeoutError,
isHttpAbortError,
isHttpBodyValidationError,
isHttpNetworkError,
isHttpTimeoutError
} from '../errors.ts';
describe('HTTP error hierarchy', () => {
it('each error carries its code as the error name', () => {
expect(new HttpNetworkError('x').name).toBe(HTTP_ERR_NETWORK);
expect(new HttpTimeoutError('x', 'attempt').name).toBe(HTTP_ERR_TIMEOUT);
expect(new HttpAbortError('x', null).name).toBe(HTTP_ERR_ABORT);
expect(new HttpBodyValidationError('x', []).name).toBe(HTTP_ERR_BODY_VALIDATION);
});
it('HttpTimeoutError exposes the scope', () => {
expect(new HttpTimeoutError('x', 'attempt').scope).toBe('attempt');
expect(new HttpTimeoutError('x', 'total').scope).toBe('total');
});
it('HttpAbortError preserves the reason', () => {
const reason = new Error('user');
expect(new HttpAbortError('x', reason).reason).toBe(reason);
});
it('HttpBodyValidationError exposes Standard Schema issues', () => {
const issues = [{ message: 'bad', path: ['email'] }];
expect(new HttpBodyValidationError('x', issues).issues).toBe(issues);
});
it('type guards distinguish each subclass', () => {
const network = new HttpNetworkError('x');
const timeout = new HttpTimeoutError('x', 'attempt');
const abort = new HttpAbortError('x', null);
const validation = new HttpBodyValidationError('x', []);
expect(isHttpNetworkError(network)).toBe(true);
expect(isHttpNetworkError(timeout)).toBe(false);
expect(isHttpTimeoutError(timeout)).toBe(true);
expect(isHttpTimeoutError(abort)).toBe(false);
expect(isHttpAbortError(abort)).toBe(true);
expect(isHttpAbortError(validation)).toBe(false);
expect(isHttpBodyValidationError(validation)).toBe(true);
expect(isHttpBodyValidationError(network)).toBe(false);
});
it('type guards reject non-error values', () => {
expect(isHttpNetworkError(null)).toBe(false);
expect(isHttpNetworkError({ name: 'HttpNetworkError' })).toBe(false);
expect(isHttpNetworkError('string')).toBe(false);
});
});

Powered by TurnKey Linux.