This check used to pass:
expect(array).toContain(value)
Array:
[
{"_t":"user","id":1073970419,"email":"[email protected]","name":"Spectator"},
{"_t":"user","id":4464992042,"email":"[email protected]","name":"Collaborator"},
{"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"}
]
Value:
{"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"}
But no longer passes. Whats the new way to write the same test?
isArray() method is used to check if an object is an array. The Array. isArray() method returns true if an object is an array, otherwise returns false .
Array ChecksarrayContaining method to check the content of the array. For example, we can write: describe("jasmine. arrayContaining", function () { let foo; beforeEach(function () { foo = [1, 2, 3]; }); it("matches arrays with some of the values", function () { expect(foo).
There are various methods to check an array includes an object or not. Using includes () Method: If array contains an object/element can be determined by using includes () method. This method returns true if the array contains the object/element else return false.
In this post, we will look at different ways to check if every element of the first array exists in the second array. We can use the Array.prototype.every () method (which was introduced in ES5) to check whether all elements in the array pass the test implemented by the provided function.
The function we passed to the Array.find method gets called with each array element, until it returns a truthy value or iterates over all array elements.
The Array.findIndex method is very similar to the Array.find method, but returns the index of the element that satisfies the conditional check, and not the element itself. The Array.findIndex method calls its callback function with each element in the array until a truthy value is returned or the values in the array are exhausted.
The syntax you need is:
const obj = {"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"};
expect(array).toContain(jasmine.objectContaining(obj));
See fiddle: https://jsfiddle.net/bblackwo/4o5u5Lmo/16/
It won't contain that object (remember, two objects with the same properties are not the same object for the purposes of equality), so toContain
will never pass.
You need to use another test, like toEqual
or (if you only want to check for a subset of properties), toEqual
combined with jasmine.objectContaining
.
Here's the toEqual
example from the Jasmine documentation on that page:
describe("The 'toEqual' matcher", function() {
it("works for simple literals and variables", function() {
var a = 12;
expect(a).toEqual(12);
});
it("should work for objects", function() {
var foo = {
a: 12,
b: 34
};
var bar = {
a: 12,
b: 34
};
expect(foo).toEqual(bar);
});
});
Note now foo
equals bar
.
Here's their example using jasmine.objectContaining
:
describe("jasmine.objectContaining", function() {
var foo;
beforeEach(function() {
foo = {
a: 1,
b: 2,
bar: "baz"
};
});
it("matches objects with the expect key/value pairs", function() {
expect(foo).toEqual(jasmine.objectContaining({
bar: "baz"
}));
expect(foo).not.toEqual(jasmine.objectContaining({
c: 37
}));
});
// ...
});
Note how the object with several properties matches the partial object supplied to jasmine.objectContaining
.
@T.J.Crowder already explained precisely the problem. Just to help you a tad more, if you want to adapt your example, you'd need something like this:
var userA = {"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"}
array =
[
{"_t":"user","id":1073970419,"email":"[email protected]","name":"Spectator"},
{"_t":"user","id":4464992042,"email":"[email protected]","name":"Collaborator"},
userA
]
expect(array).toContain(userA);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With