Skip to content

Commit 5b1b74c

Browse files
TrottMylesBorins
authored andcommitted
test: refactor addons-napi/test_exception/test.js
* provide block scoping to prevent unintended side effects * remove confusing and unnecessary assertion message * use consisitent `actual, expected` argument order for assertions Backport-PR-URL: #19265 PR-URL: #18340 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e15f577 commit 5b1b74c

File tree

1 file changed

+42
-44
lines changed
  • test/addons-napi/test_exception

1 file changed

+42
-44
lines changed

test/addons-napi/test_exception/test.js

+42-44
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,47 @@ const common = require('../../common');
44
const test_exception = require(`./build/${common.buildType}/test_exception`);
55
const assert = require('assert');
66
const theError = new Error('Some error');
7-
function throwTheError() {
8-
throw theError;
7+
8+
{
9+
const throwTheError = () => { throw theError; };
10+
11+
// Test that the native side successfully captures the exception
12+
let returnedError = test_exception.returnException(throwTheError);
13+
assert.strictEqual(theError, returnedError);
14+
15+
// Test that the native side passes the exception through
16+
assert.throws(
17+
() => { test_exception.allowException(throwTheError); },
18+
(err) => err === theError
19+
);
20+
21+
// Test that the exception thrown above was marked as pending
22+
// before it was handled on the JS side
23+
assert.strictEqual(test_exception.wasPending(), true,
24+
'VM was marked as having an exception pending' +
25+
' when it was allowed through');
26+
27+
// Test that the native side does not capture a non-existing exception
28+
returnedError = test_exception.returnException(common.mustCall());
29+
assert.strictEqual(returnedError, undefined,
30+
'Returned error should be undefined when no exception is' +
31+
` thrown, but ${returnedError} was passed`);
932
}
10-
let caughtError;
11-
12-
// Test that the native side successfully captures the exception
13-
let returnedError = test_exception.returnException(throwTheError);
14-
assert.strictEqual(theError, returnedError);
15-
16-
// Test that the native side passes the exception through
17-
assert.throws(
18-
() => {
19-
test_exception.allowException(throwTheError);
20-
},
21-
function(err) {
22-
return err === theError;
23-
},
24-
'Thrown exception was allowed to pass through unhindered'
25-
);
26-
27-
// Test that the exception thrown above was marked as pending
28-
// before it was handled on the JS side
29-
assert.strictEqual(test_exception.wasPending(), true,
30-
'VM was marked as having an exception pending' +
31-
' when it was allowed through');
32-
33-
// Test that the native side does not capture a non-existing exception
34-
returnedError = test_exception.returnException(common.mustCall());
35-
assert.strictEqual(undefined, returnedError,
36-
'Returned error should be undefined when no exception is' +
37-
` thrown, but ${returnedError} was passed`);
38-
39-
// Test that no exception appears that was not thrown by us
40-
try {
41-
test_exception.allowException(common.mustCall());
42-
} catch (anError) {
43-
caughtError = anError;
33+
34+
{
35+
// Test that no exception appears that was not thrown by us
36+
let caughtError;
37+
try {
38+
test_exception.allowException(common.mustCall());
39+
} catch (anError) {
40+
caughtError = anError;
41+
}
42+
assert.strictEqual(caughtError, undefined,
43+
'No exception originated on the native side, but' +
44+
` ${caughtError} was passed`);
45+
46+
// Test that the exception state remains clear when no exception is thrown
47+
assert.strictEqual(test_exception.wasPending(), false,
48+
'VM was not marked as having an exception pending' +
49+
' when none was allowed through');
4450
}
45-
assert.strictEqual(undefined, caughtError,
46-
'No exception originated on the native side, but' +
47-
` ${caughtError} was passed`);
48-
49-
// Test that the exception state remains clear when no exception is thrown
50-
assert.strictEqual(test_exception.wasPending(), false,
51-
'VM was not marked as having an exception pending' +
52-
' when none was allowed through');

0 commit comments

Comments
 (0)