Files
brainwave/backend/src/utils/asyncHandler.test.ts
T
LaurensBartelds ef8fe6f938
Test / backend (push) Failing after 9s
Test / frontend (push) Failing after 14s
feat: enhance testing framework with new test cases and configurations
2026-04-26 21:45:02 +02:00

38 lines
1.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { asyncHandler } from './asyncHandler'
import type { Request, Response, NextFunction } from 'express'
const mockReq = {} as Request
const mockRes = {} as Response
let mockNext: NextFunction
beforeEach(() => {
mockNext = vi.fn() as unknown as NextFunction
})
describe('asyncHandler', () => {
it('calls next with error when async handler throws', async () => {
const error = new Error('boom')
const handler = asyncHandler(async () => {
throw error
})
await handler(mockReq, mockRes, mockNext)
expect(mockNext).toHaveBeenCalledWith(error)
})
it('does not call next when handler resolves', async () => {
const handler = asyncHandler(async () => {
// success
})
await handler(mockReq, mockRes, mockNext)
expect(mockNext).not.toHaveBeenCalled()
})
it('calls next with error when handler rejects', async () => {
const error = new Error('rejected')
const handler = asyncHandler(() => Promise.reject(error))
await handler(mockReq, mockRes, mockNext)
expect(mockNext).toHaveBeenCalledWith(error)
})
})