mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-17 16:13:36 +08:00
Share workspace packages between Ray Admin and the interactive docs. BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
64 lines
1.7 KiB
TypeScript
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)
|
|
})
|