Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

vitest/require-local-test-context-for-concurrent-snapshots

Require the expect of the test context for snapshots in concurrent tests

Concurrent tests run at the same time, so the expect you import from "vitest" can't tell which test a snapshot belongs to. The snapshot can be saved under another test's name, or compared with another test's snapshot. Use the expect that Vitest passes to the test function, which is tied to that test.

The rule checks toMatchSnapshot, toMatchInlineSnapshot, toMatchFileSnapshot, toThrowErrorMatchingSnapshot and toThrowErrorMatchingInlineSnapshot, in test.concurrent, it.concurrent and describe.concurrent.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"vitest/require-local-test-context-for-concurrent-snapshots" = "error"

Examples

Reported:

import { describe, expect, test } from "vitest";

test.concurrent("renders the header", () => {
  expect(render("header")).toMatchSnapshot();
});

describe.concurrent("footer", () => {
  test("renders the links", () => {
    expect(render("links")).toMatchSnapshot();
  });
});

Not reported:

import { expect, test } from "vitest";

test.concurrent("renders the header", ({ expect }) => {
  expect(render("header")).toMatchSnapshot();
});

test.concurrent("renders the footer", (context) => {
  context.expect(render("footer")).toMatchSnapshot();
});

test("renders the sidebar", () => {
  expect(render("sidebar")).toMatchSnapshot();
});