---
title: "Test a Rule"
description: "Test Rules with fixture projects from vite-doctor/testkit, and run Rules on test files."
canonical_url: "https://vite-doctor.onmax.me/extending/testing"
---
# Test a Rule

> Test Rules with fixture projects from vite-doctor/testkit, and run Rules on test files.

`vite-doctor/testkit` writes fixture files to a temporary project, runs Doctor with only the Rules you pass, and returns the Doctor Run result:

```ts [doctor/extension.test.ts]
import { expect, test } from "vitest";
import { runProjectFixture, runRuleFixture } from "vite-doctor/testkit";
import acme, { noLegacyClient } from "./extension";

test("reports legacy client imports", async () => {
  const result = await runRuleFixture({
    rule: noLegacyClient,
    framework: "vite",
    files: { "src/client.ts": 'import { createClient } from "acme/legacy";\n' },
  });
  expect(result.diagnostics.map((diagnostic) => diagnostic.code)).toEqual(["ACME0001"]);
});

test("runs the published extension", async () => {
  const result = await runProjectFixture({
    framework: "vite",
    extensions: [acme],
    files: { "src/client.ts": 'import { createClient } from "acme";\n' },
  });
  expect(result.diagnostics).toEqual([]);
});
```

| Option | What it does |
| --- | --- |
| `framework` | `vite`, `vue`, `nitro`, or `nuxt`. Defaults to `vue`. |
| `dependencies` | Entries to add to the generated `package.json`. |
| `config` | Doctor config, such as `extends`. |
| `run` | Doctor Run options. The rule cache stays in memory unless you pass `run: { cache: true }`. |

## Share a fixture project

`runRuleFixture` and `runProjectFixture` write a new project and detect its Project Inventory on every call. Most Rule tests change one source file, so `createProjectFixture` writes the project and detects its inventory once, then reuses both for every run:

```ts [doctor/extension.test.ts]
import { afterAll, expect, test } from "vitest";
import { createProjectFixture } from "vite-doctor/testkit";
import { noLegacyClient } from "./extension";

const project = createProjectFixture({ framework: "nuxt", files: { "app/.gitkeep": "" } });
afterAll(() => project.dispose());

test.each(["acme/legacy", "acme/legacy/client"])("reports %s", async (entry) => {
  const result = await project.run({
    rule: noLegacyClient,
    files: { "app/pages/index.vue": `<script setup>import "${entry}"</script>` },
  });
  expect(result.diagnostics.map((diagnostic) => diagnostic.code)).toEqual(["ACME0001"]);
});
```

`createProjectFixture` takes `framework`, `dependencies`, and `files`, and its files stay on disk for every run. `run` takes `rule` or `rules`, `extensions`, `config`, and `run` like `runProjectFixture`, adds its own `files`, runs Doctor on the whole project, and deletes its files before the next run starts. Runs on one fixture project never overlap.

The Project Inventory comes from the fixture project's own files, so run files cannot change it. Put the files that shape it in `createProjectFixture`: dependencies, `nuxt.config.ts`, `tsconfig.json`, Nuxt `server/` routes, or the Nuxt `app/` directory, which `app/.gitkeep` creates without adding a source file. When a test needs its own inventory, use `runRuleFixture` or a separate fixture project. A run file that would replace a fixture project file throws. Call `dispose()` in `afterAll` to delete the project.

## Test files

Doctor skips test code by default, so framework Rules don't misfire on mocks and test setup. Test code means files in `test`, `tests`, `__tests__`, and `test-utils` folders, and `*.test.*` and `*.spec.*` files. A file Rule about source text that applies to tests too, such as a policy on comments or naming, declares `meta.includeTestFiles: true`:

```ts
createRule({
  meta: {
    id: "acme/no-banned-words",
    title: "Banned words",
    category: "style",
    severity: "info",
    includeTestFiles: true,
  },
  create(ctx) {
    // runs on src/foo.ts, src/foo.test.ts and tests/setup.ts
  },
});
```

Only Rules that declare it run on test files, and Doctor reads and parses test files only when such a Rule is enabled. Manifest and workspace Rules ignore the field.

- Test files never reach other Rules, the Project Inventory, the workspace graph, or `--analyses`, and `ctx.inventory.sourcePaths()` doesn't list them.
- Fixtures, `__snapshots__`, type-test folders such as `test-d`, test files inside Nuxt module sources, and paths excluded through `exclude` or `.gitignore` stay skipped.
- Overrides, `doctor-disable` directives, `--changed`, `--rules`, baselines, and the analysis cache work on test files as on other files.
- In the report, `scope.files` counts a test file only when such a Rule analyzed it.

The testkit runs Rules through the same path, so a fixture file such as `src/foo.test.ts` reaches a Rule that declares `includeTestFiles` and no other.


## Sitemap

See the full [sitemap](https://vite-doctor.onmax.me/sitemap.md) for all pages.
