node/test/js-native-api/test_function/test.js
Michael Dawson 79b2c9a371 node-api: handle pending exception in cb wrapper
fixes: https://github.com/nodejs/node-addon-api/issues/1025

The functionreference test from the node-api tests
was reporting a failed v8 check when Node.js was compiled
as debug. The failure was because an exception was
pending but the C++ wrappers were returning
a return value that was invalid.

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/39476
Fixes: https://github.com/nodejs/node-addon-api/issues/1025
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2021-08-03 16:38:45 -04:00

53 lines
1.2 KiB
JavaScript

'use strict';
// Flags: --expose-gc
const common = require('../../common');
const assert = require('assert');
// Testing api calls for function
const test_function = require(`./build/${common.buildType}/test_function`);
function func1() {
return 1;
}
assert.strictEqual(test_function.TestCall(func1), 1);
function func2() {
console.log('hello world!');
return null;
}
assert.strictEqual(test_function.TestCall(func2), null);
function func3(input) {
return input + 1;
}
assert.strictEqual(test_function.TestCall(func3, 1), 2);
function func4(input) {
return func3(input);
}
assert.strictEqual(test_function.TestCall(func4, 1), 2);
assert.strictEqual(test_function.TestName.name, 'Name');
assert.strictEqual(test_function.TestNameShort.name, 'Name_');
let tracked_function = test_function.MakeTrackedFunction(common.mustCall());
assert(!!tracked_function);
tracked_function = null;
global.gc();
assert.deepStrictEqual(test_function.TestCreateFunctionParameters(), {
envIsNull: 'Invalid argument',
nameIsNull: 'napi_ok',
cbIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument'
});
assert.throws(
() => test_function.TestBadReturnExceptionPending(),
{
code: 'throwing exception',
name: 'Error'
}
);