Skip to content

Commit 66a87d1

Browse files
jasnellmarco-ippolito
authored andcommittedNov 17, 2024
test: update multiple assert tests to use node:test
PR-URL: #54585 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>

6 files changed

+992
-961
lines changed
 

Diff for: ‎test/parallel/test-assert-checktag.js

+51-53
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,68 @@
11
'use strict';
2-
const common = require('../common');
3-
4-
if (!common.hasCrypto) {
5-
common.skip('missing crypto');
6-
}
7-
2+
const { hasCrypto } = require('../common');
3+
const { test } = require('node:test');
84
const assert = require('assert');
95

6+
// Turn off no-restricted-properties because we are testing deepEqual!
7+
/* eslint-disable no-restricted-properties */
8+
109
// Disable colored output to prevent color codes from breaking assertion
1110
// message comparisons. This should only be an issue when process.stdout
1211
// is a TTY.
1312
if (process.stdout.isTTY)
1413
process.env.NODE_DISABLE_COLORS = '1';
1514

16-
// Turn off no-restricted-properties because we are testing deepEqual!
17-
/* eslint-disable no-restricted-properties */
15+
test('', { skip: !hasCrypto }, () => {
16+
// See https://github.com/nodejs/node/issues/10258
17+
{
18+
const date = new Date('2016');
19+
function FakeDate() {}
20+
FakeDate.prototype = Date.prototype;
21+
const fake = new FakeDate();
1822

19-
// See https://github.com/nodejs/node/issues/10258
20-
{
21-
const date = new Date('2016');
22-
function FakeDate() {}
23-
FakeDate.prototype = Date.prototype;
24-
const fake = new FakeDate();
23+
assert.notDeepEqual(date, fake);
24+
assert.notDeepEqual(fake, date);
2525

26-
assert.notDeepEqual(date, fake);
27-
assert.notDeepEqual(fake, date);
26+
// For deepStrictEqual we check the runtime type,
27+
// then reveal the fakeness of the fake date
28+
assert.throws(
29+
() => assert.deepStrictEqual(date, fake),
30+
{
31+
message: 'Expected values to be strictly deep-equal:\n' +
32+
'+ actual - expected\n\n+ 2016-01-01T00:00:00.000Z\n- Date {}'
33+
}
34+
);
35+
assert.throws(
36+
() => assert.deepStrictEqual(fake, date),
37+
{
38+
message: 'Expected values to be strictly deep-equal:\n' +
39+
'+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z'
40+
}
41+
);
42+
}
2843

29-
// For deepStrictEqual we check the runtime type,
30-
// then reveal the fakeness of the fake date
31-
assert.throws(
32-
() => assert.deepStrictEqual(date, fake),
33-
{
34-
message: 'Expected values to be strictly deep-equal:\n' +
35-
'+ actual - expected\n\n+ 2016-01-01T00:00:00.000Z\n- Date {}'
36-
}
37-
);
38-
assert.throws(
39-
() => assert.deepStrictEqual(fake, date),
40-
{
41-
message: 'Expected values to be strictly deep-equal:\n' +
42-
'+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z'
44+
{ // At the moment global has its own type tag
45+
const fakeGlobal = {};
46+
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
47+
for (const prop of Object.keys(global)) {
48+
fakeGlobal[prop] = global[prop];
4349
}
44-
);
45-
}
46-
47-
{ // At the moment global has its own type tag
48-
const fakeGlobal = {};
49-
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
50-
for (const prop of Object.keys(global)) {
51-
fakeGlobal[prop] = global[prop];
50+
assert.notDeepEqual(fakeGlobal, global);
51+
// Message will be truncated anyway, don't validate
52+
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
53+
assert.AssertionError);
5254
}
53-
assert.notDeepEqual(fakeGlobal, global);
54-
// Message will be truncated anyway, don't validate
55-
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
56-
assert.AssertionError);
57-
}
5855

59-
{ // At the moment process has its own type tag
60-
const fakeProcess = {};
61-
Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process));
62-
for (const prop of Object.keys(process)) {
63-
fakeProcess[prop] = process[prop];
56+
{ // At the moment process has its own type tag
57+
const fakeProcess = {};
58+
Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process));
59+
for (const prop of Object.keys(process)) {
60+
fakeProcess[prop] = process[prop];
61+
}
62+
assert.notDeepEqual(fakeProcess, process);
63+
// Message will be truncated anyway, don't validate
64+
assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
65+
assert.AssertionError);
6466
}
65-
assert.notDeepEqual(fakeProcess, process);
66-
// Message will be truncated anyway, don't validate
67-
assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
68-
assert.AssertionError);
69-
}
67+
});
7068
/* eslint-enable */

