diff --git a/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c b/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c index c495f8780d0141..1b6bf8391bd87f 100644 --- a/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c +++ b/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c @@ -12,11 +12,30 @@ static void Finalize(napi_env env, void* data, void* hint) { napi_status expected_status = napi_pending_exception; #endif // NAPI_EXPERIMENTAL - if (napi_delete_reference(env, *ref) != napi_ok) abort(); - if (napi_get_global(env, &global) != napi_ok) abort(); - if (napi_get_named_property(env, global, "setTimeout", &set_timeout) != - expected_status) - abort(); + NODE_API_NOGC_ASSERT_RETURN_VOID( + napi_delete_reference(env, *ref) == napi_ok, + "deleting reference in finalizer should succeed"); + NODE_API_NOGC_ASSERT_RETURN_VOID( + napi_get_global(env, &global) == napi_ok, + "getting global reference in finalizer should succeed"); + napi_status result = + napi_get_named_property(env, global, "setTimeout", &set_timeout); + + // The finalizer could be invoked either from check callbacks (as native + // immediates) if the event loop is still running (where napi_ok is returned) + // or during environment shutdown (where napi_cannot_run_js or + // napi_pending_exception is returned). This is not deterministic from + // the point of view of the addon. + NODE_API_NOGC_ASSERT_RETURN_VOID( +#ifdef NAPI_EXPERIMENTAL + result == napi_cannot_run_js || result == napi_ok, + "getting named property from global in finalizer should succeed " + "or return napi_cannot_run_js"); +#else + result == napi_pending_exception || result == napi_ok, + "getting named property from global in finalizer should succeed " + "or return napi_pending_exception"); +#endif // NAPI_EXPERIMENTAL free(ref); }