question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement a matcher API for "loosely" asserting on deep properties

See original GitHub issue

We have come across this discussion a few times, but for many people deep.equal is too-strict of an assertion. For example:

  • #97 discusses Sinon matchers, and having something similar.
  • #324 went a bit further, including showing how Jasmine handles this with jasmine.any.
  • This may help with issues like #193
  • This would help to solve issues like #643 (and perhaps tangentially, #426)
  • Personally I’ve run into small problems with this stuff before, with things like asserting arguments with chai-spies

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:

expect(o).to.deep.equal({
  foo: 'bar',
  baz: chai.match.a('function').with.length(2),
});

expect(someSpy).to.be.calledWith('foo', chai.match.a('string').that.matches(/bar/));

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:41
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

13reactions
keithamuscommented, Oct 12, 2017

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!

7reactions
jpbochicommented, Jun 28, 2018

I ended up implementing @vaskevich approach as a plugin. It even replaces this potentially confusing text about stubs being called. Here it is:

chai.use((_chai, utils) => {
  chai.Assertion.addMethod('matchEql', function fn(expectedMatch) {
    const subject = utils.flag(this, 'object');
    const stub = sinon.stub();
    stub(subject);
    try {
      sinon.assert.calledWithMatch(stub, expectedMatch);
    } catch (error) {
      error.name = 'MatchAssertionError';
      error.message = error.message.replace(
        /^expected stub to be called with match/,
        `expected ${utils.objDisplay(subject)} to match`
      );
      throw error;
    }
  });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Matchers - Sinon.JS
Matchers can be passed as arguments to spy.calledOn, spy.calledWith, spy.returned and thecorresponding sinon.assert functions as well as spy.withArgs.
Read more >
assert-match - npm
assert -match allows you to check actual value (or its property) not against a specific expected value as standard deep assertions do, but...
Read more >
Loose match one value in jest.toHaveBeenCalledWith
I have an analytics tracker that will only call after 1 second and with an object where the intervalInMilliseconds (duration) value is not ......
Read more >
Writing Good Assertions - Manning Publications
From Testing JavaScript Applications by Lucas da Costa. In this article, I will teach you techniques to help you write better assertions.
Read more >
Karate API Automation-Assertions using matcher APIs - Medium
Assert whole actual JSON against expected JSON using match equals ... create a file named as “EResult.json” under “Karate.api.data” package ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found