agentleFS
qivora-labsqivora-labs/agentinit/.cursor/rules/agentinit-vitest.mdc

agentinit / rules

Cursor rule1 starsChanged 12 days agoMITView on GitHub

Behavior assertions, top-level vi.mock, and fake timers.

The file

We split this agentinit-vitest.mdc into the rules it gives an agent

3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    ALWAYS: call vi.mock at the top level of the test file, with vi imported from vitest, and only for a module the unit does not own.

    line 13
  2. Very specific:

    ALWAYS: call vi.useFakeTimers() before code that schedules a timer, then advance with vi.advanceTimersByTime or vi.runAllTimers.

    line 14
  3. Very specific:

    ALWAYS: run the suite with pnpm test.

    line 16

≈ “Common” is an estimate: at least 1 in 5 of the 6,668 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
agentinit-vitest.mdc
---
description: Behavior assertions, top-level vi.mock, and fake timers.
globs: "**/*.test.tsx,**/*.spec.tsx,*.test.ts,*.spec.ts,src/**/*.test.ts,src/**/*.spec.ts,app/**/*.test.ts,app/**/*.spec.ts,lib/**/*.test.ts,lib/**/*.spec.ts,server/**/*.test.ts,server/**/*.spec.ts,pages/**/*.test.ts,pages/**/*.spec.ts,components/**/*.test.ts,components/**/*.spec.ts,test/**/*.test.ts,test/**/*.spec.ts,tests/*.test.ts,tests/unit/**/*.test.ts,tests/integration/**/*.test.ts,__tests__/**/*.test.ts,__tests__/**/*.spec.ts"
alwaysApply: false
---

## Boundary
`describe` groups the tests for one unit. `it` and `test` are the same function. `vi.mock` is hoisted to the top of the file and runs before imports. `vi.useFakeTimers` replaces `setTimeout`, `setInterval`, and `Date` until `vi.useRealTimers`. `toMatchSnapshot` compares serialized output with a file stored next to the test.

## Directives
- **ALWAYS** group one unit's tests in a `describe` named for that unit. **ALWAYS** name `it` or `test` after a result the caller can observe, such as `returns 0 for an empty cart`. **NEVER** name a test after an internal call.
- **ALWAYS** assert an observable result: the returned value, the thrown error, or a callback the caller passed in. **NEVER** assert the order of calls inside the unit.
- **ALWAYS** call `vi.mock` at the top level of the test file, with `vi` imported from `vitest`, and only for a module the unit does not own. **NEVER** mock the module under test.
- **ALWAYS** call `vi.useFakeTimers()` before code that schedules a timer, then advance with `vi.advanceTimersByTime` or `vi.runAllTimers`. **ALWAYS** call `vi.useRealTimers()` before the file moves on.
- **PREFER** `expect(value).toEqual` when the expected value can be written in the test. Use `toMatchSnapshot` only for a large stable structure. **NEVER** accept a snapshot update without reading the diff.
- **ALWAYS** run the suite with `pnpm test`.

## Contract
```ts
import { afterEach, describe, expect, it, vi } from "vitest";
import { priceAfter } from "./price-after";

vi.mock("./rates", () => ({
  loadRate: () => 2,
}));

function runLater(callback: () => void, ms: number) {
  setTimeout(callback, ms);
}

describe("priceAfter", () => {
  it("returns the price times the rate", () => {
    expect(priceAfter(10)).toBe(20);
  });
});

describe("runLater", () => {
  afterEach(() => {
    vi.useRealTimers();
  });

  it("runs the callback after one second", () => {
    vi.useFakeTimers();
    const callback = vi.fn();
    runLater(callback, 1000);
    vi.advanceTimersByTime(1000);
    expect(callback).toHaveBeenCalledTimes(1);
  });
});

// ❌ Anti-pattern: a hoisted mock of the unit, and a name that describes an internal call
// vi.mock("./price-after");
// it("calls loadRate", () => {
//   expect(true).toBe(true);
// });
```

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.