-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: detect illegally nested tests (#4525) #5395
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
base: main
Are you sure you want to change the base?
Conversation
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.
Thanks for starting on this! I think there should be a way to avoid globals.
Throws if there is an it() call inside an it() call. Same for test().
Also, changed to also detect a test inside a hook
64a713e
to
a710267
Compare
Absolutely -- I didn't want to use a global variable, but didn't understand the structure of Mocha well enough to avoid it. But I think I have it working now. When the test runner is called, it is passed a suite. I now store a Also, I tweaked it so that it also detects tests nested inside of hooks. For example, this is now caught (although this is far less likely to occur than a test inside a test): before('my hook', () => {
it('should not have this test inside a hook', () => {});
}); Thanks for the feedback; please let me know if any changes are needed. |
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.
Looks great to me, nice! I fiddled with it a bunch and couldn't find
No blocking review comments from me. Just one small "it's fine either way" consideration. But, since the backing issue #4525 is a semver-major breaking change, we're going to have to wait to merge this until we're prepping a major version with breaking behavioral changes.
Pull common code from tdd.js and bdd.js into common.js
Hi @JoshuaKGoldberg just checking in to make sure you saw that last week I addressed the point you raised about duplicate code :) |
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.
Lovely!
No blockers from me other than just waiting for a major version. Thanks!
Fix name to match, and remove redundant comment Co-authored-by: Josh Goldberg ✨ <[email protected]>
Throws if there is an it() call inside an it() call. Same for test().
PR Checklist
status: accepting prs
Overview
Adds some state to keep track of whether we are currently inside a test. If we are, and we enter a new test, throws an error.
helpful failure messages
If a nested test is detected, the failure that is logged shows the name of both
the outer and inner test. For example:
new tests
I added a bunch of tests:
global variable
I could find no existing state that was visible and allowed me to determine
whether we were already inside a test. Therefore, this PR creates a new global
variable,
global._mochaExecutionState
. If there is a better, non-global placeto store this, that would be great.
old failing tests
I don’t know why, but
npm test
had some failing tests even before my changes.They are unrelated to my proposed changes.
minor code duplication and code complexity
Before my change, in lib/interfaces/{tdd.js,bdd.js} the main test functions,
context.test and context.it, were super short and simple. I don’t love the fact
that my change adds a bit of complexity to both (and it’s the same code in both
-- but on the other hand, those two functions already contain identical code).
risk of breaking existing tests
I want to point out that even though this is a desirable feature, I think there
is significant risk of breaking people’s current tests. I became aware of this
nested-test issue when I worked at Asana, and over our very large codebase,
there were at least 20 tests that had this problem. It took a lot of work to
fix them all.
If Mocha just made this change, this breakage could cause people to avoid
upgrading to more recent versions.
Mitigation options to consider:
nested-test checks are ON by default, but can be turned off. This way, when
someone upgrades Mocha, they may get errors, but they can still keep the new
Mocha without having to stop and invest a lot of time in fixing their tests.
Also, on by default means that if anyone uses Mocha in a new project, they
will get the nested-test checks.
default, then this could be considered a breaking change, which would mean a
major version bump.