If an agent works on your TypeScript project, add two lines to your AGENTS.md: after each change, vitest run --since-last-run; before finishing, a plain vitest run for the whole suite. Vitest 5.1 reruns only the tests affected by what changed since the last run, and it doesn’t need Git to know.
What shipped
Vitest is the test framework of the Vite ecosystem and a common choice in TypeScript projects. Version 5.1 came out on November 2, 2026 with a new option, still marked experimental: --since-last-run.
The CLI docs describe it as a way to “run only the tests whose dependencies were modified since the last run of the test”.
Here’s how it works, according to the affected tests guide:
- The first run executes every test and records which files each one loaded: imports, dynamic imports, mocks, setup files and snapshots.
- On later runs, a test reruns if any of those files is newer than the run that recorded it, or was deleted.
- A test that failed last time keeps rerunning until it passes, even if the fix lives in a file it never recorded.
- New tests, with no record, always run.
The records live in Vitest’s cache. Run vitest run --clearCache and the next run is a full one again.
Why –changed isn’t enough
Vitest already had --changed, which asks Git which files changed and runs the tests that import them. For checking a branch before opening a pull request it’s still the right tool: vitest run --changed origin/main.
The problem is how agents work. The guide puts it well: agents often run in containers or temporary worktrees, commit in the middle of a task and start a new process for every command. Right after a commit, --changed sees nothing pending and runs no tests. Outside a repository, it doesn’t work at all.
--since-last-run ignores Git. It compares against the last time each test ran, so it doesn’t matter whether the agent committed, is in a worktree or is in a folder that isn’t a repository.
How to use it with an agent
1. Upgrade Vitest to 5.1 or later:
npm i -D vitest@^5.1.0
2. Do one full run in a fresh environment. With --since-last-run, the first run already is one, because there are no records yet.
3. Add these two lines to your AGENTS.md (or CLAUDE.md). They’re the ones the docs suggest:
- After a change, run `vitest run --since-last-run` to test only what the change affects.
- Before you finish, run `vitest run` once for the whole suite.
4. If you use package.json scripts, a short one helps the agent pick the right option:
{
"scripts": {
"test": "vitest run",
"test:affected": "vitest run --since-last-run"
}
}
Where it falls short
The guide is upfront about the limits, and there are two:
- Files no test loaded aren’t tracked. A new
.envor a new file in__mocks__isn’t in any record, so changing it triggers nothing. - Modification times can lie. A checkout or an unpacked archive can leave timestamps in the past and hide changes.
The answer to both is the same: finish with the full suite. That’s why the second line in AGENTS.md isn’t optional.
There’s also a practical detail if your agent starts every task in a fresh container: the cache starts empty, so the first loop is always a full run. That’s expected. If you want the savings to carry over between sessions, keep Vitest’s cache folder between runs instead of throwing it away with the container.
My take
I’d add this today to any TypeScript project where an agent works. An agent loops many times per task: change, test, fix, test again. If every loop runs the whole suite, you pay in minutes of waiting and in output tokens the agent has to read. With --since-last-run, each loop tests what it touched and the full suite runs once, at the end.
I like that this was designed with agents in mind from the start, with the AGENTS.md text right there in the docs. And I like that it doesn’t depend on Git, because an agent in a worktree commits whenever it sees fit, and --changed goes blind right after.
What I wouldn’t do is trust the AGENTS.md line for the final run. It’s an instruction, and an agent can skip it. If you want the full suite to run every time before the agent wraps up, put it in a hook the agent can’t skip, and keep --since-last-run for the loops in between. And since the option is experimental, I’d watch the next release notes before adding it to CI as well.
Sources
If you want to see how I work with AI agents without a team, follow me on LinkedIn.
