|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { shouldLogException, type ErrorWithCode } from '../lib/should-log-exception' |
| 4 | + |
| 5 | +// Helper function to create test errors with code property |
| 6 | +function createError(message: string, name: string = 'Error', code: string = ''): ErrorWithCode { |
| 7 | + const error = new Error(message) as ErrorWithCode |
| 8 | + error.name = name |
| 9 | + error.code = code |
| 10 | + return error |
| 11 | +} |
| 12 | + |
| 13 | +describe('shouldLogException', () => { |
| 14 | + describe('ECONNRESET errors', () => { |
| 15 | + test('should not log ECONNRESET errors', () => { |
| 16 | + const error = createError('Connection reset', 'Error', 'ECONNRESET') |
| 17 | + |
| 18 | + expect(shouldLogException(error)).toBe(false) |
| 19 | + }) |
| 20 | + }) |
| 21 | + |
| 22 | + describe('TypeError: terminated filtering', () => { |
| 23 | + test('should not log TypeError with exact message "terminated"', () => { |
| 24 | + const error = createError('terminated', 'TypeError') |
| 25 | + |
| 26 | + expect(shouldLogException(error)).toBe(false) |
| 27 | + }) |
| 28 | + |
| 29 | + test('should log TypeError with different message', () => { |
| 30 | + const error = createError('Cannot read property', 'TypeError') |
| 31 | + |
| 32 | + expect(shouldLogException(error)).toBe(true) |
| 33 | + }) |
| 34 | + |
| 35 | + test('should log TypeError with partial "terminated" message', () => { |
| 36 | + const error = createError('connection terminated unexpectedly', 'TypeError') |
| 37 | + |
| 38 | + expect(shouldLogException(error)).toBe(true) |
| 39 | + }) |
| 40 | + |
| 41 | + test('should log non-TypeError with "terminated" message', () => { |
| 42 | + const error = createError('terminated', 'Error') |
| 43 | + |
| 44 | + expect(shouldLogException(error)).toBe(true) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + describe('regular errors', () => { |
| 49 | + test('should log regular errors', () => { |
| 50 | + const error = createError('Something went wrong', 'Error') |
| 51 | + |
| 52 | + expect(shouldLogException(error)).toBe(true) |
| 53 | + }) |
| 54 | + |
| 55 | + test('should log errors with no code', () => { |
| 56 | + const error = createError('Test error', 'Error') |
| 57 | + |
| 58 | + expect(shouldLogException(error)).toBe(true) |
| 59 | + }) |
| 60 | + }) |
| 61 | +}) |
0 commit comments