Added fail() method to Should and Expect interfaces #356

Merged
merged 6 commits into from Feb 10, 2015

Projects

None yet

3 participants

@Soviut
Soviut commented Jan 30, 2015

Should and Expect now have fail() methods like Assert does.

should.fail(0, 1, 'failure message');
expect().fail('failure message');

Please pay special attention to the Expect implementation as I wasn't totally certain how all of it worked. It may not be fully chainable, though it's not really intended to be.

@keithamus keithamus commented on an outdated diff Jan 30, 2015
@@ -21,7 +21,7 @@
},
"main": "./index",
"scripts": {
- "test": "make test"
+ "test": "mocha --require ./test/bootstrap"
@keithamus
keithamus Jan 30, 2015 Member

This looks like an extraneous change. Could you rebase it out please?

@keithamus keithamus and 1 other commented on an outdated diff Jan 30, 2015
lib/chai/core/assertions.js
@@ -215,6 +215,16 @@ module.exports = function (chai, _) {
Assertion.addChainableMethod('contains', include, includeChainingBehavior);
Assertion.addChainableMethod('includes', include, includeChainingBehavior);
+ // TODO: Add jsdoc
+
+ function assertFail (msg) {
+ if (msg) flag(this, 'message', msg);
+ var obj = flag(this, 'object');
+ throw new chai.AssertionError(msg, {}, obj);
+ }
+
+ Assertion.addMethod('fail', assertFail);
+
@keithamus
keithamus Jan 30, 2015 Member

Curious why you added it as a method for all assertions, rather than just tacking it onto expect. I was thinking you would just add expect.fail = function much like you did with should.fail

@Soviut
Soviut Jan 30, 2015

I was having issues adding fail directly to expect because it's a function. Are there any examples of how functions are directly added to expect anywhere else in the code base?

@Soviut
Soviut Feb 10, 2015

@keithamus Can you advise on how to add a fail method directly to expect? I can't find concrete examples and it seems somewhat impossible because expect is a function, unless I'm misunderstanding the architecture.

@keithamus
keithamus Feb 10, 2015 Member

Shouldn't matter that expect is a function. Fail can just be added as a property of it. See the example below:

> function x() {}
undefined
> x.fail = function () {}
[Function]
> x
{ [Function: x] fail: [Function] }
> x.fail
[Function]
>
@keithamus
Member

Some good work so far @Soviut. Couple of notes above which I'd like to see addressed - if you could be so kind. Also, as you've noted in the code, it'd be great to get some documentation put in here too 😄

@keithamus keithamus and 1 other commented on an outdated diff Jan 30, 2015
lib/chai/core/assertions.js
@@ -215,6 +215,16 @@ module.exports = function (chai, _) {
Assertion.addChainableMethod('contains', include, includeChainingBehavior);
Assertion.addChainableMethod('includes', include, includeChainingBehavior);
+ // TODO: Add jsdoc
+
+ function assertFail (msg) {
+ if (msg) flag(this, 'message', msg);
+ var obj = flag(this, 'object');
+ throw new chai.AssertionError(msg, {}, obj);
+ }
@keithamus
keithamus Jan 30, 2015 Member

Another point to make here; for API consistency I think should.fail and expect.fail should work exactly the same as assert.fail. Method signature and all.

@Soviut
Soviut Jan 30, 2015

I agree with the consistency. I chose this route because I was trying to be consistent with the interface. Namely, that values are usually passed along the chain with expect. I'll try reworking it.

@Soviut
Soviut commented Feb 10, 2015

@keithamus There is now an expect.fail(0, 1, 'message'); method. Does this look good now?

@keithamus
Member

Looks fantastic @Soviut.

Could you add a little bit of documentation above each of the methods in the code? This way it'll get carried over onto the site documentation when the site is regenerated.

@Soviut
Soviut commented Feb 10, 2015

Will do.

@Soviut
Soviut commented Feb 10, 2015

@keithamus documentation added. I basically just copied what was already in the assert interface.

@keithamus
Member

Perfect. Consider it merged 👍

@keithamus keithamus merged commit 9b25d0c into chaijs:master Feb 10, 2015

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@sieira
sieira commented Aug 22, 2015

Sorry for bothering you, but I can't manage to find the docs for this...

@keithamus
Member

@sieira the website is outdated right now. We're working on it. chaijs/chai-docs#74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment