Chai: Add `.loose` and `.strict`

Created on 18 Jan 2017  路  8Comments  路  Source: chaijs/chai

As discussed in #280, there are legitimate use cases for performing loose equality comparisons, even though in most cases it's unsafe. Also, it would be useful to have a .strict language chain that's mostly just to improve expressiveness of an assertion, but in rare occasions could also be used to clear the .deep or .loose flags within an assertion chain.

expect('3').to.loose.equal(3); // Perform type coercion
expect(myObj).to.strict.equal(myObj); // Just for expressiveness
expect({a: 1}).to.deep.equal({a: 1}).but.not.strict.equal({a: 1}); // Clear the `deep` flag for second `equal`

I'm currently in favor of adding .loose and .strict as long as the documentation notes the dangers of .loose. (I'm currently working on a separate documentation update that adds such warnings to existing assertions).

feature-requests more-discussion-needed

Most helpful comment

I agree with adding .loose and .strict.

I would like to understand better why we have this inconsistency between assert and expect|should interfaces. On assert we have .equal which performs loose comparison, while in expect|should the .equal assertion is strict by default.

I don't know if this should be addressed in a different issue, but I think we could make assert.equal an alias to assert.strictEqual for consistency (which is a breaking change), and add an assert.looseEqual method.

All 8 comments

I agree with adding .loose and .strict.

I would like to understand better why we have this inconsistency between assert and expect|should interfaces. On assert we have .equal which performs loose comparison, while in expect|should the .equal assertion is strict by default.

I don't know if this should be addressed in a different issue, but I think we could make assert.equal an alias to assert.strictEqual for consistency (which is a breaking change), and add an assert.looseEqual method.

This isn't critical, so let's defer it to post v4.

@meeber do you think creating a post-v4 label and applying it to a bunch of these issues is a good idea?

@keithamus Good idea. There's a lot more post-v4 issues than pre-v4 issues, and the post-v4s are gonna be hanging around a lot longer, so I'm gonna label the pre-v4 issues instead of the post-v4 issues.

@meeber So fix-for-v4 means that we need to fix those issues before releasing v4?

@vieiralucas Yup. I just changed it to fix-before-v4. Also, my selections are certainly up for debate.

We've added this to our Roadmap https://github.com/chaijs/chai/projects/2! We'll be releasing chai 5 soon, but for now I'll close this issue because it is tracked on our roadmap.

Hi @keithamus, I see .loose.equal is in the Done column of the roadmap, but I don't see it in the current version of chai.

image

Any idea when it'll be available? The system I write tests for is a bit wild; sometimes you get numbers and sometimes you get strings, and it doesn't actually matter which one they are as long as the number's right. I'm doing a .deep.equals on an object with just such properties, and it's causing test failures since I can't specify loose equality.

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xareelee picture xareelee  路  3Comments

AnAppAMonth picture AnAppAMonth  路  3Comments

kharandziuk picture kharandziuk  路  4Comments

endymion00 picture endymion00  路  3Comments

domenic picture domenic  路  4Comments