Repository navigation
DeallocTests 4.0 (3/5): trackForDeallocation, leak hints and Dependency Injection support - #27
Open
DanielCech wants to merge 5 commits into
Conversation
- `trackForDeallocation` checks objects owned by the tested one inside `expectDeallocation`, and the system under test of any unit test: an XCTestCase method, or the `.checksDeallocation` Swift Testing trait. - Failure messages list likely causes found in the leaked object's stored properties: closures, Tasks, Combine subscriptions, timers and reference cycles. Only for confirmed leaks, phrased as suggestions, because Mirror can't tell weak properties from strong ones. - `expectDeallocation(of:resolvedFrom:)` resolves a dependency from an AsyncContainer, releases the shared instances and checks it; a value type is reported with its concrete type. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This was referenced Oct 5, 2026
Doc comments are kept for public API only. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The Swift Testing trait skips deallocation verification whenever the test body throws.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Adds broader deallocation tracking, diagnostic leak hints, and Dependency Injection container support.
Changes:
- Adds
trackForDeallocationfor Swift Testing and XCTest. - Generates reflection-based leak hints.
- Adds
AsyncContainerdependency checks and related tests.
| File | Description |
|---|---|
Package.swift |
Adds conditional DI test dependency. |
Sources/DeallocTests/Diagnostics/LeakHints.swift |
Detects likely leak causes. |
Sources/DeallocTests/Diagnostics/LeakReport.swift |
Includes hints in reports. |
Sources/DeallocTests/Expectation/DeallocationTracker.swift |
Supports task-local tracking and hints. |
Sources/DeallocTests/Expectation/ExpectDeallocation+DependencyInjection.swift |
Adds DI resolution checks. |
Sources/DeallocTests/Expectation/ExpectDeallocation.swift |
Tracks nested objects. |
Sources/DeallocTests/Expectation/TrackForDeallocation.swift |
Adds tracking APIs and trait. |
Tests/DeallocTestsTests/ExpectDeallocationTests.swift |
Tests tracking and DI support. |
Tests/DeallocTestsTests/LeakHintsAndSwiftUITests.swift |
Tests hints and nested tracking. |
Tests/DeallocTestsTests/LeakReportTests.swift |
Tests hint generation and formatting. |
Tests/DeallocTestsTests/TrackForDeallocationXCTests.swift |
Tests XCTest tracking. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…rows The trait skipped verification when the test body threw, so a failing test silently checked nothing. It now verifies before rethrowing, like XCTest's teardown blocks, which also run after a thrown error. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
DanielCech
added this pull request to stack #30
October 9, 2026 08:03
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.

Part 3 of 5. DeallocTests 4.0 series (merge in order): #25 groundwork → #26
expectDeallocation→ #27 tracking, hints, DI → #28 configuration → #29 remove legacy, docs. Why 4.0, the breaking changes and the alternatives we looked at: see #25.Beyond one object: check owned objects and any unit test's system under test, explain leaks, and check dependencies from a Dependency Injection container.
Why
checkClassesonly worked for classes that installed tracking themselves, in app code.Alloc/Deallocconsole output..sharedregistrations live in the container. 3.x only handled this inside aDeallocTestersubclass.What changes
trackForDeallocationA call without the trait or an enclosing check is reported instead of silently checking nothing.
Leak hints
Tasks, Combine subscriptions, timers and reference cycles through properties.niland empty values, shows@Observablenames, and doesn't follow Apple framework objects.Mirrorcan't tellweakfrom strong, so a weak delegate would be a false leak. Hints are computed only for a confirmed leak and phrased as suggestions.Dependency Injection (with the default trait)
The dependency is resolved, the container's shared instances are released, then the check runs. A value type is reported with its concrete type, because checking a boxed copy would always pass.
How to review
Expectation/TrackForDeallocation.swiftDiagnostics/LeakHints.swift,LeakReport.swiftExpectation/ExpectDeallocation+DependencyInjection.swiftSource +398, tests +327.
Testing
Tracking in tests, suites, parameterized tests, nested and XCTest; one leak case per hint kind; DI shared/new/value type. Green on macOS (both trait settings) and the iOS Simulator.
🤖 Generated with Claude Code