Permalink
Browse files

Add explicit failure through `expect().fail()`

Sometimes, you just need to signal failure, and the logic doesn't make
sense to encapsulate within `.to.be.not` type chaining.  This commit
adds an explicit `fail()` assertion which forces failure.  Since there's
no context to expect() with which to construct an 'expected ... to be
...` type failure message, also let a message be specified with an
optional first argument: `fail("custom message")`.
  • Loading branch information...
1 parent e0de582 commit 08484a644cf729c580e2ff3f12d977d24d81dcbe @yourcelf yourcelf committed May 13, 2012
Showing with 29 additions and 0 deletions.
  1. +6 −0 README.md
  2. +11 −0 expect.js
  3. +12 −0 test/expect.js
View
@@ -162,6 +162,12 @@ expect(0).to.be.below(3);
expect(1).to.be.lessThan(3);
```
+**fail**: explicitly forces failure.
+
+```js
+expect().fail()
+expect().fail("Custom failure message")
+
## Using with a test framework
For example, if you create a test suite with
View
@@ -459,6 +459,17 @@
return this;
};
+ /**
+ * Assert a failure.
+ *
+ * @param {String ...} custom message
+ * @api public
+ */
+ Assertion.prototype.fail = function (msg) {
+ msg = msg != null ? msg : "explicit failure";
+ this.assert(false, msg, msg);
+ return this;
+ };
/**
* Function bind implementation.
View
@@ -527,4 +527,16 @@ describe('expect', function () {
}, "expected 5 to be below 4");
});
+ it('should fail with `fail`', function () {
+ err(function () {
+ expect().fail();
+ }, "explicit failure");
+ });
+
+ it('should fail with `fail` and custom message', function () {
+ err(function () {
+ expect().fail("explicit failure with message");
+ }, "explicit failure with message");
+ });
+
});

0 comments on commit 08484a6

Please sign in to comment.