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,23 +1,20 @@
'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');
function FakeDate() {} function FakeDate() {}
FakeDate.prototype = Date.prototype; FakeDate.prototype = Date.prototype;
@ -42,9 +39,9 @@ if (process.stdout.isTTY)
'+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z' '+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z'
} }
); );
} }
{ // At the moment global has its own type tag { // At the moment global has its own type tag
const fakeGlobal = {}; const fakeGlobal = {};
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global)); Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
for (const prop of Object.keys(global)) { for (const prop of Object.keys(global)) {
@ -54,9 +51,9 @@ if (process.stdout.isTTY)
// Message will be truncated anyway, don't validate // Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global), assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
assert.AssertionError); assert.AssertionError);
} }
{ // At the moment process has its own type tag { // At the moment process has its own type tag
const fakeProcess = {}; const fakeProcess = {};
Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process)); Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process));
for (const prop of Object.keys(process)) { for (const prop of Object.keys(process)) {
@ -66,5 +63,6 @@ if (process.stdout.isTTY)
// Message will be truncated anyway, don't validate // Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeProcess, process), assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
assert.AssertionError); assert.AssertionError);
} }
});
/* eslint-enable */ /* eslint-enable */

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,21 @@
// 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');
}, { }, {
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
name: 'AssertionError', name: 'AssertionError',
message: '\'first\' != \'second\'', message: '\'first\' != \'second\'',
@ -21,12 +23,13 @@ assert.throws(() => {
actual: 'first', actual: 'first',
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');
}, { }, {
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
name: 'AssertionError', name: 'AssertionError',
message: 'another custom message', message: 'another custom message',
@ -34,30 +37,34 @@ assert.throws(() => {
actual: 'ignored', actual: 'ignored',
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');
}, { }, {
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
name: 'AssertionError', name: 'AssertionError',
message: '\'first\' operator \'second\'', message: '\'first\' operator \'second\'',
operator: 'operator', operator: 'operator',
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,10 +1,11 @@
'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(); },
{ {
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
@ -16,12 +17,13 @@ assert.throws(
generatedMessage: true, generatedMessage: true,
stack: /Failed/ stack: /Failed/
} }
); );
});
// One arg = message test('One arg = message', () => {
assert.throws(() => { assert.throws(() => {
assert.fail('custom message'); assert.fail('custom message');
}, { }, {
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
name: 'AssertionError', name: 'AssertionError',
message: 'custom message', message: 'custom message',
@ -29,16 +31,20 @@ assert.throws(() => {
actual: undefined, actual: undefined,
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', () => {
assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' }); Object.prototype.get = () => { throw new Error('failed'); };
delete Object.prototype.get; assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' });
delete Object.prototype.get;
});

View File

@ -4,20 +4,23 @@
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');
assert.throws( test('Verify that asserting in the very first line produces the expected result', () => {
assert.throws(
() => require(path('assert-first-line')), () => require(path('assert-first-line')),
{ {
name: 'AssertionError', name: 'AssertionError',
message: "The expression evaluated to a falsy value:\n\n ässört.ok('')\n" message: "The expression evaluated to a falsy value:\n\n ässört.ok('')\n"
} }
); );
assert.throws( assert.throws(
() => require(path('assert-long-line')), () => require(path('assert-long-line')),
{ {
name: 'AssertionError', name: 'AssertionError',
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,23 +2,23 @@
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() {
(function b() { (function b() {
(function c() { (function c() {
err = new Error('test error'); err = new Error('test error');
})(); })();
})(); })();
})(); })();
const msg = err.message; const msg = err.message;
const stack = err.stack; const stack = err.stack;
(function x() { (function x() {
(function y() { (function y() {
(function z() { (function z() {
let threw = false; let threw = false;
@ -37,9 +37,11 @@ const stack = err.stack;
assert(threw); assert(threw);
})(); })();
})(); })();
})(); })();
});
assert.throws( test('General ifError tests', () => {
assert.throws(
() => { () => {
const error = new Error(); const error = new Error();
error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.'; error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
@ -49,43 +51,44 @@ assert.throws(
assert(!error.stack.includes('Yes!')); assert(!error.stack.includes('Yes!'));
return true; return true;
} }
); );
assert.throws( assert.throws(
() => assert.ifError(new TypeError()), () => assert.ifError(new TypeError()),
{ {
message: 'ifError got unwanted exception: TypeError' message: 'ifError got unwanted exception: TypeError'
} }
); );
assert.throws( assert.throws(
() => assert.ifError({ stack: false }), () => assert.ifError({ stack: false }),
{ {
message: 'ifError got unwanted exception: { stack: false }' message: 'ifError got unwanted exception: { stack: false }'
} }
); );
assert.throws( assert.throws(
() => assert.ifError({ constructor: null, message: '' }), () => assert.ifError({ constructor: null, message: '' }),
{ {
message: 'ifError got unwanted exception: ' message: 'ifError got unwanted exception: '
} }
); );
assert.throws( assert.throws(
() => { assert.ifError(false); }, () => { assert.ifError(false); },
{ {
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);
} });