Skip to content

Commit

Permalink
typings: add JSDoc typings for assert
Browse files Browse the repository at this point in the history
PR-URL: #38188
Reviewed-By: Bradley Farias <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
VoltrexKeyva authored and targos committed Apr 15, 2021
1 parent c3a5e15 commit 82d5988
Showing 1 changed file with 123 additions and 8 deletions.
131 changes: 123 additions & 8 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ function innerFail(obj) {
throw new AssertionError(obj);
}

/**
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @param {string} [operator]
* @param {Function} [stackStartFn]
* @returns {never}
*/
function fail(actual, expected, message, operator, stackStartFn) {
const argsLen = arguments.length;

Expand Down Expand Up @@ -405,14 +413,24 @@ function innerOk(fn, argLen, value, message) {
}
}

// Pure assertion tests whether a value is truthy, as determined
// by !!value.
/**
* Pure assertion tests whether a value is truthy, as determined
* by !!value.
* @param {...any} args
* @returns {void}
*/
function ok(...args) {
innerOk(ok, args.length, ...args);
}
assert.ok = ok;

// The equality assertion tests shallow, coercive equality with ==.
/**
* The equality assertion tests shallow, coercive equality with ==.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
/* eslint-disable no-restricted-properties */
assert.equal = function equal(actual, expected, message) {
if (arguments.length < 2) {
Expand All @@ -430,8 +448,14 @@ assert.equal = function equal(actual, expected, message) {
}
};

// The non-equality assertion tests for whether two objects are not
// equal with !=.
/**
* The non-equality assertion tests for whether two objects are not
* equal with !=.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
assert.notEqual = function notEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
Expand All @@ -448,7 +472,13 @@ assert.notEqual = function notEqual(actual, expected, message) {
}
};

// The equivalence assertion tests a deep equality relation.
/**
* The deep equivalence assertion tests a deep equality relation.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
assert.deepEqual = function deepEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
Expand All @@ -465,7 +495,13 @@ assert.deepEqual = function deepEqual(actual, expected, message) {
}
};

// The non-equivalence assertion tests for any deep inequality.
/**
* The deep non-equivalence assertion tests for any deep inequality.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
Expand All @@ -483,6 +519,14 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
};
/* eslint-enable */

/**
* The deep strict equivalence assertion tests a deep strict equality
* relation.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
Expand All @@ -499,6 +543,14 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
}
};

/**
* The deep strict non-equivalence assertion tests for any deep strict
* inequality.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
assert.notDeepStrictEqual = notDeepStrictEqual;
function notDeepStrictEqual(actual, expected, message) {
if (arguments.length < 2) {
Expand All @@ -516,6 +568,13 @@ function notDeepStrictEqual(actual, expected, message) {
}
}

/**
* The strict equivalence assertion tests a strict equality relation.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
assert.strictEqual = function strictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
Expand All @@ -531,6 +590,13 @@ assert.strictEqual = function strictEqual(actual, expected, message) {
}
};

/**
* The strict non-equivalence assertion tests for any strict inequality.
* @param {any} actual
* @param {any} expected
* @param {string | Error} [message]
* @returns {void}
*/
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
Expand Down Expand Up @@ -839,22 +905,51 @@ function expectsNoError(stackStartFn, actual, error, message) {
throw actual;
}

/**
* Expects the function `promiseFn` to throw an error.
* @param {() => any} promiseFn
* @param {...any} [args]
* @returns {void}
*/
assert.throws = function throws(promiseFn, ...args) {
expectsError(throws, getActual(promiseFn), ...args);
};

/**
* Expects `promiseFn` function or its value to reject.
* @param {() => Promise<any>} promiseFn
* @param {...any} [args]
* @returns {Promise<void>}
*/
assert.rejects = async function rejects(promiseFn, ...args) {
expectsError(rejects, await waitForActual(promiseFn), ...args);
};

/**
* Asserts that the function `fn` does not throw an error.
* @param {() => any} fn
* @param {...any} [args]
* @returns {void}
*/
assert.doesNotThrow = function doesNotThrow(fn, ...args) {
expectsNoError(doesNotThrow, getActual(fn), ...args);
};

/**
* Expects `fn` or its value to not reject.
* @param {() => Promise<any>} fn
* @param {...any} [args]
* @returns {Promise<void>}
*/
assert.doesNotReject = async function doesNotReject(fn, ...args) {
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
};

/**
* Throws `value` if the value is not `null` or `undefined`.
* @param {any} err
* @returns {void}
*/
assert.ifError = function ifError(err) {
if (err !== null && err !== undefined) {
let message = 'ifError got unwanted exception: ';
Expand Down Expand Up @@ -939,24 +1034,44 @@ function internalMatch(string, regexp, message, fn) {
}
}

/**
* Expects the `string` input to match the regular expression.
* @param {string} string
* @param {RegExp} regexp
* @param {string | Error} [message]
* @returns {void}
*/
assert.match = function match(string, regexp, message) {
internalMatch(string, regexp, message, match);
};

/**
* Expects the `string` input not to match the regular expression.
* @param {string} string
* @param {RegExp} regexp
* @param {string | Error} [message]
* @returns {void}
*/
assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
internalMatch(string, regexp, message, doesNotMatch);
};

assert.CallTracker = CallTracker;

// Expose a strict only variant of assert
/**
* Expose a strict only variant of assert.
* @param {...any} args
* @returns {void}
*/
function strict(...args) {
innerOk(strict, args.length, ...args);
}

assert.strict = ObjectAssign(strict, assert, {
equal: assert.strictEqual,
deepEqual: assert.deepStrictEqual,
notEqual: assert.notStrictEqual,
notDeepEqual: assert.notDeepStrictEqual
});

assert.strict.strict = assert.strict;

0 comments on commit 82d5988

Please sign in to comment.