GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — node-addon-api/test at main · nodejs/node-addon-api

Expand file tree

/

test

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

test

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
addon_build
addon_build
 
 
basic_types
basic_types
 
 
child_processes
child_processes
 
 
common
common
 
 
dataview
dataview
 
 
globalObject
globalObject
 
 
maybe
maybe
 
 
object
object
 
 
require_basic_finalizers
require_basic_finalizers
 
 
threadsafe_function
threadsafe_function
 
 
typed_threadsafe_function
typed_threadsafe_function
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
addon.cc
addon.cc
 
 
addon.js
addon.js
 
 
addon_data.cc
addon_data.cc
 
 
addon_data.js
addon_data.js
 
 
array_buffer.cc
array_buffer.cc
 
 
array_buffer.js
array_buffer.js
 
 
async_context.cc
async_context.cc
 
 
async_context.js
async_context.js
 
 
async_progress_queue_worker.cc
async_progress_queue_worker.cc
 
 
async_progress_queue_worker.js
async_progress_queue_worker.js
 
 
async_progress_worker.cc
async_progress_worker.cc
 
 
async_progress_worker.js
async_progress_worker.js
 
 
async_worker.cc
async_worker.cc
 
 
async_worker.js
async_worker.js
 
 
async_worker_nocallback.js
async_worker_nocallback.js
 
 
async_worker_persistent.cc
async_worker_persistent.cc
 
 
async_worker_persistent.js
async_worker_persistent.js
 
 
bigint.cc
bigint.cc
 
 
bigint.js
bigint.js
 
 
binding-swallowexcept.cc
binding-swallowexcept.cc
 
 
binding.cc
binding.cc
 
 
binding.gyp
binding.gyp
 
 
buffer.cc
buffer.cc
 
 
buffer.h
buffer.h
 
 
buffer.js
buffer.js
 
 
buffer_new_or_copy-inl.h
buffer_new_or_copy-inl.h
 
 
buffer_no_external.cc
buffer_no_external.cc
 
 
callbackInfo.cc
callbackInfo.cc
 
 
callbackInfo.js
callbackInfo.js
 
 
callbackscope.cc
callbackscope.cc
 
 
callbackscope.js
callbackscope.js
 
 
date.cc
date.cc
 
 
date.js
date.js
 
 
env_cleanup.cc
env_cleanup.cc
 
 
env_cleanup.js
env_cleanup.js
 
 
env_misc.cc
env_misc.cc
 
 
env_misc.js
env_misc.js
 
 
error.cc
error.cc
 
 
error.js
error.js
 
 
error_handling_for_primitives.cc
error_handling_for_primitives.cc
 
 
error_handling_for_primitives.js
error_handling_for_primitives.js
 
 
error_terminating_environment.js
error_terminating_environment.js
 
 
except_all.cc
except_all.cc
 
 
except_all.js
except_all.js
 
 
exports.js
exports.js
 
 
external.cc
external.cc
 
 
external.js
external.js
 
 
finalizer_order.cc
finalizer_order.cc
 
 
finalizer_order.js
finalizer_order.js
 
 
function.cc
function.cc
 
 
function.js
function.js
 
 
function_reference.cc
function_reference.cc
 
 
function_reference.js
function_reference.js
 
 
handlescope.cc
handlescope.cc
 
 
handlescope.js
handlescope.js
 
 
index.js
index.js
 
 
memory_management.cc
memory_management.cc
 
 
memory_management.js
memory_management.js
 
 
movable_callbacks.cc
movable_callbacks.cc
 
 
movable_callbacks.js
movable_callbacks.js
 
 
name.cc
name.cc
 
 
name.js
name.js
 
 
napi_child.js
napi_child.js
 
 
object_reference.cc
object_reference.cc
 
 
object_reference.js
object_reference.js
 
 
objectwrap.cc
objectwrap.cc
 
 
objectwrap.js
objectwrap.js
 
 
objectwrap_constructor_exception.cc
objectwrap_constructor_exception.cc
 
 
objectwrap_constructor_exception.js
objectwrap_constructor_exception.js
 
 
objectwrap_function.cc
objectwrap_function.cc
 
 
objectwrap_function.js
objectwrap_function.js
 
 
objectwrap_multiple_inheritance.cc
objectwrap_multiple_inheritance.cc
 
 
objectwrap_multiple_inheritance.js
objectwrap_multiple_inheritance.js
 
 
objectwrap_removewrap.cc
objectwrap_removewrap.cc
 
 
objectwrap_removewrap.js
objectwrap_removewrap.js
 
 
objectwrap_worker_thread.js
objectwrap_worker_thread.js
 
 
promise.cc
promise.cc
 
 
promise.js
promise.js
 
 
reference.cc
reference.cc
 
 
reference.js
reference.js
 
 
run_script.cc
run_script.cc
 
 
run_script.js
run_script.js
 
 
shared_array_buffer.cc
shared_array_buffer.cc
 
 
shared_array_buffer.js
shared_array_buffer.js
 
 
symbol.cc
symbol.cc
 
 
symbol.js
symbol.js
 
 
testUtil.js
testUtil.js
 
 

README.md

Outline

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();
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.