help icon indicating copy to clipboard operation
help copied to clipboard

Asserting a range with descriptive error messages

Open nicholaswmin opened this issue 1 year ago • 15 comments

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 between 1500 and 2000?

Now, to do that I use a pair of assert.ok like so:

const value = 1700

assert.ok(value >= 1500)
assert.ok(value <= 2000)

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:

assert.ok(value >= 1500, `expected value >= 1500 , got: ${value}`)
assert.ok(value <= 2000, `expected value <= 2000 , got: ${value}`)

Apart from cooking up my own assertion functions, is there a better way to do this out-the-box that I'm missing?

nicholaswmin avatar Aug 03 '24 19:08 nicholaswmin