|
| 1 | +import type { Hooks, Logger } from '../types'; |
| 2 | +import { useHooks } from '../useHooks'; |
| 3 | + |
| 4 | +describe('useHooks', () => { |
| 5 | + let warnSpy: jest.SpyInstance; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + warnSpy.mockRestore(); |
| 13 | + }); |
| 14 | + |
| 15 | + test('calls function normally without hooks', () => { |
| 16 | + const fn = jest.fn((a: number, b: number) => a + b); |
| 17 | + const wrapped = useHooks(fn, 'Test', {}); |
| 18 | + expect(wrapped(1, 2)).toBe(3); |
| 19 | + expect(fn).toHaveBeenCalledWith(1, 2); |
| 20 | + }); |
| 21 | + |
| 22 | + test('preHook wraps the function', () => { |
| 23 | + const fn = jest.fn((x: number) => x * 2); |
| 24 | + const preHook = jest.fn( |
| 25 | + ({ fn }: { fn: (x: number) => number }, x: number) => fn(x + 10), |
| 26 | + ); |
| 27 | + const wrapped = useHooks(fn, 'Test', { |
| 28 | + preTest: preHook, |
| 29 | + } as unknown as Hooks.Functions); |
| 30 | + expect(wrapped(5)).toBe(30); |
| 31 | + expect(preHook).toHaveBeenCalled(); |
| 32 | + }); |
| 33 | + |
| 34 | + test('postHook receives result', () => { |
| 35 | + const fn = jest.fn((x: number) => x * 2); |
| 36 | + const postHook = jest.fn( |
| 37 | + ( |
| 38 | + { result }: { fn: (x: number) => number; result?: number }, |
| 39 | + _x: number, |
| 40 | + ) => (result || 0) + 1, |
| 41 | + ); |
| 42 | + const wrapped = useHooks(fn, 'Test', { |
| 43 | + postTest: postHook, |
| 44 | + } as unknown as Hooks.Functions); |
| 45 | + expect(wrapped(5)).toBe(11); |
| 46 | + expect(postHook).toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + test('preHook error falls back to original fn and warns via console', () => { |
| 50 | + const fn = jest.fn((x: number) => x * 2); |
| 51 | + const preHook = jest.fn(() => { |
| 52 | + throw new Error('preHook exploded'); |
| 53 | + }); |
| 54 | + const wrapped = useHooks(fn, 'Test', { |
| 55 | + preTest: preHook, |
| 56 | + } as unknown as Hooks.Functions); |
| 57 | + |
| 58 | + expect(wrapped(5)).toBe(10); |
| 59 | + expect(fn).toHaveBeenCalledWith(5); |
| 60 | + expect(warnSpy).toHaveBeenCalledWith( |
| 61 | + expect.stringContaining('preTest'), |
| 62 | + expect.any(Error), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + test('postHook error returns original result and warns via console', () => { |
| 67 | + const fn = jest.fn((x: number) => x * 2); |
| 68 | + const postHook = jest.fn(() => { |
| 69 | + throw new Error('postHook exploded'); |
| 70 | + }); |
| 71 | + const wrapped = useHooks(fn, 'Test', { |
| 72 | + postTest: postHook, |
| 73 | + } as unknown as Hooks.Functions); |
| 74 | + |
| 75 | + expect(wrapped(5)).toBe(10); |
| 76 | + expect(fn).toHaveBeenCalledWith(5); |
| 77 | + expect(warnSpy).toHaveBeenCalledWith( |
| 78 | + expect.stringContaining('postTest'), |
| 79 | + expect.any(Error), |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + test('both hooks erroring keeps pipeline alive', () => { |
| 84 | + const fn = jest.fn((x: number) => x * 2); |
| 85 | + const preHook = jest.fn(() => { |
| 86 | + throw new Error('pre'); |
| 87 | + }); |
| 88 | + const postHook = jest.fn(() => { |
| 89 | + throw new Error('post'); |
| 90 | + }); |
| 91 | + const wrapped = useHooks(fn, 'Test', { |
| 92 | + preTest: preHook, |
| 93 | + postTest: postHook, |
| 94 | + } as unknown as Hooks.Functions); |
| 95 | + |
| 96 | + expect(wrapped(5)).toBe(10); |
| 97 | + expect(warnSpy).toHaveBeenCalledTimes(2); |
| 98 | + }); |
| 99 | + |
| 100 | + test('uses logger.warn instead of console when logger provided', () => { |
| 101 | + const fn = jest.fn((x: number) => x * 2); |
| 102 | + const preHook = jest.fn(() => { |
| 103 | + throw new Error('pre'); |
| 104 | + }); |
| 105 | + const mockLogger = { |
| 106 | + warn: jest.fn(), |
| 107 | + log: jest.fn(), |
| 108 | + debug: jest.fn(), |
| 109 | + error: jest.fn(), |
| 110 | + throw: jest.fn(), |
| 111 | + scope: jest.fn(), |
| 112 | + child: jest.fn(), |
| 113 | + } as unknown as Logger.Instance; |
| 114 | + |
| 115 | + const wrapped = useHooks( |
| 116 | + fn, |
| 117 | + 'Test', |
| 118 | + { preTest: preHook } as unknown as Hooks.Functions, |
| 119 | + mockLogger, |
| 120 | + ); |
| 121 | + |
| 122 | + expect(wrapped(5)).toBe(10); |
| 123 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 124 | + expect.stringContaining('preTest'), |
| 125 | + expect.objectContaining({ error: expect.any(Error) }), |
| 126 | + ); |
| 127 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments