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,70 +1,68 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto) {
common.skip('missing crypto');
}
const { hasCrypto } = require('../common');
const { test } = require('node:test');
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
// message comparisons. This should only be an issue when process.stdout
// is a TTY.
if (process.stdout.isTTY)
process.env.NODE_DISABLE_COLORS = '1';
// Turn off no-restricted-properties because we are testing deepEqual!
/* eslint-disable no-restricted-properties */
test('', { skip: !hasCrypto }, () => {
// See https://github.com/nodejs/node/issues/10258
{
const date = new Date('2016');
function FakeDate() {}
FakeDate.prototype = Date.prototype;
const fake = new FakeDate();
// See https://github.com/nodejs/node/issues/10258
{
const date = new Date('2016');
function FakeDate() {}
FakeDate.prototype = Date.prototype;
const fake = new FakeDate();
assert.notDeepEqual(date, fake);
assert.notDeepEqual(fake, date);
assert.notDeepEqual(date, fake);
assert.notDeepEqual(fake, date);
// For deepStrictEqual we check the runtime type,
// then reveal the fakeness of the fake date
assert.throws(
() => assert.deepStrictEqual(date, fake),
{
message: 'Expected values to be strictly deep-equal:\n' +
'+ actual - expected\n\n+ 2016-01-01T00:00:00.000Z\n- Date {}'
}
);
assert.throws(
() => assert.deepStrictEqual(fake, date),
{
message: 'Expected values to be strictly deep-equal:\n' +
'+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z'
}
);
}
{ // At the moment global has its own type tag
const fakeGlobal = {};
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
for (const prop of Object.keys(global)) {
fakeGlobal[prop] = global[prop];
// For deepStrictEqual we check the runtime type,
// then reveal the fakeness of the fake date
assert.throws(
() => assert.deepStrictEqual(date, fake),
{
message: 'Expected values to be strictly deep-equal:\n' +
'+ actual - expected\n\n+ 2016-01-01T00:00:00.000Z\n- Date {}'
}
);
assert.throws(
() => assert.deepStrictEqual(fake, date),
{
message: 'Expected values to be strictly deep-equal:\n' +
'+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z'
}
);
}
assert.notDeepEqual(fakeGlobal, global);
// Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
assert.AssertionError);
}
{ // At the moment process has its own type tag
const fakeProcess = {};
Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process));
for (const prop of Object.keys(process)) {
fakeProcess[prop] = process[prop];
{ // At the moment global has its own type tag
const fakeGlobal = {};
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
for (const prop of Object.keys(global)) {
fakeGlobal[prop] = global[prop];
}
assert.notDeepEqual(fakeGlobal, global);
// Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
assert.AssertionError);
}
assert.notDeepEqual(fakeProcess, process);
// Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
assert.AssertionError);
}
{ // At the moment process has its own type tag
const fakeProcess = {};
Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process));
for (const prop of Object.keys(process)) {
fakeProcess[prop] = process[prop];
}
assert.notDeepEqual(fakeProcess, process);
// Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
assert.AssertionError);
}
});
/* eslint-enable */

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -1,44 +1,50 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const { test } = require('node:test');
// No args
assert.throws(
() => { assert.fail(); },
{
test('No args', () => {
assert.throws(
() => { assert.fail(); },
{
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'Failed',
operator: 'fail',
actual: undefined,
expected: undefined,
generatedMessage: true,
stack: /Failed/
}
);
});
test('One arg = message', () => {
assert.throws(() => {
assert.fail('custom message');
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'Failed',
message: 'custom message',
operator: 'fail',
actual: undefined,
expected: undefined,
generatedMessage: true,
stack: /Failed/
}
);
// One arg = message
assert.throws(() => {
assert.fail('custom message');
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'custom message',
operator: 'fail',
actual: undefined,
expected: undefined,
generatedMessage: false
generatedMessage: false
});
});
// One arg = Error
assert.throws(() => {
assert.fail(new TypeError('custom message'));
}, {
name: 'TypeError',
message: 'custom message'
test('One arg = Error', () => {
assert.throws(() => {
assert.fail(new TypeError('custom message'));
}, {
name: 'TypeError',
message: 'custom message'
});
});
Object.prototype.get = common.mustNotCall();
assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' });
delete Object.prototype.get;
test('Object prototype get', () => {
Object.prototype.get = () => { throw new Error('failed'); };
assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' });
delete Object.prototype.get;
});

View File

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

View File

@ -2,90 +2,93 @@
require('../common');
const assert = require('assert');
const { test } = require('node:test');
// Test that assert.ifError has the correct stack trace of both stacks.
let err;
// Create some random error frames.
(function a() {
(function b() {
(function c() {
err = new Error('test error');
test('Test that assert.ifError has the correct stack trace of both stacks', () => {
let err;
// Create some random error frames.
(function a() {
(function b() {
(function c() {
err = new Error('test error');
})();
})();
})();
})();
const msg = err.message;
const stack = err.stack;
const msg = err.message;
const stack = err.stack;
(function x() {
(function y() {
(function z() {
let threw = false;
try {
assert.ifError(err);
} catch (e) {
assert.strictEqual(e.message,
'ifError got unwanted exception: test error');
assert.strictEqual(err.message, msg);
assert.strictEqual(e.actual, err);
assert.strictEqual(e.actual.stack, stack);
assert.strictEqual(e.expected, null);
assert.strictEqual(e.operator, 'ifError');
threw = true;
}
assert(threw);
(function x() {
(function y() {
(function z() {
let threw = false;
try {
assert.ifError(err);
} catch (e) {
assert.strictEqual(e.message,
'ifError got unwanted exception: test error');
assert.strictEqual(err.message, msg);
assert.strictEqual(e.actual, err);
assert.strictEqual(e.actual.stack, stack);
assert.strictEqual(e.expected, null);
assert.strictEqual(e.operator, 'ifError');
threw = true;
}
assert(threw);
})();
})();
})();
})();
});
assert.throws(
() => {
const error = new Error();
error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
assert.ifError(error);
},
(error) => {
assert(!error.stack.includes('Yes!'));
return true;
}
);
test('General ifError tests', () => {
assert.throws(
() => {
const error = new Error();
error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
assert.ifError(error);
},
(error) => {
assert(!error.stack.includes('Yes!'));
return true;
}
);
assert.throws(
() => assert.ifError(new TypeError()),
{
message: 'ifError got unwanted exception: TypeError'
}
);
assert.throws(
() => assert.ifError(new TypeError()),
{
message: 'ifError got unwanted exception: TypeError'
}
);
assert.throws(
() => assert.ifError({ stack: false }),
{
message: 'ifError got unwanted exception: { stack: false }'
}
);
assert.throws(
() => assert.ifError({ stack: false }),
{
message: 'ifError got unwanted exception: { stack: false }'
}
);
assert.throws(
() => assert.ifError({ constructor: null, message: '' }),
{
message: 'ifError got unwanted exception: '
}
);
assert.throws(
() => assert.ifError({ constructor: null, message: '' }),
{
message: 'ifError got unwanted exception: '
}
);
assert.throws(
() => { assert.ifError(false); },
{
message: 'ifError got unwanted exception: false'
}
);
assert.throws(
() => { assert.ifError(false); },
{
message: 'ifError got unwanted exception: false'
}
);
});
// Should not throw.
assert.ifError(null);
assert.ifError();
assert.ifError(undefined);
test('Should not throw', () => {
assert.ifError(null);
assert.ifError();
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;
try {
// eslint-disable-next-line no-restricted-syntax
@ -98,4 +101,4 @@ assert.ifError(undefined);
assert(!e.stack.includes('throws'), e);
}
assert(threw);
}
});