Skip to content

Commit

Permalink
fix!: isObject now correctly fails for null and undefined
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
NiGhTTraX committed Sep 24, 2023
1 parent a5aaddd commit 9b50fe4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/expectation/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
isEqual,
isMatchWith,
isObjectLike,
isPlainObject,
isUndefined,
omitBy,
} from 'lodash';
Expand Down Expand Up @@ -136,8 +137,12 @@ const isObject = <T extends object, K extends DeepPartial<T>>(
partial?: K
): TypeMatcher<T> =>
matches(
(actual) =>
isMatchWith(actual, partial || {}, (argValue, partialValue) => {
(actual) => {
if (!isPlainObject(actual)) {
return false;
}

return isMatchWith(actual, partial || {}, (argValue, partialValue) => {
if (isMatcher(partialValue)) {
return partialValue.matches(argValue);
}
Expand Down
9 changes: 9 additions & 0 deletions src/expectation/matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,16 @@ describe('It', () => {
});

describe('isObject', () => {
it('should not match non objects', () => {
expect(It.isObject().matches('not an object')).toBeFalsy();
expect(It.isObject().matches([])).toBeFalsy();
expect(It.isObject().matches(null)).toBeFalsy();
expect(It.isObject().matches(undefined)).toBeFalsy();
expect(It.isObject().matches(new (class {})())).toBeFalsy();
});

it('should match any object with empty object', () => {
expect(It.isObject().matches({})).toBeTruthy();
expect(
It.isObject().matches({
foo: 'bar',
Expand Down

0 comments on commit 9b50fe4

Please sign in to comment.