Add test.cleanup() and test.cleanupEach() for reliable cleanup tasks#3469
Open
stevenmini2019 wants to merge 1 commit into
Open
Add test.cleanup() and test.cleanupEach() for reliable cleanup tasks#3469stevenmini2019 wants to merge 1 commit into
stevenmini2019 wants to merge 1 commit into
Conversation
Implements avajs#928. `test.cleanup(fn)` registers fn as both a `before` hook and an `after.always` hook; `test.cleanupEach(fn)` registers it as both a `beforeEach` hook and an `afterEach.always` hook. The "after" halves always run, even when a test fails or --fail-fast is used, so idempotent cleanup code can rely on them to remove state left behind by the current (or a previously crashed) run. Both are available on test.serial and support .skip().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements reliable cleanup tasks for #928. Adds two new chain methods:
test.cleanup(fn)— registersfnas both abeforehook and anafter.alwayshook. Theafter.alwayshalf always runs, even when a test fails or--fail-fastaborts the run, so idempotent cleanup code can rely on it to remove state left behind by the current (or a previously crashed) run.test.cleanupEach(fn)— registersfnas both abeforeEachhook and anafterEach.alwayshook, so it runs around every test.Both are also available on
test.serial(test.serial.cleanup,test.serial.cleanupEach) and support.skip().Why this design
The root problem in #928 is that
after/.alwayshooks are not guaranteed to run after a failure or when a process exits/crashes mid-run, leaving stale fixtures behind. By pairing abefore(orbeforeEach) registration with anafter.always(orafterEach.always) registration, the cleanup code is guaranteed to run on the way out — this is the contracttest.cleanup()andtest.cleanupEach()provide.Implementation
lib/create-chain.js— adds acreateCleanupChainhelper that wirescleanup/cleanupEachinto bothrootandroot.serial, with.skip()support.lib/runner.js— the chain-metadata callback now recognizes thecleanup/cleanupEachtask types and pushes the implementation into two task arrays:before+afterAlways(forcleanup) andbeforeEach+afterEachAlways(forcleanupEach). Theafterhalves are flaggedalways: trueso they run even on failure.types/test-fn.d.ts— adds aCleanupFntype and exposescleanup/cleanupEachonTestFnandSerialFn.test-types/module/conditional-chains.ts— type-level coverage for the new chains.test/create-chain/test.js— metadata-level assertions (runs in CI).test/cleanup/test.js— runtime integration test proving the dual execution, including the on-failure guarantee.docs/01-writing-tests.md— documentstest.cleanup()/test.cleanupEach().Verification
xopasses on all modified files.test/cleanup/test.jspasses locally:cleanupruns asbefore+after.always,cleanupEachruns around every test, and a failing test still triggers theafter.alwayshalf (the core guarantee of this feature).Notes / open question
Unlike the previously-open PR #3457 (which only touched
create-chain.jsand never connected the runtime), this wires the behavior all the way throughrunner.jsso the hooks actually execute. Happy to adjust the API surface (e.g. method names) if maintainers prefer a different convention.IssueHunt Summary
Referenced issues
This pull request has been submitted to: