Latest / Software Testing with Fexingo: QA, Automation, and Reliable Software Engineering / How State Management Testing Prevents UI Bugs
Transcript
- Lucas: So here's a scenario I keep seeing in code reviews: a developer writes unit tests for every reducer in a Redux store. Coverage is at ninety-five percent. But the app still crashes when a user rapid-clicks a button under network latency. Why? Luna: Because the tests only check each action in isolation, right? They don't simulate the real sequence of dispatches that happen in production. Lucas: Exactly. That's the gap that state management testing fills. Traditional unit tests treat state transitions as deterministic derivations from one action. But real UIs fire actions in unpredictable order — often overlapping, sometimes while async operations are still in flight. Luna: And that's where race conditions live. I remember a fintech case from about two years ago — a payments app that would show a double charge if you tapped 'Pay Now' twice before the first request completed. Lucas: Right. The Redux store had a 'submitting' boolean that was supposed to disable the button, but the test only checked that toggling SUBMIT_START set it to true. It never tested what happens when SUBMIT_SUCCESS and another SUBMIT_START arrive in the same synchronous tick. Luna: So what's the fix? How do you test for that specific race condition? Lucas: You write what I'd call a sequence test. You dispatch a chain of actions — not just one at a time, but a realistic pattern. In the fintech case, you'd dispatch SUBMIT_START, then immediately SUBMIT_SUCCESS, then another SUBMIT_START within the same test, and assert that the store never produces a second 'pending transaction' entry. Luna: That sounds straightforward, but the combinatorial explosion must be huge. How do you decide which sequences to test? Lucas: You don't brute-force all permutations. You look at the actual user flows. The most common problematic patterns are rapid double-clicks, simultaneous tab switches during form submission, and back to back API calls where the second depends on data from the first. I usually start with a user-flow matrix: list every action in a feature, then pair them with every other action that could logically fire within a hundred milliseconds. Luna: So it's basically combinatorial testing applied to state transitions. Have you seen tools that automate that? Lucas: There's a technique called action-sequence fuzzing. You take all the action types defined for a slice, and you write a fuzzer that generates random sequences of those actions, including async resolution and rejection. Then you assert invariants — things like 'the total balance never goes negative' or 'the pending flag is never stuck at true for more than ten seconds.' Luna: That's clever. But invariants only catch the specific things you think to check. What about edges you didn't anticipate? Luna: And you run those with a library like fast-check or Hypothesis for JavaScript? Lucas: Exactly. The key insight is that state management testing is a different category from component testing. A React Testing Library test might simulate a button click and check that a counter incremented. But it won't catch a bug where the counter increments twice because a useEffect ran on an unintended dependency. That's a state management level bug. Luna: So where do these tests live? In the same test suite as the unit tests? Lucas: I like to keep them separate — a file called something like 'state-machine.spec.ts' that only tests the store logic, not the UI. That way you can run them in CI without even rendering components. They're fast because they're pure JavaScript. Luna: Pure function tests on the store logic, but with sequences instead of single actions. That makes sense. What about state management libraries that aren't Redux? Like Zustand or Pinia? Lucas: The same principle applies, but the test setup looks different. With Zustand, your store is just a hook, so you can test it by calling the action functions directly and inspecting the state object. But you lose the serialized action log that Redux gives you. That's actually a trade-off: Redux's middleware makes it easier to intercept and replay sequences for testing. Luna: So with Zustand you'd have to manually track actions? Or add your own logging middleware? Lucas: You'd either write a thin wrapper that records action names, or you'd just test the final state after a sequence of method calls. It's less visible but still doable. The important thing is that you're testing combinations, not isolated mutations. Luna: I want to circle back to the fintech example. Did they fix the double-charge bug by adding state management tests? Luna: So the test became the spec for the intended behavior. That's powerful. I've also seen teams use middleware to enforce those invariants at runtime in development — like a 'state-invariant middleware' that throws if the store ever violates your rules. Lucas: Yeah, that's a nice safety net. It's like a linter for your state. You define the rules once, test them in CI, and also run them during dev so you catch violations as soon as they happen. I've seen teams use that for things like 'never store undefined in any key' or 'pagination offset must always be a multiple of the page size.' Luna: That catches bugs before they even reach a test suite. And it documents the state contract for new team members. Lucas: Exactly. And the nice part is, these techniques scale from a single Redux store to micro-frontends or even backend event sourcing. The principles are the same: test the sequence, assert the invariants, fuzz the edges. Luna: One thing I've wondered: with all the shift toward server state and React Query or SWR, does client-side state management testing become less important? Lucas: Not really. Those libraries manage caching and fetching, but they don't replace local UI state. You still have modals, forms, wizards, and multi-step flows. In fact, server state introduces new state management challenges — like stale while revalidate race conditions where a cached response arrives after a fresh fetch and overwrites it. That's a sequence bug. Luna: So the same testing approach applies: define the possible action types, and fuzz sequences. Lucas: Precisely. I've even seen teams use Redux-style middleware to instrument React Query's cache events for testing. It's the same pattern, just different labels. Luna: Alright, let's get concrete. If I'm starting a new project tomorrow, what's the minimal set of state management tests I should write? Lucas: Start with the three most common user flows for each feature. For a checkout flow, that's: add to cart, change quantity, submit order. Write a sequence test for each, then add one edge case where the user submits twice or navigates away mid-submit. Then add invariants for the critical business rules: 'cart total equals sum of item prices', 'order ID is never null after submit', that kind of thing. Luna: And for the invariants, you'd write those as separate assertions that run after every action in the sequence, not just at the end? Lucas: Exactly. Because a bug might introduce a transient violation that gets corrected later — but that transient state could still cause a visual glitch or a re-render issue. Testing the invariant after each action catches those. Luna: That's a good habit. Reminds me of database constraint checks inside transactions. Lucas: It's the same idea. And since we're on the topic of habits, I want to mention something honest: putting together episodes like this, researching concrete examples like that fintech case, takes time. And a handful of listeners already support the show directly through buy me a coffee dot com slash fexingo. That's literally what makes it possible to keep doing this many deep dives without ads. Luna: Yeah, it's a small group, but it makes a big difference. We're grateful for everyone who chips in. Lucas: So, back to the practice. Once you have those core sequence tests, the next level is automation. You can run them in your CI pipeline on every push. And because they're pure state tests, they're fast — milliseconds per test. No browser, no network. Luna: That's a huge advantage over end to end tests, which take minutes. So you get fast feedback on the most brittle part of your app. Lucas: Right. And the best part is, once you have a good set of sequence tests, you can start to automatically minimize them — if a new action is added, the fuzzer can check whether any existing test already covers the same state space. That prevents test bloat. Luna: Do you see teams adopting this as a standard practice, or is it still niche? Luna: Because component tests can't catch what state tests catch. They're complementary. Lucas: Exactly. So my advice: next time your team writes a new feature, before you spin up a full component test, write a sequence test for the state logic. It'll catch the bugs that slip through every other layer. Luna: And if you're refactoring state management, those tests become your safety net for the whole transition. Lucas: That's the big picture. State management testing isn't just about finding bugs — it's about making your state logic explicit, testable, and resilient to the chaos of real user interactions.