Skip to content
Sarj Code Standards

prefer-test-fixture-reuse

Reuse a requested test fixture instead of constructing another instance of its class.

Why

A second constructor bypasses the fixture's overrides, caching, configuration and cleanup ownership.

Fix

Use the requested fixture. Supply a typed factory fixture when a test requires different configuration or multiple independent instances.

Examples

Before — flagged The test constructs the store its fixture supplied
src/store.test.ts
import { test as base } from "vitest";
class Store {}
const test = base.extend({
store: async ({}, use) => {
await use(new Store());
},
});
test("reads", ({ store }) => {
const other = new Store();
expect(other).toBeDefined();
});
After — preferred Request and use the store fixture
src/store.test.ts
import { test as base } from "vitest";
class Store {}
const test = base.extend({
store: async ({}, use) => {
await use(new Store());
},
});
test("reads", ({ store }) => {
expect(store).toBeDefined();
});