Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add JSDoc typings for assert #38188

Closed
wants to merge 7 commits into from
Closed
Changes from 2 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
130 changes: 122 additions & 8 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,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 {void}
*/
function fail(actual, expected, message, operator, stackStartFn) {
const argsLen = arguments.length;

Expand Down Expand Up @@ -403,14 +411,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 @@ -428,8 +446,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 @@ -446,7 +470,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 @@ -463,7 +493,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 @@ -481,6 +517,13 @@ 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 @@ -497,6 +540,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 @@ -514,6 +565,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 @@ -529,6 +587,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 @@ -837,22 +902,51 @@ function expectsNoError(stackStartFn, actual, error, message) {
throw actual;
}

/**
* Expects the function `promiseFn` to throw an error.
* @param {Function} 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 {Function} promiseFn
* @param {...any} [args]
* @returns {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 {Function} 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 {Function} fn
* @param {...any} [args]
* @returns {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 @@ -937,24 +1031,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;