test array to include an object with a certain key: value
See original GitHub issueHello,
As the title says we found our selves in a situation where we need to check if an array contains an element that one of his properties equals to something.
We tried and search for a couple of hours until we desided to raise an issue about it. We were able to solve the problem like this:
const playersRegs = created.gameLog.filter(e => e.nameType === eventTypes.PlayerReg);
expect(playersRegs).to.have.lengthOf(1);
but we were hoping for something a bit like:
expect(created.gameLog).to.include.something.that.has.property('nameType', eventTypes.PlayerReg);
if we missed anything please let us know and if we didnt please consider adding this functionality 😉
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Test array to include an object with a certain key: value
You can use .some() instead: const hasPlayersReg = created.gameLog.some(e => e.nameType === eventTypes.
Read more >Check if an array of objects contains a certain key-value in ...
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true...
Read more >How to check if array includes a value in JavaScript?
Code Recipe to check if an array includes a value in JavaScript using ES6. ... operator to loop over an object to check...
Read more >How to check if an array includes an object in JavaScript
Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array ......
Read more >Check if Array Contains an Object in JavaScript | bobbyhadz
Check if each object contains a property with the specified value. `Array.some()` will return `true` if the object is contained in the array....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@keithamus Is there a solution available through
chai
now?Makes sense. In a case like this one, I can understand accepting decreased precision in exchange for increased maintainability.
A couple of weeks ago I was thinking about the level of effort of making the
chai-things
plugin compatible with Chai v4. The current stance is we’d wait for #585 to be resolved before fixingchai-things
, but that feels like a long ways away to me. Given that the plugin is basically unusable at the moment, I think it might be worth it to fix the plugin directly.