Skip to content
Closed
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
Prev Previous commit
Next Next commit
test: added test for assert.ok() changes
Added tests to check if any previous behaviour where instanceof was
not throwing a custom error is working with the isNativeError change
in assert.ok()
  • Loading branch information
NiharPhansalkar committed Dec 28, 2023
commit cf550ebef5982819094684c3a309cded691bd3d5
48 changes: 47 additions & 1 deletion test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const start = 'Expected values to be strictly deep-equal:';
const actExp = '+ actual - expected';

assert.ok(a.AssertionError.prototype instanceof Error,
'a.AssertionError instanceof Error');
'a.AssertionError instanceof Error');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'a.AssertionError instanceof Error');
'a.AssertionError instanceof Error');


assert.throws(() => a(false), a.AssertionError, 'ok(false)');
assert.throws(() => a.ok(false), a.AssertionError, 'ok(false)');
Expand All @@ -55,6 +55,52 @@ assert.throws(() => a.ok(false), a.AssertionError, 'ok(false)');
assert.ok(threw, 'Error: ok(false)');
}

// Throw message if the message is instanceof Error.
Comment thread
NiharPhansalkar marked this conversation as resolved.
Outdated
{
let threw = false;
const context = vm.createContext();
try {
const errorConstructor = vm.runInContext('Error', context);
const customError = new errorConstructor('ok(false)');
assert.ok(false, customError.message);
} catch (e) {
threw = true;
assert.ok(e instanceof Error);
}
assert.ok(threw, 'Error: ok(false)');
Comment thread
NiharPhansalkar marked this conversation as resolved.
Outdated
}

Comment on lines +58 to +68

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Thrown error should be the passed through error instance of the native error
{
const context = vm.createContext();
const error = vm.runInContext('new TypeError("custom error")', context);
assert.throws(() => assert(false, error), {
message: 'custom error',
name: 'TypeError'
});
}

// Throw message if the message is instanceof TypeError.
{
let threw = false;
const context = vm.createContext();
context.TypeError = TypeError;
try {
const TypeErrorConstructor = vm.runInContext('TypeError', context);
const customTypeError = new TypeErrorConstructor('ok(false)');
assert.ok(false, customTypeError);
} catch (e) {
threw = true;
assert.ok(e instanceof Error);
}
assert.ok(threw, 'TypeError: ok(false)');
}

// Throw message if the message is instanceof SyntaxError.
{
let threw = false;
const context = vm.createContext();
context.SyntaxError = SyntaxError;
try {
const SyntaxErrorConstructor = vm.runInContext('SyntaxError', context);
const customSyntaxError = new SyntaxErrorConstructor('ok(false)');
assert.ok(false, customSyntaxError);
} catch (e) {
threw = true;
assert.ok(e instanceof Error);
}
assert.ok(threw, 'SyntaxError: ok(false)');
}

a(true);
a('test', 'ok(\'test\')');
Expand Down