-
Notifications
You must be signed in to change notification settings - Fork 284
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
Asserting a range with descriptive error messages #4461
Comments
CC @nodejs/assert |
looks reasonable to me! |
@nicholaswmin - pls feel free to raise a PR if you are willing to! |
Although, doesn't the assert message include the failed assertion line, so wouldn't it already show the failure? |
@RedYetiDev here's the output: import test from 'node:test'
await test('is within range', async t => {
const value = 2700
t.assert.ok(value >= 1500)
t.assert.ok(value <= 2000)
})
My "complaint" is that it doesn't print Now granted, the above is impossible to do with the code I'm providing because that's a Boolean expression. t.assert.ok(value <= 2000, value)
t.assert.ok(value <= 2000, value) AssertionError [ERR_ASSERTION]: Failed, value: 2700 was not ok Hmm.. I'm a bit confused... Not sure why I opened this issue as what I'm proposing sounds like a silly API. Doing this: t.assert.ok(value <= 2000, value) prints the value anyway. I don't remember what I had in mind. Sorry. |
|
I'm personally against adding specific assertion checks to It'll eventually snowball into a myriad utilities. I miss the hyper-declarative syntax of Chai but I've also come to love the minimal, no-frills API surface of ... I don't remember what i had in mind when i opened this. If I need range checks I can extend I think this needs to be closed ?? |
The closest thing I can figure out that would help without adding specific assertion checks is printing the expression itself on An example: import test from 'node:test'
await test('foo', async t => {
const value = 2700
t.assert.ok(value >= 1500)
t.assert.ok(value <= 2000)
}) logs:
which is more descriptive than the current:
Not sure if that's doable without doing esprima-like parsing though? |
For your specific case, a custom error (E.G.
In some cases, that is what happens: import assert from 'node:assert';
assert.ok(25 > 50);
|
Yes. That works! That's more than enough. I can repro your working example with |
I'm aware. I'm opening an issue for it as we speak. |
want me to have a look into it? |
If you'd like, see nodejs/node#54760 |
Fixed in nodejs/node#54776 |
Amazing; sorry, life got in the way ... |
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: nodejs#54776 Refs: nodejs/help#4461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Node.js Version
v22
NPM Version
10.8.2
Operating System
MacOS Sonoma
Subsystem
assert
Description
I work a lot with performance testing code which frequently leads in the unfortunate position where I need to assert ranges rather than exact values, i.e
is
value
between1500
and2000
?Now, to do that I use a pair of
assert.ok
like so:so far so good, could be better but that's clean and concise enough for my taste.
The problem is that setting a descriptive error message quickly becomes rather repetitive and convoluted.
I have to resort to something akin to this:
Apart from cooking up my own assertion functions, is there a better way to do this out-the-box that I'm missing?
The text was updated successfully, but these errors were encountered: