-
-
Notifications
You must be signed in to change notification settings - Fork 698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make assertions (such as .exist) callable #56
Comments
This behavior is by design and will not be changed, for a few reasons. I think what might help you is to explain our guidelines for how we construct each assertion. I am hoping that you will find this informative enough that helps you to adapt. Please let us know if you have any questions after this. Hope this helps: Guidelines1. An assertion will be a method when parameter(s) are needed, a property when a paramater(s) are not needed.Though some properties don't do anything (such as Few caveats:
2. All assertions must permit the construction of an infinite chain.In your scenario you mentioned that you would not be able to chain unless you called the function. Coincidentally, there is an // 1 exists and is a number
var n = 1;
expect(n).to.exist.and.be.a('number');
// fn throws an instanceof ReferenceError with an error message that does not match regex /good function/
var fn = function () { throw ReferenceError('bad function'); };
expect(fn).to.throw(ReferenceError).and.not.throw(/good function/); |
Oh, so there is an I guess I just have to suck it up until I get used to it ;). Writing tests in a proper red-green-refactor way should help with that as well. Thanks for your time :). |
One problem with this approach is that many editors/linters warns against using expressions as statements. It's quite useful to not have to disable such settings but sometimes they can only be set per project so one can't just disable them for tests. |
@mzgol and others, there is a plugin (dirty-chai) which overrides this behaviour, making all property-assertions method-assertions. If you desire this behaviour (to appease linting gods) then I suggest you use this. |
@keithamus Thanks for the link, I'll try it! I think remarks in https://www.npmjs.com/package/dirty-chai#function-form-for-terminating-assertion-properties are valid, especially the part that mistyping one thing makes the test pass whereas the only correct approach when writing tests is that making a mistake means failing the test. One can achieve it either by using the function form (you'll get Because of that, I think it's not just about personal taste and that it's something that should be supported by core. But if that's out of the picture, I'll try the plugin. |
@mzgol I agree, trust me we've been here before. We tried to support both but it didn't work well, so we reverted. At this juncture we'd need to make a breaking change for method-style-assertions - and it is something that polarises the community. Not something we're willing to do lightly, especially as a plugin like dirty-chai does the job. For context/history, feel free to read #41, #297, #306. |
OK, I understand the problems with maintaining back compat. This is unfortunate but the world isn't ideal. I'll just try to use BTW, perhaps when ES6 Proxies become implemented throwing on accessing non-existing assertions could be implemented? In this way at least in Chrome/Firefox we'd get a failure on mismatch which will suffice in most cases. |
As an aside, Chai has ~650k downloads a month, while dirty-chai has ~2k, chai-missing-assertions has ~5k. Combined they make up just over 1% of all chai downloads. While I don't think those numbers are truly reflective of users who want to have method assertions, I think it can be used as a rough barometer. If the majority of chai users felt property assertions were that bad I'd expect to see at the very least double digit numbers, if not closer to, say, 50% of all chai downloads. ES6 Proxies are a suitable idea. But we should discuss this in a new issue, rather than continuing discussion here. |
Sure. I've just created #407 for that. |
I am currently writing some tests using the expect-version of chai. And it is going fine, except there is a readability issue.
Take the following code:
This is perfectly valid, and will test as expected, since the assertion is done inside the getter
.exist
.But to my eye, there is no action taken here. I would much prefer the line to be:
This can be achieved by simply changing the
.exist
definition from:to:
The only drawback is that you cannot chain directly on
.exist
(likeexpect(a).to.exist.ok
), but since there is no.and
and the tests does not break after doing the change above, I don't see that as an issue.So the question is, is this only an issue in my mind? And should I spend time on making a proper pull request?
The text was updated successfully, but these errors were encountered: