Index of every page on this site: llms.txt
All skills
fubits1 avatar

/vitest

@a024d57
by Iljafubits1/svelte-skills11 stars
2
Run it yourself

Vitest test.projects, browser mode with playwright(), optimizeDeps, separate vitest.config from vite.config. Use when editing vitest config, debugging flaky browser tests, upgrading @vitest/*, or splitting node vs browser projects.

Use this Skill: https://skilld.dev/gh/fubits1/svelte-skills/vitest

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesmocking.md

≈714 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Mocking

Placement and hoisting

  • vi.mock, vi.hoisted, vi.unmock go at the top level. Inside describe, test, or a function they throw in Vitest 5 (was defined outside of the module's top level scope). vi.doMock may go anywhere

  • Vitest hoists vi.mock above the static imports, so its factory runs before any const in the file exists. A factory that references a module-level const fails with a static import:

    Error: [vitest] There was an error when mocking a module. If you are using "vi.mock" factory,
    make sure there are no top level variables inside, since this call is hoisted to top of the file.
    Caused by: ReferenceError: Cannot access 'fakeAdd' before initialization

Two fixes, both measured:

// a) vi.hoisted: create the value in the hoisted scope
import { expect, test, vi } from "vitest";
import { add } from "./math";

const { fakeAdd } = vi.hoisted(() => ({ fakeAdd: vi.fn(() => 42) }));
vi.mock("./math", () => ({ add: fakeAdd }));

test("hoisted", () => {
  expect(add(1, 2)).toBe(42);
});
// b) dynamic import: the mocked module loads after the const exists
import { expect, test, vi } from "vitest";

const fakeAdd = vi.fn(() => 42);
vi.mock("./math", () => ({ add: fakeAdd }));

const { add } = await import("./math");

test("dynamic import", () => {
  expect(add(1, 2)).toBe(42);
});

Use (b) when the factory needs several module-level fakes or stores; use (a) when the module must be a static import.

Svelte and SvelteKit seams

  • SvelteKit's client router does not start in a Vitest browser run (observed in a SvelteKit app; not part of the measured set), so components that call $app/navigation or read $app/state need those modules mocked. Capture callbacks through vi.hoisted to drive them by hand:

    const nav = vi.hoisted(() => ({ afterNavigate: [] as Array<() => void> }));
    vi.mock("$app/navigation", () => ({
      goto: vi.fn(),
      afterNavigate: (fn: () => void) => nav.afterNavigate.push(fn),
    }));
    // later: nav.afterNavigate.forEach((fn) => fn())
  • Replace a heavy child component with a fixture: vi.mock("./Chart.svelte", () => import("./fixtures/ChartStub.svelte")). The fixture can count mounts or render its props for assertions

Mock state between tests

  • Vitest 5 defaults to clearMocks: true: call history is cleared before every test. beforeEach(() => vi.clearAllMocks()) is redundant unless the config sets clearMocks: false. Calls recorded in a setup file, at module scope, or in beforeAll are gone by the time a test asserts; read them at module scope or trigger them inside the test
  • vi.restoreAllMocks() restores vi.spyOn originals. It does not touch vi.fn() mocks created in vi.mock factories; their history is reset by clearMocks (or vi.clearAllMocks() when that default is off)

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill provides configuration guidelines and best practices for Vitest, focusing on browser mode, Storybook integration, and robust assertion patterns. No security risks or malicious behaviors were detected.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: MEDIUM · 1 issue

Signed by skilld at a024d57. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Activeupdated last month
user-invocable
true
Other metadata
compatibility
Written for Vitest 5 (Node 22.12+, 24 or 26+; Vite >=6.4; factory browser provider, test.projects)

README badge

README badge for fubits1/svelte-skills/vitest