Skip to content

Commit cb37765

Browse files
authored
Add note about Mocha's watch mode (testing-library#887)
1 parent fddbf26 commit cb37765

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

docs/react-testing-library/setup.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,29 @@ mocha -r @testing-library/react/dont-cleanup-after-each
368368
Alternatively, you could import `@testing-library/react/pure` in all your tests
369369
that you don't want the `cleanup` to run and the `afterEach` won't be setup
370370
automatically.
371+
372+
### Auto Cleanup in Mocha's watch mode
373+
374+
When using Mocha in watch mode, the globally registered cleanup is run only the
375+
first time after each test. Therefore, subsequent runs will most likely fail
376+
with a _TestingLibraryElementError: Found multiple elements_ error.
377+
378+
To enable automatic cleanup in Mocha's watch mode, add a cleanup
379+
[root hook](https://mochajs.org/#root-hook-plugins). Create a
380+
`mocha-watch-cleanup-after-each.js` file with the following contents:
381+
382+
```
383+
const { cleanup } = require("@testing-library/react");
384+
385+
exports.mochaHooks = {
386+
afterEach() {
387+
cleanup();
388+
},
389+
};
390+
```
391+
392+
And register it using mocha's `-r` flag:
393+
394+
```
395+
mocha -r ./mocha-watch-cleanup-after-each.js
396+
```

0 commit comments

Comments
 (0)