test: update multiple assert tests to use node:test

PR-URL: https://github.com/nodejs/node/pull/54585
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
James M Snell 2024-08-26 22:08:07 -07:00
parent 5060bbfabc
commit 9cbef482df
6 changed files with 1001 additions and 974 deletions

View File

@ -1,21 +1,18 @@
'use strict'; 'use strict';
const common = require('../common'); const { hasCrypto } = require('../common');
const { test } = require('node:test');
if (!common.hasCrypto) {
common.skip('missing crypto');
}
const assert = require('assert'); const assert = require('assert');
// Turn off no-restricted-properties because we are testing deepEqual!
/* eslint-disable no-restricted-properties */
// Disable colored output to prevent color codes from breaking assertion // Disable colored output to prevent color codes from breaking assertion
// message comparisons. This should only be an issue when process.stdout // message comparisons. This should only be an issue when process.stdout
// is a TTY. // is a TTY.
if (process.stdout.isTTY) if (process.stdout.isTTY)
process.env.NODE_DISABLE_COLORS = '1'; process.env.NODE_DISABLE_COLORS = '1';
// Turn off no-restricted-properties because we are testing deepEqual! test('', { skip: !hasCrypto }, () => {
/* eslint-disable no-restricted-properties */
// See https://github.com/nodejs/node/issues/10258 // See https://github.com/nodejs/node/issues/10258
{ {
const date = new Date('2016'); const date = new Date('2016');
@ -67,4 +64,5 @@ if (process.stdout.isTTY)
assert.throws(() => assert.deepStrictEqual(fakeProcess, process), assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
assert.AssertionError); assert.AssertionError);
} }
});
/* eslint-enable */ /* eslint-enable */

View File

