-
-
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
Allows writing lint-friendly tests #297
Conversation
This makes the final assertion checks noop functions so that they can be formatted like function calls. This keeps linters (JSLint/JSHint) off our back for having an inop expression. Completes chaijs#41
The solution is much simpler and nicer compared to what I was originally imagining. Great job! |
@joshperry this change looks great! Thanks for working on this. Could you please also add some documentation around the methods, so that users can discover this feature (for example the documentation here: https://github.com/prodatakey/chai/blob/noopchainfunc/lib/chai/core/assertions.js#L178-L191) |
@@ -69,6 +69,10 @@ module.exports = function (_chai, util) { | |||
util.addChainableMethod(this.prototype, name, fn, chainingBehavior); | |||
}; | |||
|
|||
Assertion.addChainableNoop = function(name, fn) { | |||
util.addChainableMethod(this.prototype, name, function() { }, fn); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should create the NOOP
function as a file-wide variable and only reference it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't disagree... The only thing I'm wondering is, do we want to document this method for use by plugin authors? If so, perhaps we should just make a more idiomatic function for chainable assert terminators. addChainableAssertion
maybe?
Any progress @joshperry? It'd be sweet to see some added documentation, and @logicalparadox's suggestion implemented. Then we can look toward merging 😄 |
I am out of the country on vacation, I should be back somewhere that I can wrap this up by the middle of next week. Cheers! |
Apologies @joshperry. Enjoy your vacation! |
Was just writing up some documentation and wondered what you guys thought about having an optional
|
As far as I am concerned, I can already specify the message via However, I would appreciate so much if this PR got landed as soon as possible, and extra bells and whistles were left for later, for a different PR. My two cents. |
@bajtos Agreed. This wouldn't really work with the current assertion execution setup anyway. The assertion is thrown on property access and the function never gets called to set the message. If we wanted to provide a message parameter, there would need to be a substantial refactor of these assertions. |
@joshperry In addition to the documentation can you also write a snip for the ReleaseNotes that explains the impact of this change. You can see a previous example here. You can just post it as a comment here and I will make sure it makes it into the release notes when I publish. Thanks! Furthermore, FYI, this will be tagged as release |
Added some docs... Seems repetitive, but wasn't sure what else to put. @logicalparadox Definitely |
df2b2f4
to
bbd3aaf
Compare
No Op Function for Terminating Assertion PropertiesThe below list of assertions are property getters that assert immediately on access. Because of that, they were written to be used by terminating the assertion chain with a property access.
This syntax is definitely aesthetically pleasing but, if you are linting your test code, your linter will complain with an error something like "Expected an assignment or function call and instead saw an expression." Since the linter doesn't know about the property getter it assumes this line has no side-effects, and throws a warning in case you made a mistake. Squelching these errors is not a good solution as test code is getting to be just as important as, if not more than, production code. Catching syntactical errors in tests using static analysis is a great tool to help make sure that your tests are well-defined and free of typos. A better option was to provide a function-call form for these assertions so that the code's intent is more clear and the linters stop complaining about something looking off. This form is added in addition to the existing property access form and does not impact existing test code.
These forms can also be mixed in any way, these are all functionally identical:
The following assertions can now also be used in the function-call form:
Plugin AuthorsIf you would like to provide this function-call form for your terminating assertion properties, there is a new function to register these types of asserts. Instead of using |
a04ed74
to
bbd3aaf
Compare
@@ -185,12 +185,17 @@ module.exports = function (chai, _) { | |||
* expect(false).to.not.be.ok; | |||
* expect(undefined).to.not.be.ok; | |||
* expect(null).to.not.be.ok; | |||
* expect(null).to.not.be.ok; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks to be a duplicate line.
I have a few points which I'd personally like to see wrapped up on this:
With both of those fixed, plus my above comment about the duplicate line, I'd be very happy with this and would happily merge it in, pending @logicalparadox's veto. |
Thanks for the input, @keithamus. Agreed on both points. I added a few unit tests exercising the form in your first point and revised the wording in the docs. |
@logicalparadox I'm happy with this, I can merge if you're happy. @joshperry Thanks for putting the effort in, looks great! |
@joshperry thanks for your work, this is awesome. |
👍 |
Kudos ! Thanks for all, folks. |
Allows writing lint-friendly tests
🎉 |
@abenhamdine to quote @logicalparadox:
|
AWESOME! 🙇 |
✌️ |
I just found this and wanted to say thank you to @joshperry .. developers on my team have had issues when learning the assertions library and done things like Thank you! |
This makes the final assertion checks noop functions so that they can be formatted like function calls. This keeps linters (JSLint/JSHint) off our back for having an inop expression.
Fixes #41