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