Is it possible to test that some object contains a certain sub-object using jest?
For example, we may want to test object
{a: {x: 1, y: 2},
b: 'SomeString'}
contains sub-object
{ a: {x: 1} }
In jasmine this is possible using the jasmine.objectContaining() function.
it('can match nested partial objects', function ()
{
var joc = jasmine.objectContaining;
expect({
a: {x: 1, y: 2},
b: 'SomeString'
}).toEqual(joc({
a: joc({ x: 1})
}));
});
we should definitely have something like that in jest.
the API i was thinking about is
expect({a: {b: 1, c: 2}, d: 3, e: 4}).toContainSubObject({a: {b: 1}, e: 4});
the only thing that i'm not sure is the name of the matcher. toPartiallyMatch, toEqualPartially?
I think we could use .toMatch for this kind of stuff. It's basically the same as matching a string against another string/regex, no?
Thanks for the response!
I was initially thinking the same as you @dmitriiabramov, the name toContainSubset is similar to the method used by chai-subset. I personally found this a very clear API.
If I understand you correctly @cpojer, then you propose to add some extra functionality to the .toMatch function so that it also accepts objects in addition to regular expressions. I agree that this use case is very similar to regex matching so it would make sense to add this functionality in .toMatch.
Let's do toMatchObject. I think toMatch would be appropriate but I don't want to overload it. Do you want to send a PR for this?
You can simply use jasmine.objectContaining in the new matcher for now. We'll swap out the internals eventually :)
I would love to give it a try! Expect a PR fron me soon:)
I have been trying to implement this feature and I ran into a problem with jasmine.objectContaining. It seems that this only performs a match of the keys/values on one level of the object. However, I think the desired behavior would be to have a recursive matching on multiple levels of the object.
Jasmine has solved this by making the developer add multiple nested objectContaining statements:
it('can match nested partial objects', function ()
{
var joc = jasmine.objectContaining;
expect({
a: {x: 1, y: 2},
b: 'hi'
}).toEqual(joc({
a: joc({ x: 1})
}));
});
In order to implement the code snippet below in jest we would have to implement a recursive function:
expect({
a: {x: 1, y: 2},
b: 'hi'
}).toMatchObject({
a: { x: 1}
});
Do you guys have some suggestions on how I could best implement this?
We'll continue the conversation in the PR.
Most helpful comment
I have been trying to implement this feature and I ran into a problem with
jasmine.objectContaining. It seems that this only performs a match of the keys/values on one level of the object. However, I think the desired behavior would be to have a recursive matching on multiple levels of the object.Jasmine has solved this by making the developer add multiple nested
objectContainingstatements:In order to implement the code snippet below in jest we would have to implement a recursive function:
Do you guys have some suggestions on how I could best implement this?