🐛 Bug: Watch + mochaHooks inconsistent state on re-runs #5149
Labels
semver-major
implementation requires increase of "major" version number; "breaking changes"
status: accepting prs
Mocha can use your help with this one!
type: bug
a defect, confirmed by a maintainer
Milestone
Bug Report Checklist
faq
label, but none matched my issue.Expected
Mocha's watch mode should produce the same results on all runs, if the code does not change significantly (e.g. triggered by adding a new line.). Using
mochaHooks
that mutate globals and modules/requires should be consistent in all runs.Actual
Mocha's watch mode seems to not work the same on reloads regarding
mochaHooks
as in the 1st run.Having a
beforeEach()
thatrequire
s a fil, and which we mutate state of in that hook, when a test that relies on that mutated state runs, first time is ok, but subsequent "watch runs" fail.First run is ok, but then any irrelevant change on a watched file:
Minimal, Reproducible Example
I created a small repro repo, keeping it similar to my source: https://github.com/Kartones/mocha-hooks-watcher-bug
Includes a README with steps to reproduce, but adding here a minimal TL;DR:
given:
And a hook that mutates it:
The hook is correctly executed before each test, but
a
seems to be kept at the initial state (with_items=[]
, instead of_items=["testItem1", "testItem2"]
)Versions
Mocha: 10.4.0
NodeJS: v20.11.1
pnpm (not strictly relevant): pnpm@8.15.3
Additional Info
UPDATED: Taking a look, will provide a PR with one solution.
The text was updated successfully, but these errors were encountered: