forked from jest-community/jest-extended
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a '.toBeEmptyObject()' custom matcher. (jest-community#255)
- Loading branch information
1 parent
e888c8d
commit 35812e3
Showing
7 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/matchers/toBeEmptyObject/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\"}</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters