ray-template/__test__/basic/callWithAsyncErrorHandling.spec.ts
XiaoDaiGua-Ray 403fdd589a feat!: rebuild Ray Template as a monorepo
Share workspace packages between Ray Admin and the interactive docs.

BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
2026-11-04 14:01:28 +08:00

64 lines
1.7 KiB
TypeScript

import { callWithAsyncErrorHandling } from '../../apps/admin/src/utils/basic'
describe('callWithAsyncErrorHandling', () => {
it('should call the function and return the result', async () => {
const fn = (x: number) => x
const callbackFn = () => {}
await expect(callWithAsyncErrorHandling(fn, callbackFn, [1])).resolves.toBe(
1,
)
})
it('should call the callback function when the function throws an error', () => {
let callbackFnExecuted = 1
const fn = () => {
throw new Error('test error')
}
const callbackFn = () => {
callbackFnExecuted = 2
}
callWithAsyncErrorHandling(fn, callbackFn)
expect(callbackFnExecuted).toBe(2)
})
})
it('handles Promise rejections from both async functions and regular factories', async () => {
const rejected = new Error('rejected')
const callback = vi.fn()
const asyncFn = async () => {
throw rejected
}
const factory = () => Promise.reject(rejected)
await expect(
callWithAsyncErrorHandling(asyncFn, callback),
).resolves.toBeUndefined()
await expect(
callWithAsyncErrorHandling(factory, callback),
).resolves.toBeUndefined()
expect(callback.mock.calls).toEqual([[rejected], [rejected]])
await expect(
callWithAsyncErrorHandling(async () => 42, callback),
).resolves.toBe(42)
})
it('does not invoke the error callback again when the callback throws', async () => {
const failure = new Error('callback failed')
const callback = vi.fn(() => {
throw failure
})
await expect(
callWithAsyncErrorHandling(() => {
throw new Error('original error')
}, callback),
).rejects.toBe(failure)
expect(callback).toHaveBeenCalledTimes(1)
})