最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest - check that item does not contain object with some property - Stack Overflow

programmeradmin1浏览0评论

This is my response I need to check:

[
  { _id: 'dislike', count: 1 },
  { _id: 'hate', count: 1 },
  { _id: 'trivial', count: 1 }
]

Checking the existing values is easy:

expect(response.data).toContainEqual({ _id: 'trivial', count: 1 });
expect(response.data).toContainEqual({ _id: 'dislike', count: 1 });
expect(response.data).toContainEqual({ _id: 'hate', count: 1 });

But I need to check that the array does not contain object with { _id: 'neutral' }. Following test looked successful because the test passed:

expect(response.data).not.toContain({ _id: 'neutral', count: 0 });

But I wanted to ensure that it does what I expect so I added a test that shall fail. But it does not:

expect(response.data).not.toContain({ _id: 'hate', count: 1 });

Can you see? Both tests toContainEqual and not.toContain are valid. How can I ensure that the array does not hold object with a property _id: 'neutral'?

This is my response I need to check:

[
  { _id: 'dislike', count: 1 },
  { _id: 'hate', count: 1 },
  { _id: 'trivial', count: 1 }
]

Checking the existing values is easy:

expect(response.data).toContainEqual({ _id: 'trivial', count: 1 });
expect(response.data).toContainEqual({ _id: 'dislike', count: 1 });
expect(response.data).toContainEqual({ _id: 'hate', count: 1 });

But I need to check that the array does not contain object with { _id: 'neutral' }. Following test looked successful because the test passed:

expect(response.data).not.toContain({ _id: 'neutral', count: 0 });

But I wanted to ensure that it does what I expect so I added a test that shall fail. But it does not:

expect(response.data).not.toContain({ _id: 'hate', count: 1 });

Can you see? Both tests toContainEqual and not.toContain are valid. How can I ensure that the array does not hold object with a property _id: 'neutral'?

Share Improve this question asked Apr 18, 2020 at 20:43 Leos LiterakLeos Literak 9,47422 gold badges95 silver badges171 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

The matcher toContain checks if an item is in the array by using the strict equality check. That means that if you don't pass the reference to the object you are trying to test, it will always return false.

Thus, if you check:

expect(response.data).toContain({ _id: 'trivial', count: 1 });

It will fail, because, even though your response.data contains an object with those same attributes and values, it's not the same object (it does not point to the same reference).

That's the reason why:

expect(response.data).not.toContain({ _id: 'neutral', count: 0 });

Will always return true.

If you want to check for a specific structure and values, you should use the toContainEqual matcher:

expect(response.data).not.toContainEqual({ _id: 'hate', count: 1 });

Also, if you want to check the whole structure and values of your response data, you can simply use the toEqual matcher:

expect(response.data).toEqual([
    { _id: 'dislike', count: 1 },
    { _id: 'hate', count: 1 },
    { _id: 'trivial', count: 1 }
]);

This will check that response.data has exactly the structure and values you pass as a parameter. For extension, it is checking that it does not contain any other values.

You could do it with Array.prototype.some()

expect(response.data.some(({_id}) => _id === "neutral")).toBe(false)

Array.prototype.some() returns a boolean true when the condition is first met, and false if not.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

发布评论

评论列表(0)

  1. 暂无评论