Add comprehensive test for async to_napi_value error handling

Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-11-23 05:40:06 +00:00
parent bf0ae3f8f9
commit d57c03fab8
3 changed files with 34 additions and 1319 deletions

File diff suppressed because it is too large Load Diff

View File

@ -209,6 +209,7 @@ import {
chronoNativeDateTime,
chronoNativeDateTimeReturn,
throwAsyncError,
asyncFailInToNapiValue,
getModuleFileName,
throwSyntaxError,
type AliasedStruct,
@ -1009,6 +1010,39 @@ test('Async error with stack trace', async (t) => {
}
})
test('Async error from to_napi_value properly rejects promise', async (t) => {
// Test that errors from ToNapiValue::to_napi_value in async functions
// properly reject the promise instead of throwing synchronously
// Test with .catch()
const err1 = await t.throwsAsync(() => asyncFailInToNapiValue())
t.deepEqual(err1!.message, 'Fail in to_napi_value')
t.deepEqual(err1!.code, 'GenericFailure')
// Test with .then(success, error) - using proper Promise API
const promise2 = asyncFailInToNapiValue()
await new Promise<void>((resolve) => {
promise2.then(
() => {
t.fail('Promise should have been rejected')
resolve()
},
(error) => {
t.deepEqual(error.message, 'Fail in to_napi_value')
resolve()
},
)
})
// Test with async/await + try-catch
try {
await asyncFailInToNapiValue()
t.fail('Should have thrown an error')
} catch (error: any) {
t.deepEqual(error.message, 'Fail in to_napi_value')
}
})
test('custom status code in Error', (t) => {
t.throws(() => customStatusCode(), {
code: 'Panic',