@ -1,8 +1,9 @@
'use strict'; 'use strict';
const common = require('../common'); const { hasCrypto } = require('../common');
const assert = require('assert'); const assert = require('assert');
const util = require('util'); const util = require('util');
const { test } = require('node:test');
const { AssertionError } = assert; const { AssertionError } = assert;
const defaultMsgStart = 'Expected values to be strictly deep-equal:\n'; const defaultMsgStart = 'Expected values to be strictly deep-equal:\n';
const defaultMsgStartFull = `${defaultMsgStart}+ actual - expected`; const defaultMsgStartFull = `${defaultMsgStart}+ actual - expected`;
@ -36,6 +37,24 @@ function re(literals, ...values) {
}; };
} }
const date = new Date('2016');
class MyDate extends Date {
constructor(...args) {
super(...args);
this[0] = '1';
}
}
const date2 = new MyDate('2016');
class MyRegExp extends RegExp {
constructor(...args) {
super(...args);
this[0] = '1';
}
}
// The following deepEqual tests might seem very weird. // The following deepEqual tests might seem very weird.
// They just describe what it is now. // They just describe what it is now.
// That is why we discourage using deepEqual in our own tests. // That is why we discourage using deepEqual in our own tests.
@ -43,6 +62,7 @@ function re(literals, ...values) {
// Turn off no-restricted-properties because we are testing deepEqual! // Turn off no-restricted-properties because we are testing deepEqual!
/* eslint-disable no-restricted-properties */ /* eslint-disable no-restricted-properties */
test('deepEqual', () => {
const arr = new Uint8Array([120, 121, 122, 10]); const arr = new Uint8Array([120, 121, 122, 10]);
const buf = Buffer.from(arr); const buf = Buffer.from(arr);
// They have different [[Prototype]] // They have different [[Prototype]]
@ -97,18 +117,9 @@ assert.deepEqual(arr, buf);
); );
assert.notDeepEqual(arr, arr2); assert.notDeepEqual(arr, arr2);
} }
});
const date = new Date('2016'); test('date', () => {
class MyDate extends Date {
constructor(...args) {
super(...args);
this[0] = '1';
}
}
const date2 = new MyDate('2016');
assertNotDeepOrStrict(date, date2); assertNotDeepOrStrict(date, date2);
assert.throws( assert.throws(
() => assert.deepStrictEqual(date, date2), () => assert.deepStrictEqual(date, date2),
@ -128,14 +139,9 @@ assert.throws(
"+ '0': '1'\n+ }\n- 2016-01-01T00:00:00.000Z" "+ '0': '1'\n+ }\n- 2016-01-01T00:00:00.000Z"
} }
); );
});
class MyRegExp extends RegExp { test('regexp', () => {
constructor(...args) {
super(...args);
this[0] = '1';
}
}
const re1 = new RegExp('test'); const re1 = new RegExp('test');
const re2 = new MyRegExp('test'); const re2 = new MyRegExp('test');
@ -148,10 +154,12 @@ assert.throws(
"+ /test/\n- MyRegExp /test/ {\n- '0': '1'\n- }" "+ /test/\n- MyRegExp /test/ {\n- '0': '1'\n- }"
} }
); );
});
// For these weird cases, deepEqual should pass (at least for now), // For these weird cases, deepEqual should pass (at least for now),
// but deepStrictEqual should throw. // but deepStrictEqual should throw.
{ test('deepEqual should pass for these weird cases', () => {
const re2 = new MyRegExp('test');
const similar = new Set([ const similar = new Set([
{ 0: 1 }, // Object { 0: 1 }, // Object
new String('1'), // Object new String('1'), // Object
@ -178,7 +186,7 @@ assert.throws(
} }
} }
} }
} });
function assertDeepAndStrictEqual(a, b) { function assertDeepAndStrictEqual(a, b) {
assert.deepEqual(a, b); assert.deepEqual(a, b);
@ -222,7 +230,7 @@ function assertOnlyDeepEqual(a, b, err) {
); );
} }
// es6 Maps and Sets test('es6 Maps and Sets', () => {
assertDeepAndStrictEqual(new Set(), new Set()); assertDeepAndStrictEqual(new Set(), new Set());
assertDeepAndStrictEqual(new Map(), new Map()); assertDeepAndStrictEqual(new Map(), new Map());
@ -400,80 +408,6 @@ assertNotDeepOrStrict(
new Map([[undefined, true]]), new Map([[undefined, true]]),
new Map([[true, true]]) new Map([[true, true]])
); );
// GH-6416. Make sure circular refs don't throw.
{
const b = {};
b.b = b;
const c = {};
c.b = c;
assertDeepAndStrictEqual(b, c);
const d = {};
d.a = 1;
d.b = d;
const e = {};
e.a = 1;
e.b = {};
assertNotDeepOrStrict(d, e);
}
// GH-14441. Circular structures should be consistent
{
const a = {};
a.a = a;
const b = {};
b.a = {};
b.a.a = a;
assertDeepAndStrictEqual(a, b);
}
{
const a = {};
a.a = a;
const b = {};
b.a = b;
const c = {};
c.a = a;
assertDeepAndStrictEqual(b, c);
}
{
const a = new Set();
a.add(a);
const b = new Set();
b.add(b);
const c = new Set();
c.add(a);
assertDeepAndStrictEqual(b, c);
}
// https://github.com/nodejs/node-v0.x-archive/pull/7178
// Ensure reflexivity of deepEqual with `arguments` objects.
{
const args = (function() { return arguments; })();
assertNotDeepOrStrict([], args);
}
// More checking that arguments objects are handled correctly
{
// eslint-disable-next-line func-style
const returnArguments = function() { return arguments; };
const someArgs = returnArguments('a');
const sameArgs = returnArguments('a');
const diffArgs = returnArguments('b');
assertNotDeepOrStrict(someArgs, ['a']);
assertNotDeepOrStrict(someArgs, { '0': 'a' });
assertNotDeepOrStrict(someArgs, diffArgs);
assertDeepAndStrictEqual(someArgs, sameArgs);
}
{ {
const values = [ const values = [
123, 123,
@ -580,9 +514,80 @@ assertNotDeepOrStrict(
m4.set(m4, 2); m4.set(m4, 2);
assertDeepAndStrictEqual(m3, m4); assertDeepAndStrictEqual(m3, m4);
} }
});
// Handle sparse arrays. test('GH-6416. Make sure circular refs do not throw', () => {
const b = {};
b.b = b;
const c = {};
c.b = c;
assertDeepAndStrictEqual(b, c);
const d = {};
d.a = 1;
d.b = d;
const e = {};
e.a = 1;
e.b = {};
assertNotDeepOrStrict(d, e);
});
test('GH-14441. Circular structures should be consistent', () => {
{ {
const a = {};
a.a = a;
const b = {};
b.a = {};
b.a.a = a;
assertDeepAndStrictEqual(a, b);
}
{
const a = {};
a.a = a;
const b = {};
b.a = b;
const c = {};
c.a = a;
assertDeepAndStrictEqual(b, c);
}
{
const a = new Set();
a.add(a);
const b = new Set();
b.add(b);
const c = new Set();
c.add(a);
assertDeepAndStrictEqual(b, c);
}
});
// https://github.com/nodejs/node-v0.x-archive/pull/7178
test('Ensure reflexivity of deepEqual with `arguments` objects.', () => {
const args = (function() { return arguments; })();
assertNotDeepOrStrict([], args);
});
test('More checking that arguments objects are handled correctly', () => {
// eslint-disable-next-line func-style
const returnArguments = function() { return arguments; };
const someArgs = returnArguments('a');
const sameArgs = returnArguments('a');
const diffArgs = returnArguments('b');
assertNotDeepOrStrict(someArgs, ['a']);
assertNotDeepOrStrict(someArgs, { '0': 'a' });
assertNotDeepOrStrict(someArgs, diffArgs);
assertDeepAndStrictEqual(someArgs, sameArgs);
});
test('Handle sparse arrays', () => {
/* eslint-disable no-sparse-arrays */ /* eslint-disable no-sparse-arrays */
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]); assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
assertNotDeepOrStrict([1, , , 3], [1, , , 3, , , ]); assertNotDeepOrStrict([1, , , 3], [1, , , 3, , , ]);
@ -596,24 +601,23 @@ assertNotDeepOrStrict(
assertNotDeepOrStrict(a, b); assertNotDeepOrStrict(a, b);
a[0] = true; a[0] = true;
assertNotDeepOrStrict(a, b); assertNotDeepOrStrict(a, b);
} });
// Handle different error messages test('Handle different error messages', () => {
{
const err1 = new Error('foo1'); const err1 = new Error('foo1');
assertNotDeepOrStrict(err1, new Error('foo2'), assert.AssertionError); assertNotDeepOrStrict(err1, new Error('foo2'), assert.AssertionError);
assertNotDeepOrStrict(err1, new TypeError('foo1'), assert.AssertionError); assertNotDeepOrStrict(err1, new TypeError('foo1'), assert.AssertionError);
assertDeepAndStrictEqual(err1, new Error('foo1')); assertDeepAndStrictEqual(err1, new Error('foo1'));
assertNotDeepOrStrict(err1, {}, AssertionError); assertNotDeepOrStrict(err1, {}, AssertionError);
} });
// Handle NaN test('Handle NaN', () => {
assertDeepAndStrictEqual(NaN, NaN); assertDeepAndStrictEqual(NaN, NaN);
assertDeepAndStrictEqual({ a: NaN }, { a: NaN }); assertDeepAndStrictEqual({ a: NaN }, { a: NaN });
assertDeepAndStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]); assertDeepAndStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]);
});
// Handle boxed primitives test('Handle boxed primitives', () => {
{
const boxedString = new String('test'); const boxedString = new String('test');
const boxedSymbol = Object(Symbol()); const boxedSymbol = Object(Symbol());
@ -640,14 +644,14 @@ assertDeepAndStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]);
boxedSymbol.slow = true; boxedSymbol.slow = true;
assertNotDeepOrStrict(boxedSymbol, {}); assertNotDeepOrStrict(boxedSymbol, {});
assertNotDeepOrStrict(boxedSymbol, fakeBoxedSymbol); assertNotDeepOrStrict(boxedSymbol, fakeBoxedSymbol);
} });
// Minus zero test('Minus zero', () => {
assertOnlyDeepEqual(0, -0); assertOnlyDeepEqual(0, -0);
assertDeepAndStrictEqual(-0, -0); assertDeepAndStrictEqual(-0, -0);
});
// Handle symbols (enumerable only) test('Handle symbols (enumerable only)', () => {
{
const symbol1 = Symbol(); const symbol1 = Symbol();
const obj1 = { [symbol1]: 1 }; const obj1 = { [symbol1]: 1 };
const obj2 = { [symbol1]: 1 }; const obj2 = { [symbol1]: 1 };
@ -680,8 +684,9 @@ assertDeepAndStrictEqual(-0, -0);
assertOnlyDeepEqual(arr, arr2); assertOnlyDeepEqual(arr, arr2);
arr2[symbol1] = false; arr2[symbol1] = false;
assertOnlyDeepEqual(arr, arr2); assertOnlyDeepEqual(arr, arr2);
} });
test('Additional tests', () => {
assert.throws( assert.throws(
() => assert.notDeepEqual(1, true), () => assert.notDeepEqual(1, true),
{ {
@ -738,8 +743,9 @@ assert.deepEqual(true, 1);
assert.throws(() => assert.deepEqual(4, '5'), assert.throws(() => assert.deepEqual(4, '5'),
AssertionError, AssertionError,
'deepEqual( 4, \'5\')'); 'deepEqual( 4, \'5\')');
});
// Having the same number of owned properties && the same set of keys. test('Having the same number of owned properties && the same set of keys', () => {
assert.deepEqual({ a: 4 }, { a: 4 }); assert.deepEqual({ a: 4 }, { a: 4 });
assert.deepEqual({ a: 4, b: '2' }, { a: 4, b: '2' }); assert.deepEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
assert.deepEqual([4], ['4']); assert.deepEqual([4], ['4']);
@ -756,8 +762,9 @@ a2.a = 'test';
assert.throws(() => assert.deepEqual(Object.keys(a1), Object.keys(a2)), assert.throws(() => assert.deepEqual(Object.keys(a1), Object.keys(a2)),
AssertionError); AssertionError);
assertDeepAndStrictEqual(a1, a2); assertDeepAndStrictEqual(a1, a2);
});
// Having an identical prototype property. test('Having an identical prototype property', () => {
const nbRoot = { const nbRoot = {
toString() { return `${this.first} ${this.last}`; } toString() { return `${this.first} ${this.last}`; }
}; };
@ -784,8 +791,9 @@ assert.deepEqual(nb1, nb2);
nameBuilder2.prototype = Object; nameBuilder2.prototype = Object;
nb2 = new nameBuilder2('Ryan', 'Dahl'); nb2 = new nameBuilder2('Ryan', 'Dahl');
assert.deepEqual(nb1, nb2); assert.deepEqual(nb1, nb2);
});
// Primitives test('Primitives', () => {
assertNotDeepOrStrict(null, {}); assertNotDeepOrStrict(null, {});
assertNotDeepOrStrict(undefined, {}); assertNotDeepOrStrict(undefined, {});
assertNotDeepOrStrict('a', ['a']); assertNotDeepOrStrict('a', ['a']);
@ -808,7 +816,9 @@ assertNotDeepOrStrict(new String('a'), ['a']);
assertNotDeepOrStrict(new String('a'), { 0: 'a' }); assertNotDeepOrStrict(new String('a'), { 0: 'a' });
assertNotDeepOrStrict(new Number(1), {}); assertNotDeepOrStrict(new Number(1), {});
assertNotDeepOrStrict(new Boolean(true), {}); assertNotDeepOrStrict(new Boolean(true), {});
});
test('Additional tests', () => {
// Same number of keys but different key names. // Same number of keys but different key names.
assertNotDeepOrStrict({ a: 1 }, { b: 1 }); assertNotDeepOrStrict({ a: 1 }, { b: 1 });
@ -885,7 +895,14 @@ assert.throws(
{ message: `${defaultMsgStart}\ntrue !== 1\n` } { message: `${defaultMsgStart}\ntrue !== 1\n` }
); );
// Having the same number of owned properties && the same set of keys. assertDeepAndStrictEqual({ a: 4, b: '1' }, { b: '1', a: 4 });
assert.throws(
() => assert.deepStrictEqual([0, 1, 2, 'a', 'b'], [0, 1, 2, 'b', 'a']),
AssertionError);
});
test('Having the same number of owned properties && the same set of keys', () => {
assert.deepStrictEqual({ a: 4 }, { a: 4 }); assert.deepStrictEqual({ a: 4 }, { a: 4 });
assert.deepStrictEqual({ a: 4, b: '2' }, { a: 4, b: '2' }); assert.deepStrictEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
assert.throws(() => assert.deepStrictEqual([4], ['4']), assert.throws(() => assert.deepStrictEqual([4], ['4']),
@ -910,16 +927,11 @@ assert.throws(
message: `${defaultMsgStartFull}\n\n` + message: `${defaultMsgStartFull}\n\n` +
"+ [\n+ 'a'\n+ ]\n- {\n- '0': 'a'\n- }" "+ [\n+ 'a'\n+ ]\n- {\n- '0': 'a'\n- }"
}); });
});
/* eslint-enable */ /* eslint-enable */
assertDeepAndStrictEqual({ a: 4, b: '1' }, { b: '1', a: 4 }); test('Prototype check', () => {
assert.throws(
() => assert.deepStrictEqual([0, 1, 2, 'a', 'b'], [0, 1, 2, 'b', 'a']),
AssertionError);
// Prototype check.
function Constructor1(first, last) { function Constructor1(first, last) {
this.first = first; this.first = first;
this.last = last; this.last = last;
@ -939,9 +951,9 @@ Constructor2.prototype = Constructor1.prototype;
obj2 = new Constructor2('Ryan', 'Dahl'); obj2 = new Constructor2('Ryan', 'Dahl');
assertDeepAndStrictEqual(obj1, obj2); assertDeepAndStrictEqual(obj1, obj2);
});
// Check extra properties on errors. test('Check extra properties on errors', () => {
{
const a = new TypeError('foo'); const a = new TypeError('foo');
const b = new TypeError('foo'); const b = new TypeError('foo');
a.foo = 'bar'; a.foo = 'bar';
@ -973,10 +985,9 @@ assertDeepAndStrictEqual(obj1, obj2);
' }' ' }'
} }
); );
} });
// Check proxies. test('Check proxies', () => {
{
const arrProxy = new Proxy([1, 2], {}); const arrProxy = new Proxy([1, 2], {});
assert.deepStrictEqual(arrProxy, [1, 2]); assert.deepStrictEqual(arrProxy, [1, 2]);
const tmp = util.inspect.defaultOptions; const tmp = util.inspect.defaultOptions;
@ -998,12 +1009,11 @@ assertDeepAndStrictEqual(obj1, obj2);
message: "'ownKeys' on proxy: trap result did not include 'length'" message: "'ownKeys' on proxy: trap result did not include 'length'"
} }
); );
} });
// Strict equal with identical objects that are not identical test('Strict equal with identical objects that are not identical ' +
// by reference and longer than 50 elements 'by reference and longer than 50 elements', () => {
// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() }) // E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() })
{
const a = {}; const a = {};
const b = {}; const b = {};
for (let i = 0; i < 55; i++) { for (let i = 0; i < 55; i++) {
@ -1019,21 +1029,18 @@ assertDeepAndStrictEqual(obj1, obj2);
message: /\.\.\./g message: /\.\.\./g
} }
); );
} });
// Basic valueOf check. test('Basic valueOf check', () => {
{
const a = new String(1); const a = new String(1);
a.valueOf = undefined; a.valueOf = undefined;
assertNotDeepOrStrict(a, new String(1)); assertNotDeepOrStrict(a, new String(1));
} });
// Basic array out of bounds check. test('Basic array out of bounds check', () => {
{
const arr = [1, 2, 3]; const arr = [1, 2, 3];
arr[2 ** 32] = true; arr[2 ** 32] = true;
assertNotDeepOrStrict(arr, [1, 2, 3]); assertNotDeepOrStrict(arr, [1, 2, 3]);
}
assert.throws( assert.throws(
() => assert.deepStrictEqual([1, 2, 3], [1, 2]), () => assert.deepStrictEqual([1, 2, 3], [1, 2]),
@ -1048,21 +1055,20 @@ assert.throws(
' ]' ' ]'
} }
); );
});
// Verify that manipulating the `getTime()` function has no impact on the time test('Verify that manipulating the `getTime()` function has no impact on the time ' +
// verification. 'verification.', () => {
{
const a = new Date('2000'); const a = new Date('2000');
const b = new Date('2000'); const b = new Date('2000');
Object.defineProperty(a, 'getTime', { Object.defineProperty(a, 'getTime', {
value: () => 5 value: () => 5
}); });
assertDeepAndStrictEqual(a, b); assertDeepAndStrictEqual(a, b);
} });
// Verify that an array and the equivalent fake array object test('Verify that an array and the equivalent fake array object ' +
// are correctly compared 'are correctly compared', () => {
{
const a = [1, 2, 3]; const a = [1, 2, 3];
const o = { const o = {
__proto__: Array.prototype, __proto__: Array.prototype,
@ -1073,10 +1079,9 @@ assert.throws(
}; };
Object.defineProperty(o, 'length', { enumerable: false }); Object.defineProperty(o, 'length', { enumerable: false });
assertNotDeepOrStrict(o, a); assertNotDeepOrStrict(o, a);
} });
// Verify that extra keys will be tested for when using fake arrays. test('Verify that extra keys will be tested for when using fake arrays', () => {
{
const a = { const a = {
0: 1, 0: 1,
1: 1, 1: 1,
@ -1090,19 +1095,17 @@ assert.throws(
value: 2 value: 2
}); });
assertNotDeepOrStrict(a, [1, 1]); assertNotDeepOrStrict(a, [1, 1]);
} });
// Verify that changed tags will still check for the error message. test('Verify that changed tags will still check for the error message', () => {
{
const err = new Error('foo'); const err = new Error('foo');
err[Symbol.toStringTag] = 'Foobar'; err[Symbol.toStringTag] = 'Foobar';
const err2 = new Error('bar'); const err2 = new Error('bar');
err2[Symbol.toStringTag] = 'Foobar'; err2[Symbol.toStringTag] = 'Foobar';
assertNotDeepOrStrict(err, err2, AssertionError); assertNotDeepOrStrict(err, err2, AssertionError);
} });
// Check for non-native errors. test('Check for non-native errors', () => {
{
const source = new Error('abc'); const source = new Error('abc');
const err = Object.create( const err = Object.create(
Object.getPrototypeOf(source), Object.getOwnPropertyDescriptors(source)); Object.getPrototypeOf(source), Object.getOwnPropertyDescriptors(source));
@ -1113,19 +1116,17 @@ assert.throws(
err[Symbol.toStringTag] = 'Foo'; err[Symbol.toStringTag] = 'Foo';
err2[Symbol.toStringTag] = 'Foo'; err2[Symbol.toStringTag] = 'Foo';
assert.notDeepStrictEqual(err, err2); assert.notDeepStrictEqual(err, err2);
} });
// Check for Errors with cause property test('Check for Errors with cause property', () => {
{
const e1 = new Error('err', { cause: new Error('cause e1') }); const e1 = new Error('err', { cause: new Error('cause e1') });
const e2 = new Error('err', { cause: new Error('cause e2') }); const e2 = new Error('err', { cause: new Error('cause e2') });
assertNotDeepOrStrict(e1, e2, AssertionError); assertNotDeepOrStrict(e1, e2, AssertionError);
assertNotDeepOrStrict(e1, new Error('err'), AssertionError); assertNotDeepOrStrict(e1, new Error('err'), AssertionError);
assertDeepAndStrictEqual(e1, new Error('err', { cause: new Error('cause e1') })); assertDeepAndStrictEqual(e1, new Error('err', { cause: new Error('cause e1') }));
} });
// Check for AggregateError test('Check for AggregateError', () => {
{
const e1 = new Error('e1'); const e1 = new Error('e1');
const e1duplicate = new Error('e1'); const e1duplicate = new Error('e1');
const e2 = new Error('e2'); const e2 = new Error('e2');
@ -1136,10 +1137,9 @@ assert.throws(
assertNotDeepOrStrict(e1, e3, AssertionError); assertNotDeepOrStrict(e1, e3, AssertionError);
assertNotDeepOrStrict(e3, e4, AssertionError); assertNotDeepOrStrict(e3, e4, AssertionError);
assertDeepAndStrictEqual(e3, e3duplicate); assertDeepAndStrictEqual(e3, e3duplicate);
} });
// Verify that `valueOf` is not called for boxed primitives. test('Verify that `valueOf` is not called for boxed primitives', () => {
{
const a = new Number(5); const a = new Number(5);
const b = new Number(5); const b = new Number(5);
Object.defineProperty(a, 'valueOf', { Object.defineProperty(a, 'valueOf', {
@ -1149,10 +1149,9 @@ assert.throws(
value: () => { throw new Error('failed'); } value: () => { throw new Error('failed'); }
}); });
assertDeepAndStrictEqual(a, b); assertDeepAndStrictEqual(a, b);
} });
// Check getters. test('Check getters', () => {
{
const a = { const a = {
get a() { return 5; } get a() { return 5; }
}; };
@ -1170,10 +1169,9 @@ assert.throws(
// The descriptor is not compared. // The descriptor is not compared.
assertDeepAndStrictEqual(a, { a: 5 }); assertDeepAndStrictEqual(a, { a: 5 });
} });
// Verify object types being identical on both sides. test('Verify object types being identical on both sides', () => {
{
let a = Buffer.from('test'); let a = Buffer.from('test');
let b = Object.create( let b = Object.create(
Object.getPrototypeOf(a), Object.getPrototypeOf(a),
@ -1236,21 +1234,19 @@ assert.throws(
}); });
Object.setPrototypeOf(b, null); Object.setPrototypeOf(b, null);
assertNotDeepOrStrict(a, b, assert.AssertionError); assertNotDeepOrStrict(a, b, assert.AssertionError);
} });
{ test('Verify commutativity', () => {
// Verify commutativity
// Regression test for https://github.com/nodejs/node/issues/37710 // Regression test for https://github.com/nodejs/node/issues/37710
const a = { x: 1 }; const a = { x: 1 };
const b = { y: 1 }; const b = { y: 1 };
Object.defineProperty(b, 'x', { value: 1 }); Object.defineProperty(b, 'x', { value: 1 });
assertNotDeepOrStrict(a, b); assertNotDeepOrStrict(a, b);
} });
// eslint-disable-next-line node-core/crypto-check test('Crypto', { skip: !hasCrypto }, async () => {
if (common.hasCrypto) { const crypto = require('crypto'); // eslint-disable-line node-core/crypto-check
const crypto = require('crypto');
const { subtle } = globalThis.crypto; const { subtle } = globalThis.crypto;
{ {
@ -1267,52 +1263,68 @@ if (common.hasCrypto) {
assertDeepAndStrictEqual(a, b); assertDeepAndStrictEqual(a, b);
} }
(async () => {
{ {
const a = await subtle.importKey('raw', Buffer.alloc(1, 0), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); const a = await subtle.importKey('raw', Buffer.alloc(1, 0),
const b = await subtle.importKey('raw', Buffer.alloc(1, 1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); { name: 'HMAC', hash: 'SHA-256' },
true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1, 1),
{ name: 'HMAC', hash: 'SHA-256' },
true, ['sign']);
assertNotDeepOrStrict(a, b); assertNotDeepOrStrict(a, b);
} }
{ {
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); const a = await subtle.importKey('raw', Buffer.alloc(1),
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']); { name: 'HMAC', hash: 'SHA-256' },
true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1),
{ name: 'HMAC', hash: 'SHA-256' },
false, ['sign']);
assertNotDeepOrStrict(a, b); assertNotDeepOrStrict(a, b);
} }
{ {
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); const a = await subtle.importKey('raw', Buffer.alloc(1),
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-384' }, true, ['sign']); { name: 'HMAC', hash: 'SHA-256' },
true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1),
{ name: 'HMAC', hash: 'SHA-384' },
true, ['sign']);
assertNotDeepOrStrict(a, b); assertNotDeepOrStrict(a, b);
} }
{ {
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); const a = await subtle.importKey('raw', Buffer.alloc(1),
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['verify']); { name: 'HMAC', hash: 'SHA-256' },
true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1),
{ name: 'HMAC', hash: 'SHA-256' },
true, ['verify']);
assertNotDeepOrStrict(a, b); assertNotDeepOrStrict(a, b);
} }
{ {
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); const a = await subtle.importKey('raw', Buffer.alloc(1),
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); { name: 'HMAC', hash: 'SHA-256' },
true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1),
{ name: 'HMAC', hash: 'SHA-256' },
true, ['sign']);
assertDeepAndStrictEqual(a, b); assertDeepAndStrictEqual(a, b);
} }
})().then(common.mustCall()); });
}
// Comparing two identical WeakMap instances test('Comparing two identical WeakMap instances', () => {
{
const weakMap = new WeakMap(); const weakMap = new WeakMap();
assertDeepAndStrictEqual(weakMap, weakMap); assertDeepAndStrictEqual(weakMap, weakMap);
} });
// Comparing two different WeakMap instances test('Comparing two different WeakMap instances', () => {
{
const weakMap1 = new WeakMap(); const weakMap1 = new WeakMap();
const objA = {}; const objA = {};
weakMap1.set(objA, 'ok'); weakMap1.set(objA, 'ok');
@ -1322,17 +1334,15 @@ if (common.hasCrypto) {
weakMap2.set(objB, 'ok'); weakMap2.set(objB, 'ok');
assertNotDeepOrStrict(weakMap1, weakMap2); assertNotDeepOrStrict(weakMap1, weakMap2);
} });
// Comparing two identical WeakSet instances test('Comparing two identical WeakSet instances', () => {
{
const weakSet = new WeakSet(); const weakSet = new WeakSet();
assertDeepAndStrictEqual(weakSet, weakSet); assertDeepAndStrictEqual(weakSet, weakSet);
} });
// Comparing two different WeakSet instances test('Comparing two different WeakSet instances', () => {
{
const weakSet1 = new WeakSet(); const weakSet1 = new WeakSet();
const weakSet2 = new WeakSet(); const weakSet2 = new WeakSet();
assertNotDeepOrStrict(weakSet1, weakSet2); assertNotDeepOrStrict(weakSet1, weakSet2);
} });

