# Why My Screen Tests Rendered a State the App Never Shows

I had two test harnesses for the same React screen, and its tests passed on both: 68 on mine, then 76 on the team's, once I'd added tests for two new features. All green.

The first harness was mine. It filled the Redux store before the first render: part of the user's identity went in as preloaded state, and a `beforeRender` hook loaded the categories and projects. By the time the screen mounted, everything it needed was already there. The second harness was the team's. It rendered the screen into an empty store, then dispatched the user, the categories and the projects, one after another.

The argument for the second one was short: that's how the app behaves. I agreed, moved my tests across, and only then opened the app's startup code. Each harness had got half of it right.

## TL;DR

- Both harnesses passed the screen's tests. Green told me nothing about which one matched the app.
- The app loads the user before any screen exists, and loads the projects after the screen has mounted.
- My harness did both before the render, so it never tested a screen waiting for its data.
- The team's harness did both after the render, so every screen's first render ran with no user, no scope, and as an approver.
- The order that matches the app is identity, then render, then data. Moving one dispatch above `render()` gets you there.
- Read the startup code before you argue about the harness. It's shorter than the argument.

## The setup

The app is a set of React screens for an internal expenses tool. It doesn't call an API directly. Every read and write goes through one data client: run a query, create a record, ask who the current user is.

That makes it pleasant to test. Swap the client for a fake and the app can't tell. Our fake answers the same queries from PGlite, which is Postgres compiled to WebAssembly. Each test seeds it with fixture rows and a persona: who the user is, which team, which role. Tests run in Vitest's browser mode on Chromium through Playwright, so the screen renders in a real browser with real layout, and the screen tests also save screenshots for our docs.

Screens read their state from Redux Toolkit. The user's identity lives in an `app` slice: user id, scope, approver flag, language and so on. The screen in this story lists expense claims grouped by project. It needs the user, because it only shows claims that user submitted, and it needs the projects in scope.

## Before: fill the store, then render

My `renderScreen` took two extra options, `preloadedState` and `beforeRender`, and wrapped every screen in the app's React contexts so a test file didn't have to.

```tsx
const ctx = await renderScreen(ClaimsScreen, {
  persona: "employee",
  preloadedState: { app: { scope: "team" } },
  beforeRender: async ({ store }) => {
    await store.dispatch(loadCategories());
    await store.dispatch(loadProjects([apollo.id]));
  },
});
```

The appeal was that the first render was the finished screen. Tests were short, and assertions had little to wait for.

## After: render, then feed the store

The team's harness renders into a fresh store, inside the database, Redux and theme providers and nothing else. Each test then does what the app's startup does, in sequence:

```tsx
const ctx = await renderScreen(ClaimsScreen, { persona: "employee" });
ctx.store.dispatch(setIdentity({ ...ctx.persona.identity, userId: ME }));
await ctx.store.dispatch(loadCategories());
await ctx.store.dispatch(loadProjects([apollo.id]));
```

The screen mounts, has nothing, and fills in as the data arrives. That's what a user sees too: a list that starts empty and then fills. Each test file wraps its own screen in the translation context.

I moved the tests across, adding eight along the way for the new features and the doc screenshots. All 76 passed, three runs out of three. Then I went to write down why this was the faithful version, and opened the layout.

## What the app actually does

Three steps, in this order:

1. The layout loads the user's identity through the data client and shows a spinner until it has it. No screen exists yet.
2. The layout wraps everything below it in four React contexts: window size, translations, and two app-wide helpers.
3. A route component mounts the screen and, next to it, a data loader that fetches the categories and projects. The screen is on the page before its projects arrive.

So the app's order is identity, render, data.

