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 <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: James M Snell <[email protected]>
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‎

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,47 @@ const common = require('../../common');
44
consttest_exception=require(`./build/${common.buildType}/test_exception`);
55
constassert=require('assert');
66
consttheError=newError('Some error');
7-
functionthrowTheError(){
8-
throwtheError;
7+
8+
{
9+
constthrowTheError=()=>{throwtheError;};
10+
11+
// Test that the native side successfully captures the exception
12+
letreturnedError=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-
letcaughtError;
11-
12-
// Test that the native side successfully captures the exception
13-
letreturnedError=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-
returnerr===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+
letcaughtError;
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)