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.

Cannot do `deep.nested` on an array, have to wrap it with an object

See original GitHub issue

This was a stackoverflow question - https://stackoverflow.com/q/53906051/1828637

He has a const payload = [{ id: 0 },{ id: 1 }] and wants to do the following:

expect(payload).to.nested.include({
  '[0].id': 0,
  '[1].id': 1
});

But it does not work. He has to wrap it to get this to work:

const wrappedPayload = { a: payload };
expect(wrappedPayload).to.nested.include({
  'a[0].id': 0,
  'a[1].id': 1
});

I think this is a bug.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
meebercommented, Feb 17, 2019

In terms of current functionality, I don’t think there’s a silver bullet. Some options:

// Option 1
const wrappedPayload = { a: payload };
expect(wrappedPayload).to.nested.include({
  'a[0].id': 0,
  'a[2].value': 'qux'
});

// Option 2
expect(payload).to.have.nested.property('[0].id', 0);
expect(payload).to.have.nested.property('[2].value', 'qux');

// Option 3
expect(payload).to.include.deep.ordered.members([
  { id: 0, value: 'foo' },
  { id: 1, value: 'bar' },
  { id: 2, value: 'qux' }
]);

In terms of possible changes to functionality to support this:

  1. Update the .include assertion to support .nested with array targets
  2. Update the .members assertion to support .nested
0reactions
Noitidartcommented, Mar 1, 2019

Thanks very very much for sharing this info @meeber !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose - Cannot Update update deeply nested Objects
So you see its: Category -> products[] -> items[] I want to update the Objects in items array in the database and set...
Read more >
Using ES6 To Destructure Deeply Nested Objects in ... - ITNEXT
Today, I will show you how to use ES6 to destructure nested objects, my friends, AND what's more, prevent the dreaded undefined error...
Read more >
How to Deep Copy Objects and Arrays in JavaScript
The usual methods of copying an object or array only make a shallow copy, so deeply-nested references are a problem. You need a...
Read more >
Accessing Nested Objects in JavaScript - DEV Community ‍ ‍
Let's take this nested object as an example. const name = user.personalInfo.name; const userCity = user.
Read more >
Safely Accessing Deeply Nested Values In JavaScript - Medium
Imagine we have a props objects, which might look something like the ... we want to access any deeply nested data we have...
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