![Three boot orders compared. The app loads identity, then renders the screen, then loads projects. My harness loaded identity and projects before the first render, so the screen was never empty. The team's harness rendered first, then dispatched identity and projects, so the first render ran with no user, no scope and the approver flag on.](https://cdn.hashnode.com/uploads/covers/62db053c38759e6b49828665/a8d8f2cb-1dc5-4a17-b961-b9d965dce9dc.svg align="center")

Mine had identity right and data wrong. The team's had data right and identity wrong. Neither matched.

## What the first render sees

In the team's harness, the screen's first render reads the `app` slice's defaults. Ours are:

- `userId: null`
- `scope: null`
- `isApprover: true`

That last one is the surprise. For one render, every screen runs as an approver with no user and no scope. The app never shows a screen in that state, because the spinner is still up.

It's also a default worth questioning on its own. A privilege flag that starts as `true` fails open: anything that reads it before identity arrives gets the privileged branch. The spinner protects every screen today, so nothing is broken in the app. The safer default is `false`, so that a component which reads it too early gets the cautious path instead.

Most screens survive that first render. The one I was testing waits for a user id before it queries, so nothing goes out. A screen that queries on mount could send a query with no user in it, or start down the approver branch and then switch. A test could fail for a reason the app can't produce, or pass because a defensive check covers a state the app never reaches.

I'd even written a test for it: "waits for the logged-in user before querying". It boots the screen with no user, checks that no query went out, then dispatches the user and checks the query. It passes. It's also testing a state the app's own spinner prevents. The check isn't wrong to have. It just isn't coverage of anything a user will see.

## What my harness hid

My harness had the opposite problem. With projects loaded before the render, the screen never spent a moment empty. That sounds like a feature until you test the empty state.

After the move to render-first, an assertion like "shows the no-data message when nothing qualifies" could pass straight away. Before its projects request goes out, this screen shows the same no-data message it shows when there's nothing to show, so a screen that went on to show rows would still have passed. I had to make those tests wait for the claims query to go out before checking for the empty state.

That points at something bigger than the harness. The screen does show a spinner while projects load, but it only checks for `"loading"`, and the projects slice starts as `"idle"`. Between the screen mounting and the first request going out, it says "nothing here". That's a gap in the screen, not in the tests, and my harness hid it because the screen was never empty.

## Why both were green

Testing Library's `findBy` queries and `waitFor` retry until the page matches or the time runs out. A test that checks the finished screen sees the finished screen, whatever route it took to get there.

The in-between states are exactly what an end-state assertion skips: the first render on defaults, and the empty screen before the projects request goes out. Green told me the destination was right. It didn't tell me the route was the app's.

## The fix: one dispatch, moved up

Identity before the render, data after it:

```tsx
const store = makeStore();
// The layout doesn't mount a screen until identity has loaded. Do the same.
store.dispatch(setIdentity({ ...persona.identity, ...options.identity }));

render(<Providers store={store}><Screen /></Providers>);
```

![The fixed harness lined up against the app. Both now load identity, then render the screen, then load projects. Identity is dispatched before render(), the projects are dispatched by the test after it, and empty-state tests wait for the claims query before asserting.](https://cdn.hashnode.com/uploads/covers/62db053c38759e6b49828665/28446d2a-2100-4f38-939f-e6dc90d7cd24.svg align="center")

Tests that need a different identity, such as another team or a single-project scope, pass it as an option instead of dispatching it afterwards. Loading the data stays in the test, after the render, so the empty state stays testable. Apart from that option, nothing else in the team's harness needs to change.

## What about the contexts?

The other difference was the React contexts. My harness wrapped every screen in the layout's contexts. The team's leaves that to each test file, and so far each file adds only the translation context, which is all those screens need.

It works. It also means four test files carry the same fifteen-line wrapper. The contexts nobody provides fall back to their defaults. The window context says "standard" whatever the viewport, so a phone-width test would still render the desktop layout. The two with no default are `null`, so the first screen that reads one of them would break.

That's a conversation for when a test needs one of those screens. It doesn't change what the tests prove today.

## What I got wrong

I accepted "it's how the app behaves" without opening the app. It was the better argument of the two, so I switched, and it was still only half true.

The startup code was a few hundred lines across three files. Reading it took less time than the conversation about it. I'd have walked into that conversation with the boot order written down, and we'd have agreed on the fix in one go.

## What carries over

1. **Green on both doesn't pick a winner.** Two harnesses that both pass can still disagree about what the screen goes through.
2. **Write down the app's startup order first.** What exists before the first render, and what arrives after it.
3. **Match the order, not just the data.** The same fixtures in a different order give the screen different states.
4. **Check the slice defaults.** They're what a screen sees if it mounts early, and a privilege flag among them should default to the cautious value.
5. **Test the waiting states on purpose.** If the app loads data after mount, the tests should too, and empty-state assertions need to wait for the query first.
6. **Treat "not started" as loading.** If idle and empty look the same on screen, they look the same to your tests.
7. **Be wary of tests for states the app can't reach.** They pass, they look like coverage, and they cover nothing a user sees.
8. **Read the code before you argue about it.** It's usually shorter.

*I'm Ashish Kr Jha, a software engineer. I build things in production and then have to live with them, which is the part that actually teaches you something. More at* [*ashishkrjha.dev*](https://ashishkrjha.dev)*.*
