Skip to content
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

add allowFunctions as an allow-list for require-expect function calls #233

Closed
wants to merge 1 commit into from

Conversation

mansona
Copy link

@mansona mansona commented May 27, 2022

Hey folks 👋

I was following the documentation for @percy/ember and they have an example that allows you to pass the assert argument into a percySnapshot() call to automatically interpret the test name and module name https://github.com/percy/percy-ember#qunit

But if you have require-expect configured with the default Check Type except-simple this will fail because you shouldn't pass assert to other functions.

I was thinking about it and I wanted to make use of this functionality in our application but I didn't want to disable the require-expect rule (or even change the current config of it). I considered doing an ignore for each case that I wanted to use this functionality but this just didn't seem right. I thought in the end the Best Solution™️ would be to have the ability to configure which functions I should allow you to pass assert into.

And this PR adds the ability to add a new config option to add an allow-list for passing assert into functions 🎉

Let me know if you have any questions or if you would like me to change or improve this PR in any way 👍

Copy link
Collaborator

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing! I've left some comments.

Additionally, please add an invalid test showing that the rule still warns if you specify one function as allowed, and pass assert to a different function instead. Thanks!


If you are using the Check Type `except-simple` whenever you pass `assert` into
another function you will cause an error. If you have some functions that you
are passing `assert` to that don't add to the number of asserts (and maybe just
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the part in parentheses is probably unnecessary. Would you mind removing it?

"type": "object",
"properties": {
"allowFunctions": {
"type": "array"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add to this to require that all items in the array should be strings? Also, can we only allow this option if the previous value is except-simple?

if (!currentTest.assertName) {
return false;
}

if (allowedFunctions.includes(node.callee.name)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only work with identifiers. Do we want to support property accesses (for example, myLib.preprocess(assert)?

"test('name', function(assert) { myAssertion(null, assert, null); });"
].join(returnAndIndent),
options: ["except-simple", { allowFunctions: ["myAssertion"] }],
errors: [exceptSimpleErrorMessage("assert.expect")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this code pass? Please remove the errors property.

Example:

```js
rules: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for eslint-plugin-markdown to pass, please surround this object with curly braces and remove the dangling comma at the end. Thanks!

Here is an example:

```js
rules: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for eslint-plugin-markdown to pass, please surround this object with curly braces and remove the dangling comma at the end. Thanks!

@bmish
Copy link
Member

bmish commented Jul 6, 2022

One note: we are planning to change the default config of require-expect rule to never-except-zero in the next major release (#222) in case that helps.

@platinumazure
Copy link
Collaborator

Hi @mansona, I apologize for letting this sit so long. Let me know in the next week or so if you are still interested in finishing this up; if not, I'll go ahead and close this. In either case, I appreciate the work so far and welcome future contributions!

@platinumazure
Copy link
Collaborator

Closing this due to inactivity. Feel free to resubmit this if desired (original PR creator or anyone else). Thanks for contributing!

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

Successfully merging this pull request may close these issues.

3 participants