View File

@ -1,16 +1,18 @@
// Flags: --no-warnings
'use strict'; 'use strict';
const common = require('../common'); const { expectWarning } = require('../common');
const assert = require('assert'); const assert = require('assert');
const { test } = require('node:test');
common.expectWarning( expectWarning(
'DeprecationWarning', 'DeprecationWarning',
'assert.fail() with more than one argument is deprecated. ' + 'assert.fail() with more than one argument is deprecated. ' +
'Please use assert.strictEqual() instead or only pass a message.', 'Please use assert.strictEqual() instead or only pass a message.',
'DEP0094' 'DEP0094'
); );
// Two args only, operator defaults to '!=' test('Two args only, operator defaults to "!="', () => {
assert.throws(() => { assert.throws(() => {
assert.fail('first', 'second'); assert.fail('first', 'second');
}, { }, {
@ -22,8 +24,9 @@ assert.throws(() => {
expected: 'second', expected: 'second',
generatedMessage: true generatedMessage: true
}); });
});
// Three args test('Three args', () => {
assert.throws(() => { assert.throws(() => {
assert.fail('ignored', 'ignored', 'another custom message'); assert.fail('ignored', 'ignored', 'another custom message');
}, { }, {
@ -35,16 +38,18 @@ assert.throws(() => {
expected: 'ignored', expected: 'ignored',
generatedMessage: false generatedMessage: false
}); });
});
// Three args with custom Error test('Three args with custom Error', () => {
assert.throws(() => { assert.throws(() => {
assert.fail(typeof 1, 'object', new TypeError('another custom message')); assert.fail(typeof 1, 'object', new TypeError('another custom message'));
}, { }, {
name: 'TypeError', name: 'TypeError',
message: 'another custom message' message: 'another custom message'
}); });
});
// No third arg (but a fourth arg) test('No third arg (but a fourth arg)', () => {
assert.throws(() => { assert.throws(() => {
assert.fail('first', 'second', undefined, 'operator'); assert.fail('first', 'second', undefined, 'operator');
}, { }, {
@ -55,9 +60,11 @@ assert.throws(() => {
actual: 'first', actual: 'first',
expected: 'second' expected: 'second'
}); });
});
// The stackFrameFunction should exclude the foo frame test('The stackFrameFunction should exclude the foo frame', () => {
assert.throws( assert.throws(
function foo() { assert.fail('first', 'second', 'message', '!==', foo); }, function foo() { assert.fail('first', 'second', 'message', '!==', foo); },
(err) => !/^\s*at\sfoo\b/m.test(err.stack) (err) => !/^\s*at\sfoo\b/m.test(err.stack)
); );
});

