Skip to content
Closed
Changes from all commits
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
45 changes: 23 additions & 22 deletions lib/assert.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,10 +22,12 @@

const {
ArrayPrototypeIndexOf,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSome,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
Expand DownExpand Up@@ -374,7 +376,7 @@ function getErrMessage(message, fn) {
}
}

function innerOk(fn, argLen, value, message) {
function innerOk(fn, { length: argLen, 0: value, 1: message }) {
if (!value) {
let generatedMessage = false;

Expand DownExpand Up@@ -403,7 +405,7 @@ function innerOk(fn, argLen, value, message) {
// Pure assertion tests whether a value is truthy, as determined
// by !!value.
function ok(...args) {
innerOk(ok, args.length, ...args);
innerOk(ok, args);
}
assert.ok = ok;

Expand DownExpand Up@@ -543,7 +545,7 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {

class Comparison {
constructor(obj, keys, actual) {
for (const key of keys) {
ArrayPrototypeForEach(keys, (key) => {
if (key in obj) {
if (actual !== undefined &&
typeof actual[key] === 'string' &&
Expand All@@ -554,7 +556,7 @@ class Comparison {
this[key] = obj[key];
}
}
}
});
}
}

Expand DownExpand Up@@ -626,14 +628,13 @@ function expectedException(actual, expected, message, fn) {
expected, 'may not be an empty object');
}
if (isDeepEqual === undefined) lazyLoadComparison();
for (const key of keys) {
if (typeof actual[key] === 'string' &&
isRegExp(expected[key]) &&
RegExpPrototypeTest(expected[key], actual[key])) {
continue;
ArrayPrototypeForEach(keys, (key) => {
if (typeof actual[key] !== 'string' ||
!isRegExp(expected[key]) ||
!RegExpPrototypeTest(expected[key], actual[key])) {
compareExceptionKey(actual, expected, key, message, keys, fn);
}
compareExceptionKey(actual, expected, key, message, keys, fn);
}
});
return;
}
// Guard instanceof against arrow functions as they don't have a prototype.
Expand DownExpand Up@@ -739,9 +740,9 @@ async function waitForActual(promiseFn) {
return NO_EXCEPTION_SENTINEL;
}

function expectsError(stackStartFn, actual, error, message) {
function expectsError(stackStartFn, actual, { 0: error, 1: message, length }) {
if (typeof error === 'string') {
if (arguments.length === 4) {
if (length === 2) {
throw new ERR_INVALID_ARG_TYPE('error',
['Object', 'Error', 'Function', 'RegExp'],
error);
Expand DownExpand Up@@ -811,7 +812,7 @@ function hasMatchingError(actual, expected) {
return ReflectApply(expected, {}, [actual]) === true;
}

function expectsNoError(stackStartFn, actual, error, message) {
function expectsNoError(stackStartFn, actual, { 0: error, 1: message }) {
if (actual === NO_EXCEPTION_SENTINEL)
return;

Expand All@@ -837,19 +838,19 @@ function expectsNoError(stackStartFn, actual, error, message) {
}

assert.throws = function throws(promiseFn, ...args) {
expectsError(throws, getActual(promiseFn), ...args);
expectsError(throws, getActual(promiseFn), args);
};

assert.rejects = async function rejects(promiseFn, ...args) {
expectsError(rejects, await waitForActual(promiseFn), ...args);
expectsError(rejects, await waitForActual(promiseFn), args);
};

assert.doesNotThrow = function doesNotThrow(fn, ...args) {
expectsNoError(doesNotThrow, getActual(fn), ...args);
expectsNoError(doesNotThrow, getActual(fn), args);
};

assert.doesNotReject = async function doesNotReject(fn, ...args) {
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
expectsNoError(doesNotReject, await waitForActual(fn), args);
};

assert.ifError = function ifError(err) {
Expand DownExpand Up@@ -884,15 +885,15 @@ assert.ifError = function ifError(err) {
ArrayPrototypeShift(tmp2);
// Filter all frames existing in err.stack.
let tmp1 = StringPrototypeSplit(newErr.stack, '\n');
for (const errFrame of tmp2) {
ArrayPrototypeSome(tmp2, (errFrame) => {
// Find the first occurrence of the frame.
const pos = ArrayPrototypeIndexOf(tmp1, errFrame);
if (pos !== -1) {
// Only keep new frames.
tmp1 = ArrayPrototypeSlice(tmp1, 0, pos);
break;
return true;
}
}
});
newErr.stack =
`${ArrayPrototypeJoin(tmp1, '\n')}\n${ArrayPrototypeJoin(tmp2, '\n')}`;
}
Expand DownExpand Up@@ -948,7 +949,7 @@ assert.CallTracker = CallTracker;

// Expose a strict only variant of assert
function strict(...args) {
innerOk(strict, args.length, ...args);
innerOk(strict, args);
}
assert.strict = ObjectAssign(strict, assert, {
equal: assert.strictEqual,
Expand Down