-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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: support inequalities #55796
base: main
Are you sure you want to change the base?
assert: support inequalities #55796
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #55796 +/- ##
=======================================
Coverage 88.40% 88.41%
=======================================
Files 654 654
Lines 187815 187891 +76
Branches 36136 36145 +9
=======================================
+ Hits 166045 166130 +85
- Misses 15001 15003 +2
+ Partials 6769 6758 -11
|
This PR goes against Node.js core principles, we're all idealist people who strive for a better world, we do not and never will want to support inequalities! |
22aa05c
to
664580e
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.
besides naming, LGTM
Would you rather |
* @param {string | Error} [message] | ||
*/ | ||
assert.greaterThan = function greaterThan(actual, expected, message = `Expected ${actual} to be greater than ${expected}`) { | ||
if (actual <= 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.
shouldnt we validate that inputs are numbers?
I feel +1 for having this whenever I see
An important thing to consider is that // `false == true` not informative, have to look at the code
assert(retryCount < RETRY_MAX);
// `Expected 5 to be less than 5` tells the compared values but still not informative
assert.lessThan(retryCount, RETRY_MAX);
// Informative, both lines yield same message
assert(retryCount < RETRY_MAX, `retryCount has value of ${retryCount} which is greater than RETRY_MAX`);
assert.lessThan(retryCount, RETRY_MAX, `retryCount has value of ${retryCount} which is greater than RETRY_MAX`);
// Even better when message tells the context of error
assert(retryCount < RETRY_MAX, 'function myFunc retried too many times when delay is set to 0 and data is recursive') The current implementation uses If we make strict version with type validation, how do we handle the case when types are not matched? E.g.: // Old code without explicit `assert.equal(typeof retryCount, 'number')`
assert.ok(retryCount <= RETRY_MAX, `retry count greater than ${RETRY_MAX}`);
// New code?
assert.strictNotGreaterThan(retryCount, RETRY_MAX, `retry count greater than ${RETRY_MAX} OR retry count's type ${typeof retryCount} is not number`); |
Sounds good to me. Currently, as you said, I do use the message property, but then difference that the error contains the "actual" and "expected" failures as properties. I could also do what the other assertion methods do, that is, have their own message that displays along with the user provided message. |
This PR introduces four new assertion methods
Motivation
These methods align with the capabilities provided by other popular testing libraries, such as:
While it’s possible to use standard assertions like
assert(x > y)
, dedicated methods offer more informative error messages by including displaying the values ofx
andy
in the failure output. This saves time and effort, particularly when working with large test suites, as there’s no need to manually format output withassert(x > y, `${x} > ${y}`)
.