-
-
Notifications
You must be signed in to change notification settings - Fork 697
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
Implement a matcher API for "loosely" asserting on deep properties #644
Comments
I like the idea behind this. Where do you envision this falling on the roadmap? The only thing I'm not crazy about is the term "match", simply because of its existing association with regular expressions. I haven't yet put any thought into the feasibility of this or anything resembling this, but just pie-in-the-skying, it'd be pretty neat if the syntax resembled whatever interface the developer was using: expect(o).to.deep.equal({
foo: 'bar',
baz: expect.a('function').with.length(2),
});
o.should.deep.equal({
foo: 'bar',
baz: should.be.a('function').with.length(2),
}); |
I'm doing some work on deep-eql - extending it to take an optional comparator function, which will go a lot of the way to providing this kind of functionality. |
As mentioned in #709 I'd like to have some sort of high-level assertion that enables me to deep equal two JSON objects while using something like |
As @Turbo87 mentioned in #709, deep equal could, for example, take a second arg of a comparitor
Which fits the work we're doing in deep-eql to power this feature. |
Did Chai eventually add support for this? I couldn't find any docs around it.. cc @keithamus |
We've not added it yet. It's on my agenda after better error messaging though. Can't promise timelines but it will be coming! |
I have been through all listed discussions and each of them make propositions to permanently include new assertions into chai, but I can't find any temporary hacks/solutions. How could I pass a test comparing two objects each containing an anonymous function like this: var inputObj = {name: 'input', fn: () => ({key: 'value'})}
var expectedObj = {name: 'input', fn: () => ({key: 'value'})}
expect( inputObj ).to.deep.equal( expectedObj ) The only hack I found is this: try {
expect( inputObj ).to.deep.equal( expectedObj )
}
catch (e) {
console.log( JSON.stringify(e.actual) === JSON.stringify(e.expected) )
} But this is really too much of a hack for me. Any other ideas ? |
@Jonarod's solution is the only seemingly close solution for keeping this code clean (shy of switching to Jest). It falls short though when objects keys are in a different order. |
@mhuggins my "solution" is not even one... it's just a way to have some alerts and visually compare stuff. But I don't recommend this for serious tests plans... var inputObj = {name: 'input', fn: () => ({key: 'value'})}
var expectedObj = {name: 'input', fn: () => ({key: 'value'})}
// compare strings without functions
expect( JSON.stringify(inputObj) ).to.deep.equal( JSON.stringify(expectedObj) )
// compare functions results
expect( inputObj.fn() ).to.deep.equal( expectedOb.fn() ) yet, this is just lame workaround and won't work in 100% cases... |
@keithamus just wondering about the status. Can a better timeline be given at this point? |
the |
@keithamus I can't find any example in documentation. Is it possible to write something like this ? expect(1.0001).to.deep.equal(1, (a, b) => Math.abs(a - b) < 1e-3) |
@d-damien it is not possible right now, as the second argument is not passed down to |
This is now in our roadmap https://github.com/chaijs/chai/projects/2. |
While folks are waiting on this, there's actually an easy workaround using Sinon matchers. The implementation is something like this:
And then you can just use this new assertion.
Of course the error messages are a bit weird since they're something like |
@vaskevich I like that approach a lot. You should be able to implement it using |
the plan is to have this behaviour in Chai 5. You're welcome to make plugins that accept Sinon matchers for now - but be warned it may be obsolete by the time Chai 5 comes out 😃 |
I ended up implementing @vaskevich approach as a plugin. It even replaces this potentially confusing text about stubs being called. Here it is:
|
@jpbochi works great, finally, I could replace my deep.equal with your plugin and assert objects that contain functions! :D |
For those considering Sinon, you probably want to use sinon.assert.match() |
We have come across this discussion a few times, but for many people
deep.equal
is too-strict of an assertion. For example:jasmine.any
.The proposal is thus:
Using
deep.equal
or similar complex assertions, we are able to make much looser assertions about specific parts of the assertion, using an assertion sentinel in place of a value:The text was updated successfully, but these errors were encountered: