-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
afterEach inside beforeEach and mocha timing issues. #1975
Comments
I'd love this check to be added to mocha but I feel like an ESLint rule could also solve this one real quick. |
sorry about that problem that you experienced, frankly Git is not the best with JavaScript..one thing I always do to minimize problems is run a "git merge -Xignore_all_space" like so http://stackoverflow.com/questions/9776527/merging-without-whitespace-conflicts the problem you experienced is because of the usage of globals and contrary to what @rodoabad said I don't think ESLint is the right way to prevent the problem you experienced from happening to other people IMO, what should happen in the future, and what is currently underway, is that without globals, you won't be allowed to call beforeEach inside an afterEach, and an error will be thrown "afterEach/beforeEach is not a function" so, soon enough, it will look like: test.beforeEach(function(b){
b.afterEach(function(){ //will throw an error because b doesn't have the afterEach function as a property
});
}); just another reason why globals are bad |
I just came here to ask this as well, I think it should |
I think this patch should cover it, if you're interested I can submit Shyp@d8e28a3 |
#2244 was started on this, but not completed, if someone else would like to pick it up. |
I think the primary fix we need here is for all Mocha's interface functions to be treated as errors if called after Mocha starts running tests -- besides this particular unwanted behavior not being detected by timeout, we've also had a few issues where people tried to add tests during a hook on purpose and were surprised it didn't work the way they expected, which as far as I'm aware comes down to the fact that it's not meant to be supported (Mocha operates in two phases: collect tests+hooks, then run them; trying to add more in the running phase breaks too many assumptions in that design). |
I am a bot that watches issues for inactivity. |
Just spent ages figuring out why our tests seem to slow down about half way through a test suite and continue slowly from then on.
Basically, through what looks like a git merge, an
afterEach
call had got inside abeforeEach
call.This seemingly causes the
afterEach
to be run in the outer context (or whatever mocha calls thedescribe
-scopedbefore/after
mechanism) which was adding a load ofafterEach
calls for every subsequentit
in the suite, causing every test thereafter to run slower.Mocha's slow test reporting was not picking this up and it was a nightmare to figure out.
This brings up two issues:
beforeEach
andafterEach
calls? It doesn't seem to, and if it did it would have made debugging this a whole lot easier.afterEach
inside abeforeEach
? I can't think of a reason why you would ever want to do this?Example:
As you can see, this adds an extra after step each time an
it
is run. This resulted in ~10,000 extra calls being made to theafterEach
in one test file, which slowed our tests down significantly. These tests were definitely taking more than the--slow
timeout, but they were not being reported as slow.Expected output would be an error being thrown that
afterEach
cannot be run insidebeforeEach
.Obviously, this is a mistake in the test code, but an error would have been handy. This would also be avoided by using a proper language that doesn't have significant whitespace and linting the indentation, but I feel like mocha could have been more helpful here.
The text was updated successfully, but these errors were encountered: