mirror of
https://github.com/napi-rs/napi-rs.git
synced 2025-12-08 19:56:07 +00:00
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:
parent
bf0ae3f8f9
commit
d57c03fab8
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@ -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',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user