sinon icon indicating copy to clipboard operation
sinon copied to clipboard

Add support for mocha root hooks

Open nicojs opened this issue 5 years ago • 7 comments

Is your feature request related to a problem? Please describe.

If you want to use sinon with mocha, you shouldn't forget to restore the sinon sandbox after each test. This is documented here in general setup.

Since version 7.2, mocha supports root hooks. It is a way to export mocha root hooks (beforeEach, afterEach etc) from a file that is then included in the root suite (runs for each test). They are loaded using the --require option. More info here: https://mochajs.org/#root-hook-plugins. We could leverage this to remove this bootstrap code for every mocha user.

Describe the solution you'd like

For example:

mocha --require sinon/mocha-integration

The sinon/mocha-integration.js file would look like this:

exports.mochaHooks = {
  afterEach(){
     sinon.restore();
  }
);

We could also opt for exporting it from sinon itself, this way people could use it like this: mocha --require sinon

Describe alternatives you've considered The current way of working is fine, this is just a nice addition.

nicojs avatar Jun 15 '20 07:06 nicojs

Hmmm, I like the idea (and made something similar myself before with https://github.com/mantoni/mocha-referee-sinon), but I'm wondering if it would be enough to simply document this approach on the website? It wouldn't be an issue to support this since we're using Mocha ourselves, but why don't we have special integration points for Jest or Ava? Maybe specific test framework integrations should better live outside of this library.

mantoni avatar Jun 16 '20 08:06 mantoni

Documentation would be a great start.

Then we can explore options for making better/easier integrations with the test frameworks that people use.

I'd prefer to keep integrations in separate package(s), which might also attract contributions from the communities of around those test frameworks.

mroderick avatar Jul 16 '20 07:07 mroderick

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 14 '20 09:09 stale[bot]

I can take a look at getting this added to the docs.

Crimson-riot avatar Feb 27 '21 15:02 Crimson-riot

@Crimson-riot that would be great! If you run into difficulties, feel free to ask questions in this issue and the maintainers will do their best to help you out

mroderick avatar Mar 03 '21 17:03 mroderick

Thanks! I think I have a basic pr up, but wasn't too entirely sure which releases to add this small documentation to so as a first pass I added the docs to the latest release (v9.2.4) as well as the release-source. Would it have been better to add the docs directly to the latest release folder instead of a numbered release?

Crimson-riot avatar Mar 07 '21 23:03 Crimson-riot

Thanks! I think I have a basic pr up, but wasn't too entirely sure which releases to add this small documentation to so as a first pass I added the docs to the latest release (v9.2.4) as well as the release-source. Would it have been better to add the docs directly to the latest release folder instead of a numbered release?

The documentation is due for an overhaul, to make it easier to use and contribute to.

This was perfect 👌

mroderick avatar Mar 12 '21 12:03 mroderick

Fixed in #2346

fatso83 avatar May 18 '23 19:05 fatso83