# Write Unit Tests for a Vuex Store

Yes. The agent tests each mutation, action and initial-state branch of your Vuex modules with the services mocked, and reports the state bugs the tests expose.

[Run this on Rush](https://cloud.getrush.ai/run/write-unit-tests/vuex/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from what the repository can already run. In an app like bezkoder/vue-3-authentication-jwt the answer is nothing: `package.json` has `serve`, `build` and `lint` scripts and no test runner, and there is no test file anywhere. The store is one namespaced module, `src/store/auth.module.js`, with three actions (`login`, `logout`, `register`) and five mutations, built on `src/services/auth.service.js`, which calls axios and writes the signed-in user to `localStorage`. The project builds with Vue CLI 4.5, so there is no Vite config to hook a runner into.

It adds Vitest as a dev dependency and nothing else. Store modules and services here are plain JavaScript, so Vitest runs them without a config file and without a Vue plugin, and the Vue CLI build is untouched. The agent checks the Node version first, because the Vitest release current at the time of writing declares `^22.12.0 || ^24.0.0 || >=26.0.0` for Node in its `engines`. It adds a `test` script that calls `vitest run`. Component tests are a different matter: compiling `.vue` files under Vitest needs `@vitejs/plugin-vue`, whose peer range starts at Vue 3.2.25, and this lockfile resolves `vue` to 3.0.11. The agent says that component tests wait on a Vue upgrade and keeps this job to the store and the services.

The first test it writes fails for an instructive reason. Line 3 of the module reads `localStorage` when the file is imported, and Vitest's default environment is Node, where that global does not exist, so the import itself throws. The agent stubs `localStorage` with `vi.stubGlobal`, and because the initial state is computed once at import, it calls `vi.resetModules()` and imports the module again inside each test. That is also what lets it cover both branches of the initial state: nothing stored gives `{ status: { loggedIn: false }, user: null }`, and a stored user gives a logged-in state.

With `auth.service` replaced through `vi.mock`, the action tests assert the contract the components depend on. `login` resolves with the user and leaves it in state. On failure it rejects with the very error object the service threw, which matters because `Login.vue` reads `error.response.data.message` from it. `register` resolves with `response.data`. Two findings come out of writing these. The module passes its state as an object, not a function, so two stores created from one import share it; the agent demonstrates that with a test and keeps every other test isolated by re-importing. And `registerSuccess` sets `loggedIn` to false while leaving `user` in place, a combination the UI only avoids because `Register.vue` redirects signed-in users away. The agent pins the current behaviour in a test, labels it as a finding, and changes no production code.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write unit tests for the Vuex store in this Vue app. Read package.json, the lockfile, the build config, every file under src/store and src/services, and each component that dispatches an action, so you know which promise results and error shapes the components rely on. Say what test tooling exists. If there is none, add the lightest runner that can test plain JavaScript modules without changing the build, check its Node requirement against this machine, and add a test script. Do not upgrade vue or the build tool; if component tests would need that, say so and leave them out. Cover every mutation, every action on both its success and failure path with the HTTP service mocked, and every branch of the initial state. If a module reads a browser global at import time, stub it and re-import the module per test instead of changing the module. Assert what each action resolves or rejects with, not only what it commits. Also test the service functions with axios mocked, including what they write to localStorage. Keep production code unchanged. When a test reveals behaviour that looks unintended, such as state shared between store instances or a mutation that leaves state inconsistent, keep a test that documents the current behaviour, mark it clearly, and list it under findings. Run the tests and open a pull request with the test output, the list of cases per file, the findings, and what is still untested and why.",
  "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

The pull request adds a `tests` directory with one spec per store module and per service, a `test` script and the Vitest dev dependency in `package.json`, and the lockfile change. No file under `src` is modified, which the changed-files list shows at a glance. The description quotes the full `vitest run` output and lists the cases under each file, so a reviewer can see that the failure path of every action is there and not only the happy one.

The findings section is the part with lasting value. Each entry names the file and line, the test that demonstrates it, and what a fix would look like, without making it: `state` given as a function so store instances stop sharing it, and a decision on whether `registerSuccess` should clear `user`. A reviewer decides which of these become issues. Tests that document a questionable behaviour say so in their names, so nobody later mistakes them for a specification.

The last section says what is not covered. Here that is every `.vue` component, with the reason: the Vue version in the lockfile is below what the Vitest Vue plugin accepts. It also notes that the repository has no CI workflow, so the new script runs only when someone runs it until one is added.

## Run it on every weeknight

Store tests go stale as modules are added. Add an automation to the Project in the console with a `schedule` trigger, for example `cron` set to `0 2 * * 1-5` with your `timezone`, and a saved prompt that asks for tests on store modules and services changed since the last run, production code unchanged, findings listed. Each fire starts one run in the Project's repository and the result is a pull request waiting in the morning.

A schedule can fire at most hourly, and turning an automation back on moves it to its next slot instead of replaying missed ones, so a paused week does not produce a burst. If the nightly pull requests should not pile up, say in the prompt to add to the open test pull request's branch when one exists.

## Why run it on Rush?

Tests are only evidence if they ran. The agent installs the runner, executes the suite and puts the output in the pull request, and the commands behind it are in the session's transcript at `GET /o/{org}/sessions/{id}/messages`. A test that asserts a rejection with the original error object is not something to accept on reading alone; here it has been run against the real module, with only the network layer mocked.

Doing it in a sandbox also keeps an awkward install off your machine. Adding a modern test runner to a project pinned to an old Vue CLI means a lockfile rewrite and possibly a Node version the rest of your work does not use. The run does that on a clean checkout and hands back a diff. If the suite needs a Node the project does not otherwise require, the pull request says which, and that is a fact found by trying, not by guessing.

## Frequently asked questions

### Why Vitest and not Jest for a Vue CLI project?

For plain JavaScript store modules either works. Vitest needs no Babel or transform setup for ES modules and no config file for this job, so it adds one dev dependency and one script. If your project already runs Jest through the Vue CLI unit plugin, the agent uses that and adds nothing.

### Will it change my store to make it testable?

No. The prompt keeps production code unchanged. Where a module reads `localStorage` at import or shares a state object, the tests work around it with stubs and re-imports, and the pull request lists the change that would make the workaround unnecessary.

### Does it test components that use the store?

Only if the project can compile `.vue` files under the test runner. In the fixture described here the Vue version is too old for the plugin that does that, so the agent stops at the store and services and says what upgrading would allow.

### How does it test actions that call an API?

It replaces the service module with a mock, so no request leaves the sandbox. Each action is run once with the mock resolving and once with it rejecting, and the test asserts both the resulting state and what the action's promise settled with.

## More Vuex jobs

- [Migrate a Vue 3 App From Vuex to Pinia](https://getrush.ai/run/migrate-vuex-to-pinia/vuex/)

## The same job on other stacks

- [Write Unit Tests for an ASP.NET Core Project](https://getrush.ai/run/write-unit-tests/aspnet-core/)
- [Write Unit Tests for an Axum + SQLx API](https://getrush.ai/run/write-unit-tests/axum/)
- [Write Unit Tests for a C++ Project Built with Bazel](https://getrush.ai/run/write-unit-tests/bazel/)
- [Write Tests for a Cloudflare Worker With Durable Objects](https://getrush.ai/run/write-unit-tests/cloudflare-workers/)
- [Write Unit Tests for a Cobra CLI](https://getrush.ai/run/write-unit-tests/cobra/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
