File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
docs/react-testing-library Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -368,3 +368,29 @@ mocha -r @testing-library/react/dont-cleanup-after-each
368
368
Alternatively, you could import ` @testing-library/react/pure ` in all your tests
369
369
that you don't want the ` cleanup ` to run and the ` afterEach ` won't be setup
370
370
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
+ ```
You can’t perform that action at this time.
0 commit comments