Skip to content

Commit 226a229

Browse files
authored
feat(computedAsync): use globalThis.reportError as default onError (#4943)
1 parent 943a6a4 commit 226a229

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/core/computedAsync/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ describe('computedAsync', () => {
5555
expectTypeOf(data2).toEqualTypeOf<ComputedRef<string>>()
5656
})
5757

58+
it('default onError in computedAsync uses globalThis.reportError', async () => {
59+
const originalReportError = globalThis.reportError
60+
const mockReportError = vi.fn()
61+
globalThis.reportError = mockReportError
62+
63+
const error = new Error('An Error Message')
64+
const func = vi.fn(async () => {
65+
throw error
66+
})
67+
68+
const data = computedAsync(func, undefined)
69+
70+
expect(func).toBeCalledTimes(1)
71+
72+
expect(data.value).toBeUndefined()
73+
74+
await nextTick()
75+
expect(data.value).toBeUndefined()
76+
expect(mockReportError).toHaveBeenCalledWith(error)
77+
globalThis.reportError = originalReportError
78+
})
79+
5880
it('call onError when error is thrown', async () => {
5981
const errorMessage = shallowRef()
6082
const func = vi.fn(async () => {

packages/core/computedAsync/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function computedAsync<T>(
101101
flush = 'pre',
102102
evaluating = undefined,
103103
shallow = true,
104-
onError = noop,
104+
onError = globalThis.reportError ?? noop,
105105
} = options
106106

107107
const started = shallowRef(!lazy)

0 commit comments

Comments
 (0)