Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(objectContaining): Fixed a bug where ObjectContaining matched with non-object values. #15462

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ test('ArrayNotContaining throws for non-arrays', () => {
test('ObjectContaining matches', () => {
const foo = Symbol('foo');
for (const test of [
objectContaining({}).asymmetricMatch('jest'),
objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),
objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),
objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({
Expand Down Expand Up @@ -228,7 +227,6 @@ test('ObjectContaining matches defined properties', () => {
test('ObjectContaining matches prototype properties', () => {
const prototypeObject = {foo: 'bar'};
let obj;

if (Object.create) {
obj = Object.create(prototypeObject);
} else {
Expand All @@ -247,6 +245,18 @@ test('ObjectContaining throws for non-objects', () => {
);
});

test('ObjectContaining does not match when non-objects are passed to the expect function as arguments', () => {
for (const test of [
objectContaining({}).asymmetricMatch('jest'),
objectContaining({}).asymmetricMatch(10),
objectContaining({}).asymmetricMatch(false),
objectContaining({}).asymmetricMatch(undefined),
objectContaining({}).asymmetricMatch([]),
]) {
jestExpect(test).toEqual(false);
}
});

test('ObjectContaining does not mutate the sample', () => {
const sample = {foo: {bar: {}}};
const sample_json = JSON.stringify(sample);
Expand All @@ -259,8 +269,6 @@ test('ObjectNotContaining matches', () => {
const foo = Symbol('foo');
const bar = Symbol('bar');
for (const test of [
objectContaining({}).asymmetricMatch(null),
objectContaining({}).asymmetricMatch(undefined),
objectNotContaining({[foo]: 'foo'}).asymmetricMatch({[bar]: 'bar'}),
objectNotContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),
objectNotContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),
Expand Down
9 changes: 9 additions & 0 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,22 @@ class ObjectContaining extends AsymmetricMatcher<
}

asymmetricMatch(other: any) {
// Ensures that the argument passed to the objectContaining method is an object
if (typeof this.sample !== 'object') {
throw new TypeError(
`You must provide an object to ${this.toString()}, not '${typeof this
.sample}'.`,
);
}

// Ensures that the argument passed to the expect function is an object
// This is necessary to avoid matching of non-object values
// Arrays are a special type of object, but having a valid match with a standard object
// does not make sense, hence we do a simple array check
if (typeof other !== 'object' || Array.isArray(other)) {
return false;
}

let result = true;

const matcherContext = this.getMatcherContext();
Expand Down
Loading