Skip to content

Commit 9b50fe4

Browse files
committedSep 24, 2023
fix!: isObject now correctly fails for null and undefined
`isObject` was too loose in its definition of what an object is. With this change, classes, maps, sets, arrays, `null` and `undefined` are no longer considered objects.
1 parent a5aaddd commit 9b50fe4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed
 

‎src/expectation/it.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
isEqual,
44
isMatchWith,
55
isObjectLike,
6+
isPlainObject,
67
isUndefined,
78
omitBy,
89
} from 'lodash';
@@ -136,8 +137,12 @@ const isObject = <T extends object, K extends DeepPartial<T>>(
136137
partial?: K
137138
): TypeMatcher<T> =>
138139
matches(
139-
(actual) =>
140-
isMatchWith(actual, partial || {}, (argValue, partialValue) => {
140+
(actual) => {
141+
if (!isPlainObject(actual)) {
142+
return false;
143+
}
144+
145+
return isMatchWith(actual, partial || {}, (argValue, partialValue) => {
141146
if (isMatcher(partialValue)) {
142147
return partialValue.matches(argValue);
143148
}

‎src/expectation/matcher.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,16 @@ describe('It', () => {
599599
});
600600

601601
describe('isObject', () => {
602+
it('should not match non objects', () => {
603+
expect(It.isObject().matches('not an object')).toBeFalsy();
604+
expect(It.isObject().matches([])).toBeFalsy();
605+
expect(It.isObject().matches(null)).toBeFalsy();
606+
expect(It.isObject().matches(undefined)).toBeFalsy();
607+
expect(It.isObject().matches(new (class {})())).toBeFalsy();
608+
});
609+
602610
it('should match any object with empty object', () => {
611+
expect(It.isObject().matches({})).toBeTruthy();
603612
expect(
604613
It.isObject().matches({
605614
foo: 'bar',

0 commit comments

Comments
 (0)
Please sign in to comment.