Skip to content

Commit

Permalink
Add toBeFalse (#89)
Browse files Browse the repository at this point in the history
* Add toBeFalse predicate

* Add toBeFalse matcher

* Update docs and add contributor

* Add toBeFalse to matchers index
  • Loading branch information
mjmiles authored and mattphillips committed Oct 27, 2017
1 parent 3fdb024 commit 6b27c74
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@
"test"
]
},
{
"login": "mjmiles",
"name": "mjmiles",
"avatar_url": "https://avatars2.githubusercontent.com/u/33098064?v=4",
"profile": "https://github.com/mjmiles",
"contributions": [
"code",
"doc"
]
},
{
"login": "garmeeh",
"name": "Gary Meehan",
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ test('is jest cool', () => {

### .toBeFalse()

_Note: Currently unimplemented_

Use `.toBeFalse` when checking a value is equal (===) to `false`.

```js
Expand Down
2 changes: 2 additions & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import toBeEven from './toBeEven';
import toBeTrue from './toBeTrue';
import toBeFalse from './toBeFalse';
import toContainValue from './toContainValue';
import toContainValues from './toContainValues';
import toEqualCaseInsensitive from './toEqualCaseInsensitive';
Expand Down Expand Up @@ -27,6 +28,7 @@ import toInclude from './toInclude';
export default [
toBeEven,
toBeTrue,
toBeFalse,
toContainValue,
toContainValues,
toEqualCaseInsensitive,
Expand Down
17 changes: 17 additions & 0 deletions src/matchers/toBeFalse/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeFalse fails when given false 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeFalse(</><dim>)</>
Expected value to not be false received:
<red>false</>"
`;
exports[`.toBeFalse fails when not given false 1`] = `
"<dim>expect(</><red>received</><dim>).toBeFalse(</><dim>)</>
Expected value to be false:
<green>false</>
Received:
<red>true</>"
`;
28 changes: 28 additions & 0 deletions src/matchers/toBeFalse/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

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

const failMessage = received => () =>
matcherHint('.toBeFalse', 'received', '') +
'\n\n' +
'Expected value to be false:\n' +
` ${printExpected(false)}\n` +
'Received:\n' +
` ${printReceived(received)}`;

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

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

import matcher from './';

expect.extend(matcher);

describe('.toBeFalse', () => {
it('passes when given false', () => {
expect(false).toBeFalse();
});

it('fails when not given false', () => {
expect(() => expect(true).toBeFalse()).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeFalse', () => {
each([[true], [''], [0], [{}], [[]], [() => {}], [undefined], [null], [NaN]]).it(
'passes when not given false: %s',
given => {
expect(given).not.toBeFalse();
}
);

it('fails when given false', () => {
expect(() => expect(false).not.toBeFalse()).toThrowErrorMatchingSnapshot();
});
});
1 change: 1 addition & 0 deletions src/matchers/toBeFalse/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default expected => expected === false;
15 changes: 15 additions & 0 deletions src/matchers/toBeFalse/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import each from 'jest-each';
import predicate from './predicate';

describe('toBeFalse Predicate', () => {
it('returns true when given false', () => {
expect(predicate(false)).toBe(true);
});

each([[true], [''], [0], [{}], [[]], [() => {}], [undefined], [null], [NaN]]).it(
'returns false when given: %s',
given => {
expect(predicate(given)).toBe(false);
}
);
});

0 comments on commit 6b27c74

Please sign in to comment.