Skip to content

Commit

Permalink
Allow fail to work in try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
thehale committed Oct 9, 2023
1 parent 1f88101 commit d0474ee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/matchers/fail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function fail(_, message) {
this.dontThrow();
return {
pass: false,
message: () => (message ? message : 'fails by .fail() assertion'),
Expand Down
5 changes: 0 additions & 5 deletions test/matchers/__snapshots__/fail.test.js.snap

This file was deleted.

17 changes: 12 additions & 5 deletions test/matchers/fail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import * as matcher from 'src/matchers/fail';
expect.extend(matcher);

describe('.fail', () => {
test('fails without message', () => {
expect(() => expect().fail()).toThrowErrorMatchingSnapshot();
xtest('fails without message', () => {
expect().fail(); // This should fail!
});
test('fails with message', () => {
expect(() => expect().fail("This shouldn't fail!")).toThrowErrorMatchingSnapshot();
xtest('fails with message', () => {
expect().fail('This should fail!');
});
xtest('fails when invoked in a try/catch', () => {
try {
expect().fail();
} catch (error) {
expect('this assertion').toBe('not checked');
}
});
});

Expand All @@ -16,6 +23,6 @@ describe('.not.fail', () => {
expect().not.fail();
});
test('does not fail with message', () => {
expect().not.fail('this should fail!');
expect().not.fail('this should not fail!');
});
});

0 comments on commit d0474ee

Please sign in to comment.