View File

@ -1,9 +1,10 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const { test } = require('node:test');
// No args test('No args', () => {
assert.throws( assert.throws(
() => { assert.fail(); }, () => { assert.fail(); },
{ {
@ -17,8 +18,9 @@ assert.throws(
stack: /Failed/ stack: /Failed/
} }
); );
});
// One arg = message test('One arg = message', () => {
assert.throws(() => { assert.throws(() => {
assert.fail('custom message'); assert.fail('custom message');
}, { }, {
@ -30,15 +32,19 @@ assert.throws(() => {
expected: undefined, expected: undefined,
generatedMessage: false generatedMessage: false
}); });
});
// One arg = Error test('One arg = Error', () => {
assert.throws(() => { assert.throws(() => {
assert.fail(new TypeError('custom message')); assert.fail(new TypeError('custom message'));
}, { }, {
name: 'TypeError', name: 'TypeError',
message: 'custom message' message: 'custom message'
}); });
});
Object.prototype.get = common.mustNotCall(); test('Object prototype get', () => {
Object.prototype.get = () => { throw new Error('failed'); };
assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' }); assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' });
delete Object.prototype.get; delete Object.prototype.get;
});

View File

@ -4,8 +4,10 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const { test } = require('node:test');
const { path } = require('../common/fixtures'); const { path } = require('../common/fixtures');
test('Verify that asserting in the very first line produces the expected result', () => {
assert.throws( assert.throws(
() => require(path('assert-first-line')), () => require(path('assert-first-line')),
{ {
@ -21,3 +23,4 @@ assert.throws(
message: "The expression evaluated to a falsy value:\n\n assert.ok('')\n" message: "The expression evaluated to a falsy value:\n\n assert.ok('')\n"
} }
); );
});

View File

@ -2,9 +2,9 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const { test } = require('node:test');
// Test that assert.ifError has the correct stack trace of both stacks. test('Test that assert.ifError has the correct stack trace of both stacks', () => {
let err; let err;
// Create some random error frames. // Create some random error frames.
(function a() { (function a() {
@ -38,7 +38,9 @@ const stack = err.stack;
})(); })();
})(); })();
})(); })();
});
test('General ifError tests', () => {
assert.throws( assert.throws(
() => { () => {
const error = new Error(); const error = new Error();
@ -78,14 +80,15 @@ assert.throws(
message: 'ifError got unwanted exception: false' message: 'ifError got unwanted exception: false'
} }
); );
});
// Should not throw. test('Should not throw', () => {
assert.ifError(null); assert.ifError(null);
assert.ifError(); assert.ifError();
assert.ifError(undefined); assert.ifError(undefined);
});
// https://github.com/nodejs/node-v0.x-archive/issues/2893 test('https://github.com/nodejs/node-v0.x-archive/issues/2893', () => {
{
let threw = false; let threw = false;
try { try {
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
@ -98,4 +101,4 @@ assert.ifError(undefined);
assert(!e.stack.includes('throws'), e); assert(!e.stack.includes('throws'), e);
} }
assert(threw); assert(threw);
} });