-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: improve error messages #19467
Conversation
|
Seems like this breaks It does not seem like a big problem to me but I might be wrong. Here is a small patch that might fix it (without verifying it for real): @@ -439,7 +439,7 @@ class RuleTester {
`Expected '${actual}' to match ${expected}`
);
} else {
- assert.strictEqual(actual, expected);
+ assert.strictEqual(actual, expected, util.format("%s strictEqual %s", actual, expected));
}
} Looking at it further, it might make sense to change some more assert things as well like e.g.: diff --git a/lib/testers/rule-tester.js b/lib/testers/rule-tester.js
index de218a875..798253368 100644
--- a/lib/testers/rule-tester.js
+++ b/lib/testers/rule-tester.js
@@ -396,11 +396,11 @@ class RuleTester {
* @private
*/
function assertASTDidntChange(beforeAST, afterAST) {
- if (!lodash.isEqual(beforeAST, afterAST)) {
-
- // Not using directly to avoid performance problem in node 6.1.0. See #6111
- // eslint-disable-next-line no-restricted-properties
- assert.deepEqual(beforeAST, afterAST, "Rule should not modify AST.");
+ if (util.isDeepStrictEqual) {
+ assert.deepStrictEqual(beforeAST, afterAST, "Rule should not modify AST.");
+ // Not using directly to avoid performance problem in Node.js 6 and 7. See #6111
+ } else if (!lodash.isEqual(beforeAST, afterAST)) {
+ assert.fail("Rule should not modify AST.");
}
}
@@ -539,7 +539,7 @@ class RuleTester {
} else {
// Message was an unexpected type
- assert.fail(message, null, "Error should be a string, object, or RegExp.");
+ assert.fail("Error should be a string, object, or RegExp.");
}
}
}
@@ -554,8 +554,7 @@ class RuleTester {
} else {
const fixResult = SourceCodeFixer.applyFixes(item.code, messages);
- // eslint-disable-next-line no-restricted-properties
- assert.equal(fixResult.output, item.output, "Output is incorrect.");
+ assert.strictEqual(fixResult.output, item.output, "Output is incorrect.");
}
} |
I agree that the Presumably, assert.strictEqual(message, new assert.AssertionError({ actual: 5, expected: 'foo', operator: '===' }).message); |
If we do feature detection, maybe include a comment about when we can drop the feature detection. (In other words, "this feature detection code won't be needed anymore after we drop support for Node.js 8.0.0" or whatever. That assumes it's possible to know that version number at this time...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
802d12e
to
bc52f4d
Compare
Rebased due to conflicts. New CI https://ci.nodejs.org/job/node-test-pull-request/13857/ |
4b06f51
to
ec5e5c1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the code (rebased due to conflicts) and fixed some small bugs (e.g. errors with extra properties did not have a proper error message), removed the errorDiff
property` and added some proxy tests.
breakLength: Infinity, | ||
// Assert does not detect proxies currently. | ||
showProxy: false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this is actually relatively important to make sure the errors are actually printed properly, no matter how the default options where changed. Only the color setting is not important.
lib/internal/errors.js
Outdated
Object.defineProperty(target, 'message', { value: source.message }); | ||
Object.setPrototypeOf(target, Object.getPrototypeOf(source)); | ||
return target; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the easiest and best way to pretend to be an error without a stack trace.
lib/internal/errors.js
Outdated
notDeepStrictEqual: 'Input A expected to strictly not deep equal input B', | ||
strictEqual: 'Input A expected to strictly equal input B', | ||
notStrictEqual: 'Input A expected to strictly not equal input B' | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Trott @vsemozhetbyt PTAL.
// This code path should not be possible to reach. | ||
// The output is identical but it is not clear why. | ||
base = 'Input objects not identical:'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has happened before and there might still be some rare cases that I am not aware about that could produce this weird output. I am not sure how to handle that better though.
'stack' in actual && actual instanceof Error && | ||
'stack' in expected && expected instanceof Error) { | ||
actual = copyError(actual); | ||
expected = copyError(expected); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is best to actually show the stack trace in the comparison in case there is only a single error. That way it is clearer in what way the input is different from each other.
const arrProxy = new Proxy([1, 2], {}); | ||
assert.deepStrictEqual(arrProxy, [1, 2]); | ||
const tmp = util.inspect.defaultOptions; | ||
util.inspect.defaultOptions = { showProxy: true }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any opinions from others about checking for proxies?
Still lgtm |
lib/internal/errors.js
Outdated
@@ -19,6 +19,13 @@ let green = ''; | |||
let red = ''; | |||
let white = ''; | |||
|
|||
const READABLE_OPERATOR = { | |||
deepStrictEqual: 'Input A expected to strictly deep equal input B', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe strictly deep-equal
?
lib/internal/errors.js
Outdated
@@ -19,6 +19,13 @@ let green = ''; | |||
let red = ''; | |||
let white = ''; | |||
|
|||
const READABLE_OPERATOR = { | |||
deepStrictEqual: 'Input A expected to strictly deep equal input B', | |||
notDeepStrictEqual: 'Input A expected to strictly not deep equal input B', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here? deep equal
-> deep-equal
?
Not sure how I feel about |
I addressed the comment. @Trott I would like to keep |
Yes, that's fine by me. |
It would be nice to get another LG before landing. |
@@ -7,7 +7,7 @@ try { | |||
process.env.COLORTERM = '1'; | |||
assert.deepStrictEqual([1, 2], [2, 2]); | |||
} catch (err) { | |||
const expected = 'Input A expected to deepStrictEqual input B:\n' + | |||
const expected = 'Input A expected to strictly deep equal input B:\n' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question as above: Shouldn't this fail because it should be expecting deep-equal
?
@nodejs/testing @nodejs/tsc PTAL. I would like to land this soon. |
Since it's semver-major, it needs a CITGM run, if I'm not mistaken. |
@Trott I did that already in the beginning. It breaks the eslint tests and I opened a PR in eslint to address that. Besides that there was nothing and that should not be blocking. |
Not sure how I missed that. Sorry! |
If there is no objection until then, I am going to land this in ~30 hours. |
No objections from me |
Re-running failed Windows CI: https://ci.nodejs.org/job/node-test-commit-windows-fanned/17190/ |
Right now it is not possible to distinguish arguments from a regular object. This adds a arguments indicator.
This improves the error messages for: - assert.notDeepStrictEqual - assert.deepStrictEqual - assert.notStrictEqual - assert.strictEqual Those will now always use the same error message as used in the strict mode.
The property is not necessary as it is possible to check for the operator instead.
6ae2061
to
611ddd2
Compare
Rebased due to minor conflicts in the tests. One more CI before landing: https://ci.nodejs.org/job/node-test-pull-request/14266/ |
Still LGTM |
@nodejs/tsc ... any objections to this landing in 10.0.0? |
Right now it is not possible to distinguish arguments from a regular object. This adds a arguments indicator. PR-URL: #19467 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
This improves the error messages for: - assert.notDeepStrictEqual - assert.deepStrictEqual - assert.notStrictEqual - assert.strictEqual Those will now always use the same error message as used in the strict mode. PR-URL: #19467 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
The property is not necessary as it is possible to check for the operator instead. PR-URL: #19467 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
+1 for me |
Right now it is not possible to distinguish arguments from a regular object. This adds a arguments indicator. PR-URL: #19467 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
This improves the error messages for: - assert.notDeepStrictEqual - assert.deepStrictEqual - assert.notStrictEqual - assert.strictEqual Those will now always use the same error message as used in the strict mode. PR-URL: #19467 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
The property is not necessary as it is possible to check for the operator instead. PR-URL: #19467 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
This switches all assert error messages to use the new diffing output as in the
assert
strict mode.It also makes sure not reference equal objects are now visualised as such. Before it would e.g. have been
[1, 2] strictEqual [1, 2]
and that would be quite confusing. That is now detected and expressed.I also added support in util.inspect to visualize arguments to distinguish them from a regular object.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes