Chai: Feature request: Assert less/greater than or equal to

Created on 22 Nov 2015  路  8Comments  路  Source: chaijs/chai

I was surprised that this doesn't exist already.

assert.lte(3, 3, '3 is less than or equal to 3');

It might seem like a small shortcut, but things get messy when you're expecting a number and don't get one:

console.log(null <= 4);  //=> true
console.log(undefined <= 4);  //=> false

I currently do something along the lines of:

const lte = actual === max || actual && actual < max;
assert.isTrue(lte, '...');

... but this still isn't perfect, especially if you want '3' < 4 to be an error (comparing a string and a number).

I propose that lte/gte be designed to work strictly with numbers and throw for anything else.

Most helpful comment

Just discovered that expect / should has this.

expect(3).to.be.at.least(3);
expect(3).to.be.at.most(3);

All 8 comments

Just discovered that expect / should has this.

expect(3).to.be.at.least(3);
expect(3).to.be.at.most(3);

Hey @sholladay thanks for the issue.

As you pointed out - expect has at.least and at.most. Assert has those methods too - so it has assert.isAtMost, and assert.isAtLeast (it also has assert.isBelow and assert.isAtLeast). I suppose we could make aliases for lte and gte.

I'll close this for now - optimistic in the hope that assert.isAtMost is what you want, but if you feel strongly about adding lte and gte then I'll reopen and we can discuss it more.

@keithamus hmm looks like this is accidentally undocumented.

http://chaijs.com/api/assert/

That's how I figured out that expect has it. But looks like you're right, I do see the assert code and test.

I could open a new issue for docs or we can repurpose this.

Aliases are just "nice to have" for me, don't care that much.

@sholladay it is documented, but the site is a bit out of date. It's a long story but we're porting the site to gh-pages, and in the meantime the old one is getting out of date day-by-day. Nothing to fix but getting the site port done ASAP

FYI v3.3.0 added the assert alternatives - but the chaijs.com site is still reading from chai 2.1.0 docs.

Makes sense. Thanks!

It would be nice in the docs to include greater/less than so that when searching least and most will pop up

Was this page helpful?
0 / 5 - 0 ratings

Related issues

domenic picture domenic  路  4Comments

kharandziuk picture kharandziuk  路  4Comments

jockster picture jockster  路  4Comments

sverrirs picture sverrirs  路  3Comments

meeber picture meeber  路  4Comments