Skip to content
This repository was archived by the owner on Jun 18, 2021. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add test for errors thrown from n-api addon
  • Loading branch information
richardlau committed Jun 26, 2019
commit bb468b50ca8d0963e162e38700e7aeb515521f11
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# Build directories
/build/
/node_modules/
/test/*/build/

# Package files
NodeReport*.txt
Expand Down
10 changes: 10 additions & 0 deletions test/napi_throw/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"targets": [
{
"target_name": "test_napi_throw",
"sources": [
"test_napi_throw.c"
]
}
]
}
5 changes: 5 additions & 0 deletions test/napi_throw/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/napi_throw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test_napi_throw",
"version": "1.0.0",
"description": "Throw an error.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"install": "node-gyp rebuild"
},
"author": "",
"license": "ISC",
"repository": "https://github.com/nodejs/node-report",
"gypfile": true
}
28 changes: 28 additions & 0 deletions test/napi_throw/test_napi_throw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <assert.h>

#include <node_api.h>

napi_value ThrowError(napi_env env, napi_callback_info info) {
napi_throw_error(env, NULL, "an error occurred");
return NULL;
}

static napi_value Init(napi_env env, napi_value exports) {
napi_value result;
assert(napi_create_object(env, &result) == napi_ok);
Comment thread
richardlau marked this conversation as resolved.
Outdated
napi_value exported_function;
assert(napi_create_function(env,
"throwError",
NAPI_AUTO_LENGTH,
ThrowError,
NULL,
&exported_function) == napi_ok);

assert(napi_set_named_property(env,
result,
"throwError",
exported_function) == napi_ok);
return result;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
22 changes: 22 additions & 0 deletions test/test-napi-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const tap = require('tap');

// Build addon that throws an error
const cp = require('child_process');
const path = require('path');
const build = cp.spawnSync('npm', [ 'install', '.' ],
{ cwd: path.join(__dirname, 'napi_throw'),
encoding: 'utf8' });
tap.equal(build.stderr, '', 'No errors building addon');
tap.equal(build.signal, null, 'Failed to build addon');
tap.equal(build.status, 0, 'Failed to build addon');

// Test catching the error does not trigger an UncaughtException report
const common = require('./common.js');
const report = require('../');
const obj = require('./napi_throw/build/Release/test_napi_throw.node');
tap.throws(obj.throwError);

const reports = common.findReports(process.pid);
tap.equals(reports, [], 'No reports should be generated');