最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Excluding missing keys from JSON comparison - Stack Overflow

matteradmin6PV0评论

I have two JSON documents that I want to assert equal for Jest unit testing. They should be equal, except the second one has one more key: _id.

Example:

doc1.json
{
    username: 'someone',
    firstName: 'some',
    lastName: 'one',
}

doc2.json
{
    _id: '901735013857',
    username: 'someone',
    firstName: 'some',
    lastName: 'one',
}

My code currently looks like this:

const result = await activeDirectoryUserCollection
    .findOne({username: testUser1.username});
expect(result).toBe(testUser1);

Obviously this gives the error that they are not equal, just because of that one value.

I'm looking for an alternative to .toBe() that doesn't pletely pare the docs, but checks if one is a subset of another. (or something like that).

Alternatively I would appreciate someone to point me to a module that could help me out.

I have two JSON documents that I want to assert equal for Jest unit testing. They should be equal, except the second one has one more key: _id.

Example:

doc1.json
{
    username: 'someone',
    firstName: 'some',
    lastName: 'one',
}

doc2.json
{
    _id: '901735013857',
    username: 'someone',
    firstName: 'some',
    lastName: 'one',
}

My code currently looks like this:

const result = await activeDirectoryUserCollection
    .findOne({username: testUser1.username});
expect(result).toBe(testUser1);

Obviously this gives the error that they are not equal, just because of that one value.

I'm looking for an alternative to .toBe() that doesn't pletely pare the docs, but checks if one is a subset of another. (or something like that).

Alternatively I would appreciate someone to point me to a module that could help me out.

Share Improve this question edited Jul 9, 2019 at 21:20 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jul 9, 2019 at 15:17 Tea_Lover_418Tea_Lover_418 3163 silver badges12 bronze badges 2
  • 1 can you not just delete result._id and delete testUser1._id and then expect? – Aritra Chakraborty Commented Jul 9, 2019 at 15:19
  • You're right, I was just making it more plex than necessary. Thank you! Also if anyone ever finds this, when paring JSON you should use .toEqual() instead of .toBe(). – Tea_Lover_418 Commented Jul 9, 2019 at 15:27
Add a ment  | 

3 Answers 3

Reset to default 3

I would checkout Lodash module's .isMatch function. It performs a partial deep parison between object and source to determine if object contains equivalent property values. https://lodash./docs/4.17.11#isMatch

Example:

var object = { 'a': 1, 'b': 2 };

_.isMatch(object, { 'b': 2 });
// => true


_.isMatch(object, { 'b': 1 });
// => false

You can iterate through one Object and use the key to assert value in both Objects. Read More for...in

const result = await activeDirectoryUserCollection
    .findOne({username: testUser1.username});

for (const prop in testUser1) {
   if (testUser1[prop]) {
     expect(result[prop]).toBe(testUser1[prop]); // or use .toEqual
   }
}

I don't think you need to look outside jest for this. You can use expect.objectContaining(), which is described in the docs as:

expect.objectContaining(object) matches any received object that recursively matches the expected properties. That is, the expected object is a subset of the received object. Therefore, it matches a received object which contains properties that are present in the expected object.

You could use it like:

test('objects', () => {
    expect(doc2).toEqual(
        expect.objectContaining(doc1)
    );   
  });
Post a comment

comment list (0)

  1. No comments so far