Skip to content

Commit

Permalink
docs: Add warning about async callback for describe
Browse files Browse the repository at this point in the history
I couldn't find any reference to `describe` not supporting an async function. It seems like a natural idea given `it` and `before` do.

I spent considerable time trying to figure out why an async test was failing before I discovered the reason deep in an issue discussion (mochajs#2975 (comment)), so hope that this helps others design their test suites!
  • Loading branch information
mcdurdin committed Dec 12, 2023
1 parent eca4fec commit 65133eb
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ describe('#find()', function () {
});
```

### Limitations of asynchronous callbacks

You can use all three asynchronous patterns (`done`, `Promise`, and `async`/`await`) in callbacks for `it()` and for all hooks (`before()`, `after()`, `beforeEach()`, `afterEach()`), but `describe()` will not work correctly with an asynchronous callback -- it must be synchronous. This is because, in the "parsing" cycle all `describe` callbacks are excecuted and the test hierarchy (`runner`) is created, before any tests are run.

## Synchronous Code

When testing synchronous code, omit the callback and Mocha will automatically continue on to the next test.
Expand Down

0 comments on commit 65133eb

Please sign in to comment.