This post is about a test in a SwiftUI app I'm building with The Composable Architecture (TCA), which passed for a reason that had nothing to do with the code it was named after, and which I would not have spotted without a mutation check. If you write a state assertion in a non-exhaustive TestStore through an optional chain, like $0.detail?.picker = …, and the action under test sets detail to nil, the assertion writes nothing and can't fail.

The test

The app has a detail page that can push a small list (a picker of other bundles to merge into), and when the app switches into a more restricted session, that list has to be emptied in the same action, so nothing read for the previous session stays on screen. The reducer side of it is one line among several that empty lists the same way:

case .sessionChanged(.restricted): state.detail?.relatedByAuthor = [] state.detail?.picker?.empty() // …the other lists, and a sweep of pages the new session can't show 

empty() drops the picker's rows and any merge that's waiting for confirmation. The test set up an open page with a filled picker, sent the action, and asserted the picker came back empty:

func test_restrictedSession_emptiesAnOpenPicker() async { var state = AppFeature.State() state.detail = DetailFeature.State(page: Page(title: "Open bundle", members: [])) var picker = MergePicker(purpose: .merge) picker.targets = [target] // a fixture bundle, set up elsewhere picker.pendingMerge = target state.detail?.picker = picker let store = TestStore(initialState: state) { AppFeature() } store.exhaustivity = .off(showSkippedAssertions: false) await store.send(.sessionChanged(.restricted)) { $0.detail?.picker = MergePicker(purpose: .merge) } } 

It passed, and when the mutation check deleted the state.detail?.picker?.empty() line and ran the test again, it still passed. The check ran 20 mutants over the new merge-picker code, 18 of them made a test fail on the first pass, and this was one of the two that didn't.

Why it passed

My first guess was that non-exhaustive mode was skipping the field somehow, which is wrong, since every field you write in the closure is compared, and what matters is which state the closure gets. In the default, exhaustive mode, $0 in the send closure is the state before the action and you mutate it into what you expect afterwards. In a non-exhaustive store it's the other way around, as the TCA testing article explains, so $0 is the state after the action, and any mutation you make has to match what's already there.

The other half was in the fixture, because the same action also runs a sweep that closes any open page with nothing left to show in the new session, and the test's page had no members, so after the action detail was nil. Optional chaining in Swift skips an assignment when something in the chain is nil, without any error, so $0.detail?.picker = … wrote to nothing, the "expected" state was the actual state untouched, and the comparison passed. Whether empty() ran didn't matter, because the page it would have emptied was already gone.

In exhaustive mode the same test would have failed, because $0 would still hold the open page, the assignment would land, and the store would then report that detail went to nil. The test was non-exhaustive because the action touches a lot of unrelated state.

The fix

The fixture has to keep the page open, so it gets one member the new session can still show, and the test checks that the page is still there, so a later change that closes it fails with a message instead of turning the assertion into a no-op again:

state.detail = DetailFeature.State(page: Page( title: "Open bundle", members: [BookWithProgress(book: book, isProtected: false)] )) // …same picker setup await store.send(.sessionChanged(.restricted)) { $0.detail?.picker = MergePicker(purpose: .merge) } XCTAssertNotNil(store.state.detail, "The page must stay open for the emptied list to be observed") 

Under the same mutant this one goes red with TCA's "A state change does not match expectation." Swift also lets you check the chain inside the closure, since an optional-chained assignment returns Void?, which is nil when the assignment didn't land, so XCTAssertTrue(($0.detail?.picker = …) != nil) works too, but the explicit check after send reads better and says why it's there.

The older test right next to it, which checks that the same action empties two other lists on that page, used the same empty-page fixture and asserted both through $0.detail?, starting with $0.detail?.relatedByAuthor = [], so neither assertion had checked anything since the test was written. That code wasn't part of the 20 mutants, which is why the batch didn't catch it, and the test needed the same fix.

The other survivor was a dead guard

The second mutant that survived in the same batch turned a guard into true. The guard was in the function that builds the picker's rows, and it dropped a bundle the current session isn't allowed to show:

guard visibility.admitsBundle(id: bundle.id, in: protected) else { return nil } let shown = members.filter { visibility.admitsBook(id: $0.id, bundleId: bundle.id, in: protected) } guard !shown.isEmpty else { return nil } 

I expected a missing test, but admitsBook refuses any book whose bundle admitsBundle would refuse, so the per-member filter on the next line already drops every member of a bundle the session doesn't admit, and a bundle with no shown members is dropped by the next guard, so the first guard could never change the result. No test can fail on a line like that, and the fix was to delete it, leaving a one-line comment that the member filter covers that case.

An assertion through ?. in a non-exhaustive TestStore only checks something while the optional is still non-nil after the action, and the same goes for a receive closure, so when the action or an effect after it can close or clear the thing you're asserting on, build the fixture so it stays and check that it stayed after the last step that could clear it. Neither test looked wrong when I read it, and running a mutation check over reducers that clear state was a cheap way to find out whether an assertion can fail at all, which in this batch also turned up a guard that was safe to delete.