Skip to content

Commit

Permalink
Add Pass & Fail matchers (jest-community#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamrchr authored Oct 11, 2021
1 parent ff02ff1 commit 61c92f8
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 2 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ test('passes when using an asymmetrical matcher', () => {

#### .pass(message)

_Note: Currently unimplemented_

Passing assertion.

Expand All @@ -193,7 +192,6 @@ expect().pass('should pass');

#### .fail(message)

_Note: Currently unimplemented_

Failing assertion.

Expand Down
5 changes: 5 additions & 0 deletions src/matchers/fail/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.fail fails with message 1`] = `"This shouldn't fail!"`;

exports[`.fail fails without message 1`] = `"fails by .fail() assertion"`;
10 changes: 10 additions & 0 deletions src/matchers/fail/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import predicate from './predicate';

const failMessage = message => {
if (message) return () => message;
else return () => 'fails by .fail() assertion';
};

export default {
fail: (expected, message) => ({ pass: predicate(), message: failMessage(message) }),
};
21 changes: 21 additions & 0 deletions src/matchers/fail/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import matcher from './';

expect.extend(matcher);

describe('.fail', () => {
test('fails without message', () => {
expect(() => expect().fail()).toThrowErrorMatchingSnapshot();
});
test('fails with message', () => {
expect(() => expect().fail("This shouldn't fail!")).toThrowErrorMatchingSnapshot();
});
});

describe('.not.fail', () => {
test('does not fail without message', () => {
expect().not.fail();
});
test('does not fail with message', () => {
expect().not.fail('this should fail!');
});
});
1 change: 1 addition & 0 deletions src/matchers/fail/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => false;
7 changes: 7 additions & 0 deletions src/matchers/fail/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import predicate from './predicate';

describe('fail Predicate', () => {
test('returns false', () => {
expect(predicate()).toBe(false);
});
});
4 changes: 4 additions & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import failMatcher from './fail';
import passMatcher from './pass';
import toBeAfterMatcher from './toBeAfter';
import toBeArrayMatcher from './toBeArray';
import toBeArrayOfSizeMatcher from './toBeArrayOfSize';
Expand Down Expand Up @@ -60,6 +62,8 @@ import toThrowWithMessageMatcher from './toThrowWithMessage';

// this is absolutely horrible, but all matchers are default exports of an object with the name of the matcher

export const fail = failMatcher.fail;
export const pass = passMatcher.pass;
export const toBeAfter = toBeAfterMatcher.toBeAfter;
export const toBeArray = toBeArrayMatcher.toBeArray;
export const toBeArrayOfSize = toBeArrayOfSizeMatcher.toBeArrayOfSize;
Expand Down
5 changes: 5 additions & 0 deletions src/matchers/pass/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.pass does not pass, has no message 1`] = `"passes by .pass() assertion"`;

exports[`.not.pass does not.pass, has no message 1`] = `"This should not pass!"`;
10 changes: 10 additions & 0 deletions src/matchers/pass/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import predicate from './predicate';

const passMessage = message => {
if (message) return () => message;
else return () => 'passes by .pass() assertion';
};

export default {
pass: (expected, message) => ({ pass: predicate(), message: passMessage(message) }),
};
21 changes: 21 additions & 0 deletions src/matchers/pass/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import matcher from './';

expect.extend(matcher);

describe('.pass', () => {
test('passes without message', () => {
expect().pass();
});
test('passes with message', () => {
expect().pass('this should pass!');
});
});

describe('.not.pass', () => {
test('does not pass, has no message', () => {
expect(() => expect().not.pass()).toThrowErrorMatchingSnapshot();
});
test('does not.pass, has no message', () => {
expect(() => expect().not.pass('This should not pass!')).toThrowErrorMatchingSnapshot();
});
});
1 change: 1 addition & 0 deletions src/matchers/pass/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => true;
7 changes: 7 additions & 0 deletions src/matchers/pass/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import predicate from './predicate';

describe('true Predicate', () => {
test('returns true', () => {
expect(predicate()).toBe(true);
});
});

0 comments on commit 61c92f8

Please sign in to comment.