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'; '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
{
const date = new Date('2016');
function FakeDate() {}
FakeDate.prototype = Date.prototype;
const fake = new FakeDate();
// See https://github.com/nodejs/node/issues/10258 assert.notDeepEqual(date, fake);
{ assert.notDeepEqual(fake, date);
const date = new Date('2016');
function FakeDate() {}
FakeDate.prototype = Date.prototype;
const fake = new FakeDate();
assert.notDeepEqual(date, fake); // For deepStrictEqual we check the runtime type,
assert.notDeepEqual(fake, date); // then reveal the fakeness of the fake date
assert.throws(
// For deepStrictEqual we check the runtime type, () => assert.deepStrictEqual(date, fake),
// then reveal the fakeness of the fake date {
assert.throws( message: 'Expected values to be strictly deep-equal:\n' +
() => assert.deepStrictEqual(date, fake), '+ actual - expected\n\n+ 2016-01-01T00:00:00.000Z\n- Date {}'
{ }
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),
); {
assert.throws( message: 'Expected values to be strictly deep-equal:\n' +
() => assert.deepStrictEqual(fake, date), '+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z'
{ }
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];
} }
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 { // At the moment global has its own type tag
const fakeProcess = {}; const fakeGlobal = {};
Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process)); Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
for (const prop of Object.keys(process)) { for (const prop of Object.keys(global)) {
fakeProcess[prop] = process[prop]; 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 { // At the moment process has its own type tag
assert.throws(() => assert.deepStrictEqual(fakeProcess, process), const fakeProcess = {};
assert.AssertionError); 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 */ /* eslint-enable */

File diff suppressed because it is too large Load Diff

View File

@ -1,63 +1,70 @@
// 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\'',
operator: '!=', operator: '!=',
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',
operator: 'fail', operator: 'fail',
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,44 +1,50 @@
'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',
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', code: 'ERR_ASSERTION',
name: 'AssertionError', name: 'AssertionError',
message: 'Failed', message: 'custom message',
operator: 'fail', operator: 'fail',
actual: undefined, actual: undefined,
expected: undefined, expected: undefined,
generatedMessage: true, generatedMessage: false
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
}); });
// 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', () => {
() => require(path('assert-first-line')), assert.throws(
{ () => require(path('assert-first-line')),
name: 'AssertionError', {
message: "The expression evaluated to a falsy value:\n\n ässört.ok('')\n" name: 'AssertionError',
} 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,90 +2,93 @@
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;
try { try {
assert.ifError(err); assert.ifError(err);
} catch (e) { } catch (e) {
assert.strictEqual(e.message, assert.strictEqual(e.message,
'ifError got unwanted exception: test error'); 'ifError got unwanted exception: test error');
assert.strictEqual(err.message, msg); assert.strictEqual(err.message, msg);
assert.strictEqual(e.actual, err); assert.strictEqual(e.actual, err);
assert.strictEqual(e.actual.stack, stack); assert.strictEqual(e.actual.stack, stack);
assert.strictEqual(e.expected, null); assert.strictEqual(e.expected, null);
assert.strictEqual(e.operator, 'ifError'); assert.strictEqual(e.operator, 'ifError');
threw = true; threw = true;
} }
assert(threw); assert(threw);
})();
})(); })();
})(); })();
})(); });
assert.throws( test('General ifError tests', () => {
() => { assert.throws(
const error = new Error(); () => {
error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.'; const error = new Error();
assert.ifError(error); error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
}, assert.ifError(error);
(error) => { },
assert(!error.stack.includes('Yes!')); (error) => {
return true; assert(!error.stack.includes('Yes!'));
} 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);
} });