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

assert: validate required arguments #26641

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 0 additions & 16 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,6 @@ module.exports = {
selector: 'ThrowStatement > CallExpression[callee.name=/Error$/]',
message: 'Use `new` keyword when throwing an `Error`.',
},
{
selector: "CallExpression[callee.property.name='notDeepStrictEqual'][arguments.length<2]",
message: 'assert.notDeepStrictEqual() must be invoked with at least two arguments.',
},
{
selector: "CallExpression[callee.property.name='deepStrictEqual'][arguments.length<2]",
message: 'assert.deepStrictEqual() must be invoked with at least two arguments.',
},
{
selector: "CallExpression[callee.property.name='notStrictEqual'][arguments.length<2]",
message: 'assert.notStrictEqual() must be invoked with at least two arguments.',
},
{
selector: "CallExpression[callee.property.name='strictEqual'][arguments.length<2]",
message: 'assert.strictEqual() must be invoked with at least two arguments.',
},
{
selector: "CallExpression[callee.property.name='notDeepStrictEqual'][arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression']):not([arguments.1.type='UnaryExpression'])",
message: 'The first argument should be the `actual`, not the `expected` value.',
Expand Down
27 changes: 26 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const { codes: {
ERR_AMBIGUOUS_ARGUMENT,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_RETURN_VALUE
ERR_INVALID_RETURN_VALUE,
ERR_MISSING_ARGS
} } = require('internal/errors');
const AssertionError = require('internal/assert/assertion_error');
const { openSync, closeSync, readSync } = require('fs');
Expand Down Expand Up @@ -351,6 +352,9 @@ assert.ok = ok;
// The equality assertion tests shallow, coercive equality with ==.
/* eslint-disable no-restricted-properties */
assert.equal = function equal(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
// eslint-disable-next-line eqeqeq
if (actual != expected) {
innerFail({
Expand All @@ -366,6 +370,9 @@ assert.equal = function equal(actual, expected, message) {
// The non-equality assertion tests for whether two objects are not
// equal with !=.
assert.notEqual = function notEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
// eslint-disable-next-line eqeqeq
if (actual == expected) {
innerFail({
Expand All @@ -380,6 +387,9 @@ assert.notEqual = function notEqual(actual, expected, message) {

// The equivalence assertion tests a deep equality relation.
assert.deepEqual = function deepEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (isDeepEqual === undefined) lazyLoadComparison();
if (!isDeepEqual(actual, expected)) {
innerFail({
Expand All @@ -394,6 +404,9 @@ assert.deepEqual = function deepEqual(actual, expected, message) {

// The non-equivalence assertion tests for any deep inequality.
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (isDeepEqual === undefined) lazyLoadComparison();
if (isDeepEqual(actual, expected)) {
innerFail({
Expand All @@ -408,6 +421,9 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
/* eslint-enable */

assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (isDeepEqual === undefined) lazyLoadComparison();
if (!isDeepStrictEqual(actual, expected)) {
innerFail({
Expand All @@ -422,6 +438,9 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {

assert.notDeepStrictEqual = notDeepStrictEqual;
function notDeepStrictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (isDeepEqual === undefined) lazyLoadComparison();
if (isDeepStrictEqual(actual, expected)) {
innerFail({
Expand All @@ -435,6 +454,9 @@ function notDeepStrictEqual(actual, expected, message) {
}

assert.strictEqual = function strictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (!Object.is(actual, expected)) {
innerFail({
actual,
Expand All @@ -447,6 +469,9 @@ assert.strictEqual = function strictEqual(actual, expected, message) {
};

assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('actual', 'expected');
}
if (Object.is(actual, expected)) {
innerFail({
actual,
Expand Down
42 changes: 41 additions & 1 deletion test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ assert.throws(
'- undefined',
].join('\n');
assert.throws(
() => assert.deepEqual([1, 2, 1]),
() => assert.deepEqual([1, 2, 1], undefined),
{ message });

message = [
Expand Down Expand Up @@ -1147,3 +1147,43 @@ assert.throws(
}
assert(threw);
}

assert.throws(
() => a.equal(1),
{ code: 'ERR_MISSING_ARGS' }
);

assert.throws(
() => a.deepEqual(/a/),
{ code: 'ERR_MISSING_ARGS' }
);

assert.throws(
() => a.notEqual(null),
{ code: 'ERR_MISSING_ARGS' }
);

assert.throws(
() => a.notDeepEqual('test'),
{ code: 'ERR_MISSING_ARGS' }
);

assert.throws(
() => a.strictEqual({}),
{ code: 'ERR_MISSING_ARGS' }
);

assert.throws(
() => a.deepStrictEqual(Symbol()),
{ code: 'ERR_MISSING_ARGS' }
);

assert.throws(
() => a.notStrictEqual(5n),
{ code: 'ERR_MISSING_ARGS' }
);

assert.throws(
() => a.notDeepStrictEqual(undefined),
{ code: 'ERR_MISSING_ARGS' }
);