mocha icon indicating copy to clipboard operation
mocha copied to clipboard

🚀 Feature: Allow to set timeout on before/after/beforeEach/afterEach in declaration

Open perrin4869 opened this issue 1 year ago • 2 comments

Feature Request Checklist

Overview

it returns a TestFunction that has a timeout method. This is useful for setting timeouts in slow tests, without needing to define the test code inside a function() {} block. An analogous feature would be useful for the before/beforeEach/after/afterEach hooks. The current workaround would be:

before(function() {
  this.timeout(3000);
});

Suggested Solution

before/beforeEach/after/afterEach could return a HookFunction, analogous to TestFunction, which has a timeout method. The code above could be rewritten:

before(() => {}).timeout(3000);

Alternatives

None

Additional Info

No response

perrin4869 avatar Aug 27 '24 03:08 perrin4869

👍 Agreed, I'm surprised they didn't have this already! This took me a bit to parse through as someone who doesn't use those APIs much. Putting a summary here for clarity...

There are two ways to describe a timeout:

  • this.timeout(...);: allowed for hooks (before(...), etc.) as well as tests (it(...), etc.)
  • test.timeout(...): only allowed for tests

Assuming I'm understanding correctly, this issue is asking to allow hook.timeout(...). Doing so would mirror the test.timeout(...) already allowed.

JoshuaKGoldberg avatar Oct 08 '24 18:10 JoshuaKGoldberg

@JoshuaKGoldberg thanks for summarizing it more clearly than the original post 🙂

ok, I just looked into the codebase, and I think this will be very simple to implement, but I am not sure about potential implications. I will send a PR shortly

perrin4869 avatar Oct 14 '24 03:10 perrin4869