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.

Chain .property() assertions on objects

See original GitHub issue

Hello,

is there a workaround to allow something like this:

chai.expect({ foo: 'bar', bar: 'foo' })
  .to.have.property('foo', 'bar')
  .and.to.have.property('bar', 'foo')
  ;

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:21 (14 by maintainers)

github_iconTop GitHub Comments

15reactions
keithamuscommented, Feb 3, 2016

I just remembered that we have .include which can test subset bodies:

chai.expect({ foo: 'bar', bar: 'foo', baz: 'bing' }).to.include({ foo: 'bar', bar: 'foo' });

This should be suitable for testing many properties. You can even combine it with property for complex nesting:

var multiLayerObject = {
    foo: {
        bar: 1,
        baz: 2,
    },
    bing: 3,
    bong: 4,
};
chai.expect(multiLayerObject)
  .to.include({ bing: 3, bong: 4 })
  .and.have.property('foo').include({ bar: 1 });
3reactions
keithamuscommented, Jun 12, 2015

I quite like the idea of also… but it needs some more discussion to fill gaps; for example I can see people expecting to traverse deep objects like so:

var multiLayerObject = {
    foo: {
        bar: 1,
        baz: 2,
    },
    bing: {
        bong: 3
        bash: 4
    }
};

// Does it just go up a level?
expect(multiLayerObject).to.have.property('foo')
    .and.have.property('bar', 1)
    .and.also.have.property('baz', 2)
  .and.also.also.have.property('bing') // ???
    .and.have.property('bong', 3)
    .and.also.have.property('bash', 4);

// Or does it reset to the originally object within expect()?
expect(multiLayerObject).to.have.deep.property('foo.bar', 1)
    .and.also.have.deep.property('foo.baz', 2)
    .and.also.have.deep.property('bing.bong', 3)
    .and.also.have.deep.property('bing.bash', 4);

At the risk of sounding cynical - I can see either implementation getting a few issues springing up discussing semantics and trying to alter them to the users use-case.

Also worth pointing out that .property() isn’t the only thing that sets the object to a new one; .throw() and .ownPropertyDescriptor() do this too - and potentially more will. We’ll need a solution that works for all of these I believe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fluent assertions - how to properly chain after a type check
So your example will change to: SomeObject.Should() .BeAssignableTo<OtherObject>() .Which.SomeStringProperty.Should().StartWith("whatever");.
Read more >
Expect / Should - Chai Assertion Library
Negates all assertions that follow in the chain. expect(function () {}).to.not.throw(); expect({a: 1}).to.not.have.property('b'); expect([1, ...
Read more >
Testing Arrays and Objects with Chai.js | by Titus Stone
When it comes to testing arrays and objects with Chai.js sometimes the selection of flagging properties and assertions becomes confusing.
Read more >
BDD - Expect
The include and contain assertions can be used as either property based language chains or as methods to assert the inclusion of an...
Read more >
Optional Chaining vs Assertion Operator in TypeScript
getName()”, we first have to check whether data is not null and not ... Non-null assertion operator will not null guard your property...
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