mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.6k stars 3.01k forks source link

🚀 Feature: Make cleanReferencesAfterRun a Mocha option #4589

Open RossVertizan opened 3 years ago

RossVertizan commented 3 years ago

Is your feature request related to a problem or a nice-to-have?? Please describe. The request is a nice-to-have. I am working on integrating our tool Vitaq with Webdriverio and Mocha for use for web and app testing. Mocha is invoked from within a plugin maintained by Webdriverio.

The Vitaq tool needs to be able to run Suites multiple times and that capability was addressed by this issue (#2783) by setting the cleanReferencesAfterRun flag to be false. The solution works well for what I need to be able to do. However, the flag can only be set by calling the cleanReferencesAfterRun method on the mocha object. To set this from Webdriverio, would therefore mean a code modification. However, Webdriverio supports passing all of the Mocha options through to the mocha instance. It would be very helpful if cleanReferencesAfterRun could be elevated to be a Mocha option.

Describe the solution you'd like When the cleanReferencesAfterRun key is provided as an option, then the cleanReferencesAfterRun method should be called and the value of the option set. I would suggest the following lines of code be inserted in the file mocha.js as the Mocha instance is being initialised (line 162ish) between the handling of the retries option and the general handling of the options. I looked at adding it to the general options, but that appears to be setup only for options which default to false. The default for this would be true.

  if ('retries' in options) {
    this.retries(options.retries);
  }

  // ----> INSERT
  // Read the cleanReferencesAfterRun option
  if ('cleanReferencesAfterRun' in options) {
    this.cleanReferencesAfterRun(options.cleanReferencesAfterRun);
  }
  // <---- END

  [
    'allowUncaught',
    'asyncOnly',
    'bail',
    'checkLeaks',

Describe alternatives you've considered I've not really considered any other options, this is a fairly basic feature request and I can't really see an alternative. Either the option is recognised or it is not. If this is not accepted, I would need to fork the webdriverio mocha-framework, so that I could access the mocha object and run the cleanReferencesAfterRun method.

Additional context No additional context.

JoshuaKGoldberg commented 9 months ago

Seems like a pretty straightforward addition to the Mocha config. Hovering between considering this a bug and a feature, really.