tree: 2e1c8161e9d2c1ad5795d288fa5b9e01369f67f9 [path history] [tgz]
  1. addon_build/
  2. basic_types/
  3. child_processes/
  4. common/
  5. dataview/
  6. globalObject/
  7. maybe/
  8. object/
  9. require_basic_finalizers/
  10. threadsafe_function/
  11. typed_threadsafe_function/
  12. .gitignore
  13. addon.cc
  14. addon.js
  15. addon_data.cc
  16. addon_data.js
  17. array_buffer.cc
  18. array_buffer.js
  19. async_context.cc
  20. async_context.js
  21. async_progress_queue_worker.cc
  22. async_progress_queue_worker.js
  23. async_progress_worker.cc
  24. async_progress_worker.js
  25. async_worker.cc
  26. async_worker.js
  27. async_worker_nocallback.js
  28. async_worker_persistent.cc
  29. async_worker_persistent.js
  30. bigint.cc
  31. bigint.js
  32. binding-swallowexcept.cc
  33. binding.cc
  34. binding.gyp
  35. buffer.cc
  36. buffer.h
  37. buffer.js
  38. buffer_new_or_copy-inl.h
  39. buffer_no_external.cc
  40. callbackInfo.cc
  41. callbackInfo.js
  42. callbackscope.cc
  43. callbackscope.js
  44. date.cc
  45. date.js
  46. env_cleanup.cc
  47. env_cleanup.js
  48. env_misc.cc
  49. env_misc.js
  50. error.cc
  51. error.js
  52. error_handling_for_primitives.cc
  53. error_handling_for_primitives.js
  54. error_terminating_environment.js
  55. except_all.cc
  56. except_all.js
  57. exports.js
  58. external.cc
  59. external.js
  60. finalizer_order.cc
  61. finalizer_order.js
  62. function.cc
  63. function.js
  64. function_reference.cc
  65. function_reference.js
  66. handlescope.cc
  67. handlescope.js
  68. index.js
  69. memory_management.cc
  70. memory_management.js
  71. movable_callbacks.cc
  72. movable_callbacks.js
  73. name.cc
  74. name.js
  75. napi_child.js
  76. object_reference.cc
  77. object_reference.js
  78. objectwrap.cc
  79. objectwrap.js
  80. objectwrap_constructor_exception.cc
  81. objectwrap_constructor_exception.js
  82. objectwrap_function.cc
  83. objectwrap_function.js
  84. objectwrap_multiple_inheritance.cc
  85. objectwrap_multiple_inheritance.js
  86. objectwrap_removewrap.cc
  87. objectwrap_removewrap.js
  88. objectwrap_worker_thread.js
  89. promise.cc
  90. promise.js
  91. README.md
  92. reference.cc
  93. reference.js
  94. run_script.cc
  95. run_script.js
  96. shared_array_buffer.cc
  97. shared_array_buffer.js
  98. symbol.cc
  99. symbol.js
  100. testUtil.js
  101. thunking_manual.cc
  102. thunking_manual.js
  103. type_taggable.cc
  104. type_taggable.js
  105. typedarray-bigint.js
  106. typedarray.cc
  107. typedarray.js
  108. value_type_cast.cc
  109. value_type_cast.js
  110. version_management.cc
  111. version_management.js
test/README.md

Writing Tests

There are multiple flavors of node-addon-api test builds that cover different build flags defined in napi.h:

  1. c++ exceptions enabled,
  2. c++ exceptions disabled,
  3. c++ exceptions disabled, and NODE_ADDON_API_ENABLE_MAYBE defined.

Functions in node-addon-api that call into JavaScript can have different declared return types to reflect build flavor settings. For example, Napi::Object::Set returns bool when NODE_ADDON_API_ENABLE_MAYBE is not defined, and Napi::Maybe<bool> when NODE_ADDON_API_ENABLE_MAYBE is defined. In source code, return type variants are defined as Napi::MaybeOrValue<> to prevent the duplication of most of the code base.

To properly test these build flavors, all values returned by a function defined to return Napi::MaybeOrValue<> should be tested by using one of the following test helpers to handle possible JavaScript exceptions.

There are three test helper functions to conveniently convert Napi::MaybeOrValue<> values to raw values.

MaybeUnwrap

template <typename T>
T MaybeUnwrap(MaybeOrValue<T> maybe);

Converts MaybeOrValue<T> to T by checking that MaybeOrValue is NOT an empty Maybe.

Returns the original value if NODE_ADDON_API_ENABLE_MAYBE is not defined.

Example:

Object obj = info[0].As<Object>();
// we are sure the parameters should not throw
Value value = MaybeUnwrap(obj->Get("foobar"));

MaybeUnwrapOr

template <typename T>
T MaybeUnwrapOr(MaybeOrValue<T> maybe, const T& default_value = T());

Converts MaybeOrValue<T> to T by getting the value that wrapped by the Maybe or return the default_value if the Maybe is empty.

Returns the original value if NODE_ADDON_API_ENABLE_MAYBE is not defined.

Example:

Value CallWithArgs(const CallbackInfo& info) {
  Function func = info[0].As<Function>();
  // We don't care if the operation is throwing or not, just return it back to node-addon-api
  return MaybeUnwrapOr(
      func.Call(std::initializer_list<napi_value>{info[1], info[2], info[3]}));
}

MaybeUnwrapTo

template <typename T>
bool MaybeUnwrapTo(MaybeOrValue<T> maybe, T* out);

Converts MaybeOrValue<T> to T by getting the value that wrapped by the eMaybe or return false if the Maybe is empty

Copies the value to out when NODE_ADDON_API_ENABLE_MAYBE is not defined

Example:

Object opts = info[0].As<Object>();
bool hasProperty = false;
// The check may throw, but we are going to suppress that.
if (MaybeUnwrapTo(opts.Has("blocking"), &hasProperty)) {
  isBlocking = hasProperty &&
                MaybeUnwrap(MaybeUnwrap(opts.Get("blocking")).ToBoolean());
} else {
  env.GetAndClearPendingException();
}