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

endSuite hook is called without matching startSuite hook #5264

Open
RedX2501 opened this issue Nov 29, 2024 · 1 comment
Open

endSuite hook is called without matching startSuite hook #5264

RedX2501 opened this issue Nov 29, 2024 · 1 comment
Labels
status: waiting for author waiting on response from OP - more information needed

Comments

@RedX2501
Copy link

I'm creating this ticket due to ticket webdriverio/webdriverio#13013

The summary of the linked issue is that endSuite might be called without a matching startSuite if a describe is used that has no tests. This can happen when dynamically generating its.

I'm not sure if it is strictly necessary for hooks to always be called pairwise (start + end) but it makes coding any state dependent hooks much easier.

Here is the diff that solved my problem:

diff --git a/node_modules/mocha/lib/runner.js b/node_modules/mocha/lib/runner.js
index 60a19f0..9103d9b 100644
--- a/node_modules/mocha/lib/runner.js
+++ b/node_modules/mocha/lib/runner.js
@@ -854,14 +854,13 @@ Runner.prototype.runSuite = function (suite, fn) {
   var total = this.grepTotal(suite);
 
   debug('runSuite(): running %s', suite.fullTitle());
+  this.emit(constants.EVENT_SUITE_BEGIN, (this.suite = suite));
 
   if (!total || (self.failures && suite._bail)) {
     debug('runSuite(): bailing');
     return fn();
   }
 
-  this.emit(constants.EVENT_SUITE_BEGIN, (this.suite = suite));
-
   function next(errSuite) {
     if (errSuite) {
       // current suite failed on a hook from errSuite
@@ -903,6 +902,7 @@ Runner.prototype.runSuite = function (suite, fn) {
     // remove reference to test
     delete self.test;
 
+    debug('done(): ending %s', suite.fullTitle());
     self.hook(HOOK_TYPE_AFTER_ALL, function () {
       self.emit(constants.EVENT_SUITE_END, suite);
       fn(errSuite);

This issue body was partially generated by patch-package.

@JoshuaKGoldberg
Copy link
Member

Interesting find! Could you / someone please post an isolated reproduction we can take a look at here? One that doesn't require knowing other downstream tools like webdriverio. Thanks!

https://antfu.me/posts/why-reproductions-are-required

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP - more information needed label Nov 29, 2024
@mochajs mochajs deleted a comment from king-2929 Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

No branches or pull requests

2 participants