Skip to content

Asserting a range with descriptive error messages  #4461

Description

@nicholaswmin

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions