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

Handle arrow function test with except-simple configuration of the require-expect rule #239

Merged

Conversation

bmish
Copy link
Collaborator

@bmish bmish commented Aug 27, 2022

Fixes #238.

This bug only affected the except-simple (default) configuration of the require-expect rule. The arrow function body of the test was incorrectly being detected as a greater nesting depth, preventing the test from detecting the top-level expect statement.

I also added test cases for arrow function tests to the other configurations of this rule.

CC: @raybaker

@bmish bmish force-pushed the require-expect-arrow-except-simple branch from 5b275f6 to dab3809 Compare August 27, 2022 17:47
@coveralls
Copy link

coveralls commented Aug 27, 2022

Coverage Status

Coverage remained the same at 100.0% when pulling 1744356 on bmish:require-expect-arrow-except-simple into ef8f129 on platinumazure:master.

@bmish bmish force-pushed the require-expect-arrow-except-simple branch from dab3809 to 8c55c83 Compare August 27, 2022 17:48
@platinumazure
Copy link
Owner

Thanks @bmish for the PR (and @raybaker for reporting the original issue).

Changes look good at a glance, but I want to test the changes locally and understand better which of the added tests would have failed (this will also help me to make sure no test cases are missing). I will review tonight, hopefully.

@raybaker
Copy link

That was an incredibly quick response/fix @bmish / @platinumazure, thank you both!

@@ -125,12 +130,24 @@ ruleTester.run("require-expect", rule, {
options: ["except-simple"]
},

// When the test body itself is using an arrow function, make sure we still correctly-detect top-level expect.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the only test case that was broken before since this bug only affected the except-simple config. I added corresponding test cases to the other configs regardless.

Copy link
Owner

@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.

I knew I had a good reason to be suspicious of this bug and the proposed solution... (Not due to bad code on your side or anything)

It looks like the real issue might be improper esquery syntax in the handlers which basically prevent ArrowFunctionExpressions (with body.type=BlockStatement) from being properly ignored by the handler. Not sure why that didn't throw, or emit warnings, or anything. But yeah, the original design definitely accounted for arrow functions and that's why I wanted to take a closer look at this one. (And you don't want to know how long I spent in node inspect until I finally noticed the issue...)

Would you mind trying the changes I suggest in this review, and see if the tests still pass accordingly? My thinking is, it shouldn't matter if we are counting the test function itself as a block depth, because the isViolatingExceptSimpleRule function is checking for blockDepth greater than 1. So basically, we're building the test itself as an acceptable block depth.

Another test you could add, to see if I'm on the right track here, would be an invalid case that has 2 nested blocks or functions (3 total blocks when you include the test function itself). If I'm right, this new test would pass with my proposed approach, but fail with your approach (because blockDepth would only be 1 due to the logic you're introducing).

Let me know if I'm not making sense. Thanks again for the effort so far and I appreciate your patience as I fit this review in while also moving out of my house to avoid re-roofing, and other disruptions to my life 😄

lib/rules/require-expect.js Outdated Show resolved Hide resolved
lib/rules/require-expect.js Outdated Show resolved Hide resolved
lib/rules/require-expect.js Outdated Show resolved Hide resolved
lib/rules/require-expect.js Outdated Show resolved Hide resolved
@bmish
Copy link
Collaborator Author

bmish commented Sep 1, 2022

@platinumazure thanks for all the details and figuring out the better fix. I switched to your fix. However, I was unable to reproduce any test cases that your fix works for but mine doesn't.

Copy link
Owner

@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 the changes. Guess I was wrong about the edge case unit test, but I'm not going to worry about it. I'll merge ASAP.

@platinumazure platinumazure merged commit f789574 into platinumazure:master Sep 2, 2022
@bmish
Copy link
Collaborator Author

bmish commented Sep 2, 2022

@platinumazure thanks! Hopefully I covered all the test cases although not 100% sure of course.

Probably a good time for a patch release of this along with the Github releases (#237).

@bmish
Copy link
Collaborator Author

bmish commented Sep 2, 2022

@platinumazure also I'll plan to investigate why ESLint didn't throw an error with this invalid esquery syntax. That sounds like a breaking change to make if possible in the next version if ESLint to catch bugs like this.

@bmish bmish mentioned this pull request Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7.2.0 - qunit/require-expect not correctly handling arrow function
4 participants