Skip to content

Commit f6e8898

Browse files
authored
feat(useAsyncState)!: set globalThis.reportError as default onError (#4951)
1 parent e7e6bee commit f6e8898

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/core/useAsyncState/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { promiseTimeout } from '@vueuse/shared'
22
import { describe, expect, it, vi } from 'vitest'
3+
import { nextTick } from 'vue'
34
import { useAsyncState } from './index'
45

56
describe('useAsyncState', () => {
@@ -90,4 +91,23 @@ describe('useAsyncState', () => {
9091
const { execute } = useAsyncState(p2, '0', { throwError: true, immediate: false })
9192
await expect(execute()).rejects.toThrowError('error')
9293
})
94+
95+
it('default onError uses globalThis.reportError', async () => {
96+
const originalReportError = globalThis.reportError
97+
const mockReportError = vi.fn()
98+
globalThis.reportError = mockReportError
99+
100+
const error = new Error('error message')
101+
const func = vi.fn(async () => {
102+
throw error
103+
})
104+
105+
const { execute } = useAsyncState(func, '', { immediate: false })
106+
await execute()
107+
expect(func).toBeCalledTimes(1)
108+
109+
await nextTick()
110+
expect(mockReportError).toHaveBeenCalledWith(error)
111+
globalThis.reportError = originalReportError
112+
})
93113
})

packages/core/useAsyncState/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function useAsyncState<Data, Params extends any[] = any[], Shallow extend
8787
const {
8888
immediate = true,
8989
delay = 0,
90-
onError = noop,
90+
onError = globalThis.reportError ?? noop,
9191
onSuccess = noop,
9292
resetOnExecute = true,
9393
shallow = true,

0 commit comments

Comments
 (0)