Skip to content

Commit

Permalink
test, lib: revised tests and shortened assert code
Browse files Browse the repository at this point in the history
Changed the tests to check for correct throwing of
errors and combined if/else into one in assert.js
to make code concise.
  • Loading branch information
NiharPhansalkar committed Dec 28, 2023
1 parent cf550eb commit 1de2680
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
4 changes: 1 addition & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ function innerOk(fn, argLen, value, message) {
} else if (message == null) {
generatedMessage = true;
message = getErrMessage(message, fn);
} else if (isNativeError(message)) {
throw message;
} else if (message instanceof Error) {
} else if (isNativeError(message) || message instanceof Error) {
throw message;
}

Expand Down
51 changes: 13 additions & 38 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,51 +55,26 @@ assert.throws(() => a.ok(false), a.AssertionError, 'ok(false)');
assert.ok(threw, 'Error: ok(false)');
}

// Throw message if the message is instanceof Error.
// Thrown error should be the passed through error instance of the native error
{
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)');
}
const error = vm.runInContext('new TypeError("custom error")', context);

// 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)');
assert.throws(() => assert(false, error), {
message: 'custom error',
name: 'TypeError'
});
}

// Throw message if the message is instanceof SyntaxError.
// Thrown error should be the passed through error instance of the native error
{
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)');
const error = vm.runInContext('new SyntaxError("custom error")', context);

assert.throws(() => assert(false, error), {
message: 'custom error',
name: 'SyntaxError'
});
}

a(true);
Expand Down

0 comments on commit 1de2680

Please sign in to comment.