Skip to content

Commit

Permalink
feat: add a '.toBeEmptyObject()' custom matcher. (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansal7014 authored Oct 6, 2021
1 parent e888c8d commit 35812e3
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toBeInteger()](#tobeinteger)
- [Object](#object)
- [.toBeObject()](#tobeobject)
- [.toBeEmptyObject()](#tobeemptyobject)
- [.toContainKey(key)](#tocontainkeykey)
- [.toContainKeys([keys])](#tocontainkeyskeys)
- [.toContainAllKeys([keys])](#tocontainallkeyskeys)
Expand Down Expand Up @@ -589,6 +590,17 @@ test('passes when value is an integer', () => {

### Object

#### .toBeEmptyObject()

Use `.toBeEmptyObject` when checking if a value is an empty `Object`.

```js
test('passes when value is an empty object', () => {
expect({}).toBeEmptyObject();
expect({ a: 'hello' }).not.toBeEmptyObject();
});
```

#### .toBeObject()

Use `.toBeObject` when checking if a value is an `Object`.
Expand Down
15 changes: 15 additions & 0 deletions src/matchers/toBeEmptyObject/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeEmptyObject fails when given an empty object 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeEmptyObject()</>
Expected value to not be an empty object, received:
<red>{}</>"
`;
exports[`.toBeEmptyObject fails when not given an empty object 1`] = `
"<dim>expect(</><red>received</><dim>).toBeEmptyObject()</>
Expected value to be an empty object, received:
<red>{\\"property1\\": \\"something\\"}</>"
`;
26 changes: 26 additions & 0 deletions src/matchers/toBeEmptyObject/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeEmptyObject', 'received', '') +
'\n\n' +
'Expected value to not be an empty object, received:\n' +
` ${printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeEmptyObject', 'received', '') +
'\n\n' +
'Expected value to be an empty object, received:\n' +
` ${printReceived(received)}`;

export default {
toBeEmptyObject: expected => {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
}

return { pass: false, message: failMessage(expected) };
},
};
23 changes: 23 additions & 0 deletions src/matchers/toBeEmptyObject/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import matcher from './';

expect.extend(matcher);

describe('.toBeEmptyObject', () => {
test('passes when given an empty object', () => {
expect({}).toBeEmptyObject();
});

test('fails when not given an empty object', () => {
expect(() => expect({ property1: 'something' }).toBeEmptyObject()).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeEmptyObject', () => {
test('passes when not given an empty object', () => {
expect({ property1: 'something' }).not.toBeEmptyObject();
});

test('fails when given an empty object', () => {
expect(() => expect({}).not.toBeEmptyObject()).toThrowErrorMatchingSnapshot();
});
});
3 changes: 3 additions & 0 deletions src/matchers/toBeEmptyObject/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getType } from 'jest-get-type';

export default expected => getType(expected) === 'object' && Object.keys(expected).length === 0;
11 changes: 11 additions & 0 deletions src/matchers/toBeEmptyObject/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import predicate from './predicate';

describe('toBeEmptyObject Predicate', () => {
test('returns true when given an empty object', () => {
expect(predicate({})).toBe(true);
});

test('returns false when given a non-empty object', () => {
expect(predicate({ property1: 'something' })).toBe(false);
});
});
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,5 +716,10 @@ declare namespace jest {
* @param {String | RegExp} message
*/
toThrowWithMessage(type: Function, message: string | RegExp): any;

/**
* Use `.toBeEmptyObject` when checking if a value is an empty `Object`.
*/
toBeEmptyObject(): R;
}
}

0 comments on commit 35812e3

Please sign in to comment.