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#
example.js2 problems
import { describe, expect, test } from "vitest";
test.concurrent("renders the header", () => {
expect(render("header")).toMatchSnapshot()Require local Test Context for concurrent snapshot tests;
});
describe.concurrent("footer", () => {
test("renders the links", () => {
expect(render("links")).toMatchSnapshot()Require local Test Context for concurrent snapshot tests;
});
});example.jsNo problems
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();
});