Skip to content

Commit e5234c2

Browse files
committed
Better error messages for uncompleted effects. (#270)
1 parent 6be9598 commit e5234c2

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-LongLivingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class LongLivingEffectsTests: XCTestCase {
2828

2929
.send(.onDisappear),
3030

31-
// Simulate a screenshot being taken to show not effects
31+
// Simulate a screenshot being taken to show no effects
3232
// are executed.
3333
.do { screenshotTaken.input.send(value: ()) }
3434
)

Sources/ComposableArchitecture/TestSupport/TestStore.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@
221221
) {
222222
var receivedActions: [Action] = []
223223

224-
var cancellables: [Disposable] = []
224+
var cancellables: [String: [Disposable]] = [:]
225225

226226
func runReducer(action: Action) {
227+
let actionKey = debugCaseOutput(action)
228+
227229
let effect = self.reducer.run(&self.state, action, self.environment)
228230
var isComplete = false
229231
var cancellable: Disposable?
@@ -233,13 +235,14 @@
233235
case .completed, .interrupted:
234236
isComplete = true
235237
guard let cancellable = cancellable else { return }
236-
cancellables.removeAll(where: { $0 === cancellable })
238+
cancellables[actionKey]?.removeAll(where: { $0 === cancellable })
237239
case let .value(value):
238240
receivedActions.append(value)
239241
}
240242
}
241243
if !isComplete, let cancellable = cancellable {
242-
cancellables.append(cancellable)
244+
cancellables[actionKey] = cancellables[actionKey] ?? []
245+
cancellables[actionKey]?.append(cancellable)
243246
}
244247
}
245248

@@ -334,12 +337,23 @@
334337
line: line
335338
)
336339
}
337-
if !cancellables.isEmpty {
340+
341+
let unfinishedActions = cancellables.filter { !$0.value.isEmpty }.map { $0.key }
342+
if unfinishedActions.count > 0 {
343+
let initiatingActions = unfinishedActions.map { "\($0)" }.joined(separator: "\n")
344+
let pluralSuffix = unfinishedActions.count == 1 ? "" : "s"
345+
338346
_XCTFail(
339347
"""
340348
Some effects are still running. All effects must complete by the end of the assertion.
341349
342-
This can happen for a few reasons:
350+
The effects that are still running were started by the following action\(pluralSuffix):
351+
352+
\(initiatingActions)
353+
354+
To fix you need to inspect the effects returned from the above action\(pluralSuffix) and \
355+
make sure that all of them are completed by the end of your assertion. There are a few \
356+
reasons why your effects may not have completed:
343357
344358
• If you are using a scheduler in your effect, then make sure that you wait enough time \
345359
for the effect to finish. If you are using a test scheduler, then make sure you advance \

0 commit comments

Comments
 (0)