Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: refactor AssertionError properties #25250

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/internal/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,32 @@ class AssertionError extends Error {
this.generatedMessage = !message;
this.name = 'AssertionError [ERR_ASSERTION]';
this.code = 'ERR_ASSERTION';
this.actual = actual;
this.expected = expected;
// This prevents inspecting the actual and expected value by default.
// That is logical since the summary will be printed already. Users can then
// explicitly access these properties in case they want to know what value
// was used.
Object.defineProperties(this, {
actual: {
get() {
return actual;
},
set(val) {
actual = val;
},
enumerable: true,
configurable: true
},
expected: {
get() {
return expected;
},
set(val) {
expected = val;
},
enumerable: true,
configurable: true
}
});
this.operator = operator;
Error.captureStackTrace(this, stackStartFn);
}
Expand Down
30 changes: 19 additions & 11 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,14 @@ const circular = { y: 1 };
circular.x = circular;

function testAssertionMessage(actual, expected, msg) {
try {
assert.strictEqual(actual, '');
} catch (e) {
assert.strictEqual(
e.message,
msg || strictEqualMessageStart +
`+ actual - expected\n\n+ ${expected}\n- ''`
);
assert.ok(e.generatedMessage, 'Message not marked as generated');
}
assert.throws(
() => assert.strictEqual(actual, ''),
{
generatedMessage: true,
message: msg || strictEqualMessageStart +
`+ actual - expected\n\n+ ${expected}\n- ''`
}
);
}

function testShortAssertionMessage(actual, expected) {
Expand All @@ -280,7 +278,7 @@ testShortAssertionMessage(false, 'false');
testShortAssertionMessage(100, '100');
testShortAssertionMessage(NaN, 'NaN');
testShortAssertionMessage(Infinity, 'Infinity');
testShortAssertionMessage('', '""');
testShortAssertionMessage('a', '"a"');
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
testShortAssertionMessage('foo', '\'foo\'');
testShortAssertionMessage(0, '0');
testShortAssertionMessage(Symbol(), 'Symbol()');
Expand Down Expand Up @@ -308,6 +306,16 @@ try {
`${strictEqualMessageStart}\n1 !== 2\n`
);
assert.ok(e.generatedMessage, 'Message not marked as generated');
const descriptors = Object.getOwnPropertyDescriptors(e);
assert.strictEqual(typeof descriptors.actual.get, 'function');
assert.strictEqual(typeof descriptors.expected.get, 'function');
const { actual, expected } = e;
e.actual++;
e.expected++;
assert.strictEqual(descriptors.actual.enumerable, true);
assert.strictEqual(descriptors.expected.enumerable, true);
assert.strictEqual(e.actual, actual + 1);
assert.strictEqual(e.expected, expected + 1);
}

try {
Expand Down