Add support for mocha root hooks
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.
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.
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.
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.
I can take a look at getting this added to the docs.
@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
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?
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 thelatestrelease 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 👌
Fixed in #2346