Diff for: ‎test/parallel/test-assert-deep.js

+761-747
Large diffs are not rendered by default.

Diff for: ‎test/parallel/test-assert-fail-deprecation.js

+52-45
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,70 @@
1+
// Flags: --no-warnings
12
'use strict';
23

3-
const common = require('../common');
4+
const { expectWarning } = require('../common');
45
const assert = require('assert');
6+
const { test } = require('node:test');
57

6-
common.expectWarning(
8+
expectWarning(
79
'DeprecationWarning',
810
'assert.fail() with more than one argument is deprecated. ' +
911
'Please use assert.strictEqual() instead or only pass a message.',
1012
'DEP0094'
1113
);
1214

13-
// Two args only, operator defaults to '!='
14-
assert.throws(() => {
15-
assert.fail('first', 'second');
16-
}, {
17-
code: 'ERR_ASSERTION',
18-
name: 'AssertionError',
19-
message: '\'first\' != \'second\'',
20-
operator: '!=',
21-
actual: 'first',
22-
expected: 'second',
23-
generatedMessage: true
15+
test('Two args only, operator defaults to "!="', () => {
16+
assert.throws(() => {
17+
assert.fail('first', 'second');
18+
}, {
19+
code: 'ERR_ASSERTION',
20+
name: 'AssertionError',
21+
message: '\'first\' != \'second\'',
22+
operator: '!=',
23+
actual: 'first',
24+
expected: 'second',
25+
generatedMessage: true
26+
});
2427
});
2528

26-
// Three args
27-
assert.throws(() => {
28-
assert.fail('ignored', 'ignored', 'another custom message');
29-
}, {
30-
code: 'ERR_ASSERTION',
31-
name: 'AssertionError',
32-
message: 'another custom message',
33-
operator: 'fail',
34-
actual: 'ignored',
35-
expected: 'ignored',
36-
generatedMessage: false
29+
test('Three args', () => {
30+
assert.throws(() => {
31+
assert.fail('ignored', 'ignored', 'another custom message');
32+
}, {
33+
code: 'ERR_ASSERTION',
34+
name: 'AssertionError',
35+
message: 'another custom message',
36+
operator: 'fail',
37+
actual: 'ignored',
38+
expected: 'ignored',
39+
generatedMessage: false
40+
});
3741
});
3842

39-
// Three args with custom Error
40-
assert.throws(() => {
41-
assert.fail(typeof 1, 'object', new TypeError('another custom message'));
42-
}, {
43-
name: 'TypeError',
44-
message: 'another custom message'
43+
test('Three args with custom Error', () => {
44+
assert.throws(() => {
45+
assert.fail(typeof 1, 'object', new TypeError('another custom message'));
46+
}, {
47+
name: 'TypeError',
48+
message: 'another custom message'
49+
});
4550
});
4651

47-
// No third arg (but a fourth arg)
48-
assert.throws(() => {
49-
assert.fail('first', 'second', undefined, 'operator');
50-
}, {
51-
code: 'ERR_ASSERTION',
52-
name: 'AssertionError',
53-
message: '\'first\' operator \'second\'',
54-
operator: 'operator',
55-
actual: 'first',
56-
expected: 'second'
52+
test('No third arg (but a fourth arg)', () => {
53+
assert.throws(() => {
54+
assert.fail('first', 'second', undefined, 'operator');
55+
}, {
56+
code: 'ERR_ASSERTION',
57+
name: 'AssertionError',
58+
message: '\'first\' operator \'second\'',
59+
operator: 'operator',
60+
actual: 'first',
61+
expected: 'second'
62+
});
5763
});
5864

59-
// The stackFrameFunction should exclude the foo frame
60-
assert.throws(
61-
function foo() { assert.fail('first', 'second', 'message', '!==', foo); },
62-
(err) => !/^\s*at\sfoo\b/m.test(err.stack)
63-
);
65+
test('The stackFrameFunction should exclude the foo frame', () => {
66+
assert.throws(
67+
function foo() { assert.fail('first', 'second', 'message', '!==', foo); },
68+
(err) => !/^\s*at\sfoo\b/m.test(err.stack)
69+
);
70+
});

Diff for: ‎test/parallel/test-assert-fail.js

+37-31
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
5+
const { test } = require('node:test');
56

6-
// No args
7-
assert.throws(
8-
() => { assert.fail(); },
9-
{
7+
test('No args', () => {
8+
assert.throws(
9+
() => { assert.fail(); },
10+
{
11+
code: 'ERR_ASSERTION',
12+
name: 'AssertionError',
13+
message: 'Failed',
14+
operator: 'fail',
15+
actual: undefined,
16+
expected: undefined,
17+
generatedMessage: true,
18+
stack: /Failed/
19+
}
20+
);
21+
});
22+
23+
test('One arg = message', () => {
24+
assert.throws(() => {
25+
assert.fail('custom message');
26+
}, {
1027
code: 'ERR_ASSERTION',
1128
name: 'AssertionError',
12-
message: 'Failed',
29+
message: 'custom message',
1330
operator: 'fail',
1431
actual: undefined,
1532
expected: undefined,
16-
generatedMessage: true,
17-
stack: /Failed/
18-
}
19-
);
20-
21-
// One arg = message
22-
assert.throws(() => {
23-
assert.fail('custom message');
24-
}, {
25-
code: 'ERR_ASSERTION',
26-
name: 'AssertionError',
27-
message: 'custom message',
28-
operator: 'fail',
29-
actual: undefined,
30-
expected: undefined,
31-
generatedMessage: false
33+
generatedMessage: false
34+
});
3235
});
3336

34-
// One arg = Error
35-
assert.throws(() => {
36-
assert.fail(new TypeError('custom message'));
37-
}, {
38-
name: 'TypeError',
39-
message: 'custom message'
37+
test('One arg = Error', () => {
38+
assert.throws(() => {
39+
assert.fail(new TypeError('custom message'));
40+
}, {
41+
name: 'TypeError',
42+
message: 'custom message'
43+
});
4044
});
4145

42-
Object.prototype.get = common.mustNotCall();
43-
assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' });
44-
delete Object.prototype.get;
46+
test('Object prototype get', () => {
47+
Object.prototype.get = () => { throw new Error('failed'); };
48+
assert.throws(() => assert.fail(''), { code: 'ERR_ASSERTION' });
49+
delete Object.prototype.get;
50+
});

Diff for: ‎test/parallel/test-assert-first-line.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44

55
require('../common');
66
const assert = require('assert');
7+
const { test } = require('node:test');
78
const { path } = require('../common/fixtures');
89

9-
assert.throws(
10-
() => require(path('assert-first-line')),
11-
{
12-
name: 'AssertionError',
13-
message: "The expression evaluated to a falsy value:\n\n ässört.ok('')\n"
14-
}
15-
);
10+
test('Verify that asserting in the very first line produces the expected result', () => {
11+
assert.throws(
12+
() => require(path('assert-first-line')),
13+
{
14+
name: 'AssertionError',
15+
message: "The expression evaluated to a falsy value:\n\n ässört.ok('')\n"
16+
}
17+
);
1618

17-
assert.throws(
18-
() => require(path('assert-long-line')),
19-
{
20-
name: 'AssertionError',
21-
message: "The expression evaluated to a falsy value:\n\n assert.ok('')\n"
22-
}
23-
);
19+
assert.throws(
20+
() => require(path('assert-long-line')),
21+
{
22+
name: 'AssertionError',
23+
message: "The expression evaluated to a falsy value:\n\n assert.ok('')\n"
24+
}
25+
);
26+
});

Diff for: ‎test/parallel/test-assert-if-error.js

+74-71
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,93 @@
22

33
require('../common');
44
const assert = require('assert');
5+
const { test } = require('node:test');
56

6-
// Test that assert.ifError has the correct stack trace of both stacks.
7-
8-
let err;
9-
// Create some random error frames.
10-
(function a() {
11-
(function b() {
12-
(function c() {
13-
err = new Error('test error');
7+
test('Test that assert.ifError has the correct stack trace of both stacks', () => {
8+
let err;
9+
// Create some random error frames.
10+
(function a() {
11+
(function b() {
12+
(function c() {
13+
err = new Error('test error');
14+
})();
1415
})();
1516
})();
16-
})();
1717

18-
const msg = err.message;
19-
const stack = err.stack;
18+
const msg = err.message;
19+
const stack = err.stack;
2020

21-
(function x() {
22-
(function y() {
23-
(function z() {
24-
let threw = false;
25-
try {
26-
assert.ifError(err);
27-
} catch (e) {
28-
assert.strictEqual(e.message,
29-
'ifError got unwanted exception: test error');
30-
assert.strictEqual(err.message, msg);
31-
assert.strictEqual(e.actual, err);
32-
assert.strictEqual(e.actual.stack, stack);
33-
assert.strictEqual(e.expected, null);
34-
assert.strictEqual(e.operator, 'ifError');
35-
threw = true;
36-
}
37-
assert(threw);
21+
(function x() {
22+
(function y() {
23+
(function z() {
24+
let threw = false;
25+
try {
26+
assert.ifError(err);
27+
} catch (e) {
28+
assert.strictEqual(e.message,
29+
'ifError got unwanted exception: test error');
30+
assert.strictEqual(err.message, msg);
31+
assert.strictEqual(e.actual, err);
32+
assert.strictEqual(e.actual.stack, stack);
33+
assert.strictEqual(e.expected, null);
34+
assert.strictEqual(e.operator, 'ifError');
35+
threw = true;
36+
}
37+
assert(threw);
38+
})();
3839
})();
3940
})();
40-
})();
41+
});
4142

42-
assert.throws(
43-
() => {
44-
const error = new Error();
45-
error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
46-
assert.ifError(error);
47-
},
48-
(error) => {
49-
assert(!error.stack.includes('Yes!'));
50-
return true;
51-
}
52-
);
43+
test('General ifError tests', () => {
44+
assert.throws(
45+
() => {
46+
const error = new Error();
47+
error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
48+
assert.ifError(error);
49+
},
50+
(error) => {
51+
assert(!error.stack.includes('Yes!'));
52+
return true;
53+
}
54+
);
5355

54-
assert.throws(
55-
() => assert.ifError(new TypeError()),
56-
{
57-
message: 'ifError got unwanted exception: TypeError'
58-
}
59-
);
56+
assert.throws(
57+
() => assert.ifError(new TypeError()),
58+
{
59+
message: 'ifError got unwanted exception: TypeError'
60+
}
61+
);
6062

61-
assert.throws(
62-
() => assert.ifError({ stack: false }),
63-
{
64-
message: 'ifError got unwanted exception: { stack: false }'
65-
}
66-
);
63+
assert.throws(
64+
() => assert.ifError({ stack: false }),
65+
{
66+
message: 'ifError got unwanted exception: { stack: false }'
67+
}
68+
);
6769

68-
assert.throws(
69-
() => assert.ifError({ constructor: null, message: '' }),
70-
{
71-
message: 'ifError got unwanted exception: '
72-
}
73-
);
70+
assert.throws(
71+
() => assert.ifError({ constructor: null, message: '' }),
72+
{
73+
message: 'ifError got unwanted exception: '
74+
}
75+
);
7476

75-
assert.throws(
76-
() => { assert.ifError(false); },
77-
{
78-
message: 'ifError got unwanted exception: false'
79-
}
80-
);
77+
assert.throws(
78+
() => { assert.ifError(false); },
79+
{
80+
message: 'ifError got unwanted exception: false'
81+
}
82+
);
83+
});
8184

82-
// Should not throw.
83-
assert.ifError(null);
84-
assert.ifError();
85-
assert.ifError(undefined);
85+
test('Should not throw', () => {
86+
assert.ifError(null);
87+
assert.ifError();
88+
assert.ifError(undefined);
89+
});
8690

87-
// https://github.com/nodejs/node-v0.x-archive/issues/2893
88-
{
91+
test('https://github.com/nodejs/node-v0.x-archive/issues/2893', () => {
8992
let threw = false;
9093
try {
9194
// eslint-disable-next-line no-restricted-syntax
@@ -98,4 +101,4 @@ assert.ifError(undefined);
98101
assert(!e.stack.includes('throws'), e);
99102
}
100103
assert(threw);
101-
}
104+
});

0 commit comments

Comments
 (0)
Please sign in to comment.