UNPKG

@unito/integration-sdk

Version:

Integration SDK

65 lines (64 loc) 2.22 kB
import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import onError from '../../src/middlewares/errors.js'; import { HttpError } from '../../src/httpErrors.js'; describe('errors middleware', () => { it('headers sent, do nothing', () => { let actualStatus; let actualJson; const response = { headersSent: true, status: (code) => { actualStatus = code; return { json: (json) => { actualJson = json; }, }; }, locals: { logger: { error: () => undefined } }, }; onError(new Error(), {}, response, () => { }); assert.strictEqual(actualStatus, undefined); assert.strictEqual(actualJson, undefined); }); it('handles httpError', () => { let actualStatus; let actualJson; const response = { headersSent: false, status: (code) => { actualStatus = code; return { json: (json) => { actualJson = json; }, }; }, locals: { logger: { error: () => undefined } }, }; onError(new HttpError('httpError', 429), {}, response, () => { }); assert.strictEqual(actualStatus, 429); assert.deepEqual(actualJson, { code: '429', message: 'httpError' }); }); it('handles other error', () => { let actualStatus; let actualJson; const response = { headersSent: false, status: (code) => { actualStatus = code; return { json: (json) => { actualJson = json; }, }; }, locals: { logger: { error: () => undefined } }, }; onError(new Error('error'), {}, response, () => { }); assert.strictEqual(actualStatus, 500); assert.strictEqual(actualJson?.code, '500'); assert.deepEqual(actualJson?.originalError, { code: 'Error', message: 'error' }); }); });