assert,util: handle invalid dates as equal in deep comparison

Invalid dates are now handled as equal in all deep comparisons.

PR-URL: https://github.com/nodejs/node/pull/57627
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
Ruben Bridgewater 2025-06-21 06:00:23 +02:00 committed by GitHub
parent c3b986853c
commit 11222f1a27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 5 deletions

View file

@ -22,6 +22,7 @@ const primValues = {
'empty_object': {},
'regexp': /abc/i,
'date': new Date(),
'invalidDate': new Date('foo'),
};
const primValues2 = {
@ -34,6 +35,7 @@ const primValues2 = {
'empty_object': {},
'regexp': /abc/i,
'date': new Date(primValues.date),
'invalidDate': new Date('foo'),
};
const primValuesUnequal = {
@ -49,6 +51,7 @@ const primValuesUnequal = {
'empty_object': [],
'regexp': /abc/g,
'date': new Date(primValues.date.getTime() + 1),
'invalidDate': new Date(),
};
const bench = common.createBenchmark(main, {

View file

@ -231,6 +231,9 @@ An alias of [`assert.ok()`][].
<!-- YAML
added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57627
description: Invalid dates are now considered equal.
- version: v24.0.0
pr-url: https://github.com/nodejs/node/pull/57622
description: Recursion now stops when either side encounters a circular
@ -422,6 +425,9 @@ parameter is an instance of {Error} then it will be thrown instead of the
<!-- YAML
added: v1.2.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57627
description: Invalid dates are now considered equal.
- version: v24.0.0
pr-url: https://github.com/nodejs/node/pull/57622
description: Recursion now stops when either side encounters a circular
@ -2177,9 +2183,12 @@ added:
- v23.4.0
- v22.13.0
changes:
- version: v24.0.0
pr-url: https://github.com/nodejs/node/pull/57370
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57627
description: Invalid dates are now considered equal.
- version: v24.0.0
pr-url: https://github.com/nodejs/node/pull/57370
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
-->
* `actual` {any}

View file

@ -299,10 +299,15 @@ function objectComparisonStart(val1, val2, mode, memos) {
} else if (val1Tag === '[object Object]') {
return keyCheck(val1, val2, mode, memos, kNoIterator);
} else if (isDate(val1)) {
if (!isDate(val2) ||
DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
if (!isDate(val2)) {
return false;
}
const time1 = DatePrototypeGetTime(val1);
const time2 = DatePrototypeGetTime(val2);
if (time1 !== time2) {
// eslint-disable-next-line no-self-compare
return time1 !== time1 && time2 !== time2;
}
} else if (isRegExp(val1)) {
if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
return false;

View file

@ -752,6 +752,8 @@ test('Additional tests', () => {
assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
assertDeepAndStrictEqual(new Date('foo'), new Date('bar'));
assertDeepAndStrictEqual(/a/, /a/);
assertDeepAndStrictEqual(/a/g, /a/g);
assertDeepAndStrictEqual(/a/i, /a/i);