Repository navigation
feat: restore spies automatically with using - #58
Merged
Merged
Conversation
privatenumber
marked this pull request as ready for review
August 29, 2026 23:28
sheremet-va
requested changes
Aug 30, 2026
Contributor
Author
|
@sheremet-va Addressed! |
sheremet-va
approved these changes
Sep 4, 2026
Member
|
LGMT! |
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.
Problem
spyOn()replaces an object's property until the test calls.restore(). If a test returns early or throws first, the replacement can remain installed and affect later assertions.ECMAScript explicit resource management defines
[Symbol.dispose]()as the synchronous cleanup hook forusingdeclarations. TypeScript'susingdocumentation specifies that this cleanup also runs when a scope exits through an error or early return.For example,
using request = spyOn(service, 'request')had no cleanup behavior, so test authors needed atry/finallyblock to guarantee restoration.Closes #57
Changes
spyOn()results now expose[Symbol.dispose]()as the existing.restore()operation. Tests can scope a replacement to a block and have the original property restored automatically:The runtime hook is defined only when
Symbol.disposeexists, so the package continues to load on its supported older Node.js versions. The new behavior covers normal scope exit and error unwinding.