Skip to content

Commit

Permalink
Add better type of check to toBeObject matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Jul 30, 2018
1 parent a45463f commit 230d071
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"dependencies": {
"chalk": "^2.3.0",
"expect": "^22.1.0",
"jest-get-type": "^22.4.3",
"jest-matcher-utils": "^22.0.0"
},
"lint-staged": {
Expand Down
7 changes: 7 additions & 0 deletions src/matchers/toBeObject/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Expected value to be an object, received:
<red>0</>"
`;
exports[`.toBeObject fails when not given an object: 1,2,3 1`] = `
"<dim>expect(</><red>received</><dim>).toBeObject(</><dim>)</>
Expected value to be an object, received:
<red>[1, 2, 3]</>"
`;
exports[`.toBeObject fails when not given an object: NaN 1`] = `
"<dim>expect(</><red>received</><dim>).toBeObject(</><dim>)</>
Expand Down
18 changes: 12 additions & 6 deletions src/matchers/toBeObject/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ describe('.toBeObject', () => {
expect({}).toBeObject();
});

each([[false], [''], [0], [() => {}], [undefined], [NaN]]).test('fails when not given an object: %s', given => {
expect(() => expect(given).toBeObject()).toThrowErrorMatchingSnapshot();
});
each([[false], [''], [0], [() => {}], [undefined], [NaN], [[1, 2, 3]]]).test(
'fails when not given an object: %s',
given => {
expect(() => expect(given).toBeObject()).toThrowErrorMatchingSnapshot();
}
);
});

describe('.not.toBeObject', () => {
each([[false], [''], [0], [() => {}], [undefined], [NaN]]).test('passes when not given an object: %s', given => {
expect(given).not.toBeObject();
});
each([[false], [''], [0], [() => {}], [undefined], [NaN], [[1, 2, 3]]]).test(
'passes when not given an object: %s',
given => {
expect(given).not.toBeObject();
}
);

test('fails when given an object', () => {
expect(() => expect({}).not.toBeObject()).toThrowErrorMatchingSnapshot();
Expand Down
4 changes: 3 additions & 1 deletion src/matchers/toBeObject/predicate.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default expected => typeof expected === 'object';
import getType from 'jest-get-type';

export default expected => getType(expected) === 'object';
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,10 @@ jest-get-type@^22.1.0:
version "22.1.0"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.1.0.tgz#4e90af298ed6181edc85d2da500dbd2753e0d5a9"

jest-get-type@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.4.3.tgz#e3a8504d8479342dd4420236b322869f18900ce4"

jest-haste-map@^23.0.1:
version "23.0.1"
resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-23.0.1.tgz#cd89052abfc8cba01f560bbec09d4f36aec25d4f"
Expand Down

0 comments on commit 230d071

Please sign in to comment.