Skip to content

Commit 132925f

Browse files
test: move Assert class instance tests to separate file
1 parent 3f1c996 commit 132925f

File tree

3 files changed

+106
-80
lines changed

3 files changed

+106
-80
lines changed

lib/assert.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,5 +860,4 @@ assert.strict = ObjectAssign(strict, assert, {
860860
assert.strict.Assert = Assert;
861861
assert.strict.strict = assert.strict;
862862

863-
module.exports = assert;
864863
module.exports.Assert = Assert;

test/parallel/test-assert-class.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { Assert } = require('assert');
6+
const { test } = require('node:test');
7+
8+
// Disable colored output to prevent color codes from breaking assertion
9+
// message comparisons. This should only be an issue when process.stdout
10+
// is a TTY.
11+
if (process.stdout.isTTY) {
12+
process.env.NODE_DISABLE_COLORS = '1';
13+
}
14+
15+
test('Assert class basic instance', () => {
16+
const assertInstance = new Assert();
17+
18+
assertInstance.ok(assert.AssertionError.prototype instanceof Error,
19+
'assert.AssertionError instanceof Error');
20+
assertInstance.ok(true);
21+
assertInstance.throws(
22+
() => { assertInstance.fail(); },
23+
{
24+
code: 'ERR_ASSERTION',
25+
name: 'AssertionError',
26+
message: 'Failed',
27+
operator: 'fail',
28+
actual: undefined,
29+
expected: undefined,
30+
generatedMessage: true,
31+
stack: /Failed/
32+
}
33+
);
34+
assertInstance.equal(undefined, undefined);
35+
assertInstance.notEqual(true, false);
36+
assertInstance.throws(
37+
() => assertInstance.deepEqual(/a/),
38+
{ code: 'ERR_MISSING_ARGS' }
39+
);
40+
assertInstance.throws(
41+
() => assertInstance.notDeepEqual('test'),
42+
{ code: 'ERR_MISSING_ARGS' }
43+
);
44+
assertInstance.notStrictEqual(2, '2');
45+
assertInstance.throws(() => assertInstance.strictEqual(2, '2'),
46+
assertInstance.AssertionError, 'strictEqual(2, \'2\')');
47+
assertInstance.throws(
48+
() => {
49+
assertInstance.partialDeepStrictEqual({ a: true }, { a: false }, 'custom message');
50+
},
51+
{
52+
code: 'ERR_ASSERTION',
53+
name: 'AssertionError',
54+
message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }\n'
55+
}
56+
);
57+
assertInstance.throws(
58+
() => assertInstance.match(/abc/, 'string'),
59+
{
60+
code: 'ERR_INVALID_ARG_TYPE',
61+
message: 'The "regexp" argument must be an instance of RegExp. ' +
62+
"Received type string ('string')"
63+
}
64+
);
65+
assertInstance.throws(
66+
() => assertInstance.doesNotMatch(/abc/, 'string'),
67+
{
68+
code: 'ERR_INVALID_ARG_TYPE',
69+
message: 'The "regexp" argument must be an instance of RegExp. ' +
70+
"Received type string ('string')"
71+
}
72+
);
73+
74+
/* eslint-disable no-restricted-syntax */
75+
{
76+
function thrower(errorConstructor) {
77+
throw new errorConstructor({});
78+
}
79+
80+
let threw = false;
81+
try {
82+
assertInstance.doesNotThrow(() => thrower(TypeError), assertInstance.AssertionError);
83+
} catch (e) {
84+
threw = true;
85+
assertInstance.ok(e instanceof TypeError);
86+
}
87+
assertInstance.ok(threw, 'assertInstance.doesNotThrow with an explicit error is eating extra errors');
88+
}
89+
{
90+
let threw = false;
91+
const rangeError = new RangeError('my range');
92+
93+
try {
94+
assertInstance.doesNotThrow(() => {
95+
throw new TypeError('wrong type');
96+
}, TypeError, rangeError);
97+
} catch (e) {
98+
threw = true;
99+
assertInstance.ok(e.message.includes(rangeError.message));
100+
assertInstance.ok(e instanceof assertInstance.AssertionError);
101+
assertInstance.ok(!e.stack.includes('doesNotThrow'), e);
102+
}
103+
assertInstance.ok(threw);
104+
}
105+
/* eslint-enable no-restricted-syntax */
106+
});

test/parallel/test-assert.js

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
const { invalidArgTypeHelper } = require('../common');
2525
const assert = require('assert');
26-
const { Assert } = require('assert');
2726
const { inspect } = require('util');
2827
const { test } = require('node:test');
2928
const vm = require('vm');
@@ -1596,83 +1595,5 @@ test('assert/strict exists', () => {
15961595
assert.strictEqual(require('assert/strict'), assert.strict);
15971596
});
15981597

1599-
test('Assert class basic instance', () => {
1600-
const assertInstance = new Assert();
1601-
1602-
assertInstance.ok(assert.AssertionError.prototype instanceof Error,
1603-
'assert.AssertionError instanceof Error');
1604-
assertInstance.ok(true);
1605-
assertInstance.equal(undefined, undefined);
1606-
assertInstance.notEqual(true, false);
1607-
assertInstance.throws(
1608-
() => assertInstance.deepEqual(/a/),
1609-
{ code: 'ERR_MISSING_ARGS' }
1610-
);
1611-
assertInstance.throws(
1612-
() => assertInstance.notDeepEqual('test'),
1613-
{ code: 'ERR_MISSING_ARGS' }
1614-
);
1615-
assertInstance.notStrictEqual(2, '2');
1616-
assertInstance.throws(() => assertInstance.strictEqual(2, '2'),
1617-
assertInstance.AssertionError, 'strictEqual(2, \'2\')');
1618-
assertInstance.throws(
1619-
() => {
1620-
assertInstance.partialDeepStrictEqual({ a: true }, { a: false }, 'custom message');
1621-
},
1622-
{
1623-
code: 'ERR_ASSERTION',
1624-
name: 'AssertionError',
1625-
message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }\n'
1626-
}
1627-
);
1628-
assertInstance.throws(
1629-
() => assertInstance.match(/abc/, 'string'),
1630-
{
1631-
code: 'ERR_INVALID_ARG_TYPE',
1632-
message: 'The "regexp" argument must be an instance of RegExp. ' +
1633-
"Received type string ('string')"
1634-
}
1635-
);
1636-
assertInstance.throws(
1637-
() => assertInstance.doesNotMatch(/abc/, 'string'),
1638-
{
1639-
code: 'ERR_INVALID_ARG_TYPE',
1640-
message: 'The "regexp" argument must be an instance of RegExp. ' +
1641-
"Received type string ('string')"
1642-
}
1643-
);
1644-
1645-
{
1646-
function thrower(errorConstructor) {
1647-
throw new errorConstructor({});
1648-
}
1649-
1650-
let threw = false;
1651-
try {
1652-
assertInstance.doesNotThrow(() => thrower(TypeError), assertInstance.AssertionError);
1653-
} catch (e) {
1654-
threw = true;
1655-
assertInstance.ok(e instanceof TypeError);
1656-
}
1657-
assertInstance.ok(threw, 'assertInstance.doesNotThrow with an explicit error is eating extra errors');
1658-
}
1659-
{
1660-
let threw = false;
1661-
const rangeError = new RangeError('my range');
1662-
1663-
try {
1664-
assertInstance.doesNotThrow(() => {
1665-
throw new TypeError('wrong type');
1666-
}, TypeError, rangeError);
1667-
} catch (e) {
1668-
threw = true;
1669-
assertInstance.ok(e.message.includes(rangeError.message));
1670-
assertInstance.ok(e instanceof assertInstance.AssertionError);
1671-
assertInstance.ok(!e.stack.includes('doesNotThrow'), e);
1672-
}
1673-
assertInstance.ok(threw);
1674-
}
1675-
});
1676-
16771598
/* eslint-enable no-restricted-syntax */
16781599
/* eslint-enable no-restricted-properties */

0 commit comments

Comments
 (0)