All jobs

Write Vitest Tests for a Zustand Store

Yes. An agent tests a Zustand store's actions and its startup read from a cookie in Vitest browser mode, with a fresh store per test, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent looks for what already claims to test the store, and finds that nothing runs it. In a React app like satnaing/shadcn-admin, src/stores/auth-store.ts is the one Zustand store: create<AuthState>() with everything nested under auth, a user, an accessToken, and four actions. It is used in three places, the sign-in form, the sign-out dialog and the query cache's 401 handler in src/main.tsx. The repository has 21 test files, and the two that touch auth, sign-out-dialog.test.tsx and user-auth-form.test.tsx, both begin with vi.mock('@/stores/auth-store', ...) and hand the component a fake. So the components are tested and the store's own lines are not. The store is also not plain state: its initializer calls getCookie, runs JSON.parse on the value and uses the result as the starting token, and setAccessToken and reset write and remove that cookie through src/lib/cookies.ts.

The test setup decides how the tests can be written. vite.config.ts turns on Vitest browser mode with the Playwright provider and one Chromium instance, and the test script is vitest run --browser.headless, so tests run in a real page with a real document.cookie, and the agent runs pnpm test:browser:install first, as the workflow in .github/workflows/ci.yml does. That is good news for a cookie-backed store and a problem for the usual way of getting a fresh one. The store reads its cookie once, when the module is first evaluated. The common trick, vi.resetModules() followed by a dynamic import, hands back the same store instance in this setup, so a test that sets a cookie and then imports sees an empty token. The agent finds this by running it, and imports the module with a changing query string, ./auth-store?fresh=1, which the browser treats as a new module each time.

With a fresh store per test, it writes down what the store does. No cookie gives an empty token and a null user. A cookie holding a JSON string becomes the starting token, and the user is still null, because only the token is ever stored. setAccessToken writes the token as a JSON string; setUser writes nothing; reset clears both fields and removes the cookie. Zustand's own reset, setState(getInitialState(), true), restores the fields and leaves the cookie behind, which is why the tests do not rely on it. Two edge cases get tests because they end badly. A cookie that is not valid JSON makes the import itself reject with a SyntaxError, and since main.tsx imports the store, the app would not start. A token containing a semicolon is cut off at the semicolon when written, because setCookie does not encode the value, and the next load fails the same way.

The new file sits beside the store as src/stores/auth-store.test.ts and reuses clearCookies from src/test-utils/cookies.ts, the helper the existing cookie tests use, to remove the store's cookie before each case. It has to pass more than Vitest. eslint.config.js makes no-console and unused variables errors, and the workflow runs pnpm lint, pnpm format:check, the tests and pnpm build, whose tsc -b type-checks test files too. The agent runs all four.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Write Vitest tests for the Zustand store in this React app. Read package.json, pnpm-lock.yaml (zustand and vitest versions), vite.config.ts, eslint.config.js, .github/workflows/ci.yml, src/stores/auth-store.ts, src/lib/cookies.ts and its test, src/test-utils/cookies.ts, src/main.tsx, and the sign-in form and sign-out dialog with their tests first. The existing component tests mock the store; do not change them. Add src/stores/auth-store.test.ts that runs the real store in the project's browser mode. The store reads a cookie when its module is first evaluated, so each test needs a freshly evaluated store: check whether vi.resetModules() gives you one in browser mode, say what you found, and use a way that does. Cover: the starting state with no cookie; the starting token when the cookie holds one; setUser, setAccessToken, resetAccessToken and reset, including what each writes to or removes from document.cookie; that the user is not restored after a reload; a cookie that is not valid JSON; and a token that contains a semicolon. Test what the code does today, clear the store's cookie before each test with the existing clearCookies helper, and change nothing under src except adding the test file. Run pnpm install --frozen-lockfile, pnpm test:browser:install, pnpm test, pnpm lint, pnpm format:check and pnpm build, and paste their output. In the pull request list every behaviour the tests pinned that looks like a bug, with the test's name, and open it.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that adds one file, src/stores/auth-store.test.ts, and changes nothing else. The description lists each test with the behaviour it covers and pastes the output of the test run, the lint, the format check and the build as the agent ran them. A short section explains the fresh-store technique, with the failing attempt beside it, so the next person who adds a store does not rediscover that module resets behave differently in browser mode.

The more useful half is the list of pinned behaviour a maintainer should look at. A malformed cookie throws while the store module loads. A token with a semicolon is truncated on write and breaks the next load. The user object is lost on reload while the token survives, so a returning visitor has a token and no user. Every set replaces the whole auth object, and both components read the store with no selector, so each re-renders on any change to it. None of these is altered by the pull request; each has a test named for what happens now.

A reviewer checks that no test depends on another, by reading that each starts from a cleared cookie and a newly imported module, and that assertions read document.cookie through the same getCookie the app uses. They decide which pinned behaviours are bugs, since fixing one means editing its test in the same commit.

Run it on every merged pull request

A store grows by one action per feature. Add an automation to the Project that holds the repository with a github trigger on pull_request.closed.merged and your default branch in branch_patterns, and a prompt that says: if the merged pull request below added or changed a file under src/stores, write tests for the actions and startup behaviour it changed, in the style of auth-store.test.ts, and open a pull request; if it did not, end with one line. The merged pull request is placed after the prompt as the event and fills in nothing. The trigger fires for work by an owner, member or collaborator, never for a fork or a bot.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

These tests need a real browser, because the thing under test is a cookie. The project already runs its suite in headless Chromium, which means installing a browser and its system libraries before the first test can run, and then iterating on a store that only initializes once per page. The agent does that in its own sandbox: it installs the browser the way the workflow does, runs the suite, reads the failure when the first approach returns a stale store, and tries the next.

What comes back can be checked line by line. The transcript at GET /o/{org}/sessions/{id}/messages holds each command and its output, including the run where the module reset did not work, so the explanation in the pull request rests on something printed. And because the tests describe current behaviour, each oddity in the list is a named test a maintainer can run.

Frequently asked questions

How do I reset a Zustand store between tests?

For plain state, call useStore.setState(useStore.getInitialState(), true) before each test. That is not enough when the initializer reads from somewhere, as this one reads a cookie: the initial state was computed once, and outside storage is not touched. Then you need a newly evaluated module per test, or a store built by a factory function.

Why does vi.resetModules() not give me a fresh store here?

This project runs Vitest in browser mode, where test files load as native modules in Chromium. In the agent's check a dynamic import after vi.resetModules() returned the already-evaluated store. Importing with a different query string each time gives a new evaluation, and the agent types the result with typeof import('./auth-store').

Should the component tests stop mocking the store?

That is a separate decision and this request leaves them alone. Mocking keeps those tests about the component; the new file covers the store. If you want one test that signs in through the real store and reads the cookie afterwards, ask for it in a follow-up to the same session.

More Zustand jobs

The same job on other stacks

Keep reading