From 0ff377b6889bde8a2b62cce99cedcc19ed3c4b39 Mon Sep 17 00:00:00 2001 From: Sebastian Poxhofer Date: Fri, 4 Sep 2026 10:28:56 +0000 Subject: [PATCH] test(linters): forbid file-based snapshot matchers (#45661) * test(linters): forbid file-based snapshot matchers * test(linters): drop inline snapshot recommendation * test(linters): assert on relevant sections instead of full bodies --- .oxlintrc.json | 10 ++++++++++ docs/development/best-practices.md | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.oxlintrc.json b/.oxlintrc.json index bb2fa7d516..ce521c8f1e 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -190,6 +190,16 @@ { "assertFunctionNames": ["expect", "expectTypeOf", "checkKeysSorted"] } ], "vitest/no-conditional-expect": "error", + "vitest/no-restricted-matchers": [ + "error", + { + "toMatchSnapshot": "Use explicit assertions such as toEqual, toMatchObject or toBe so the expected value stays visible in the test file.", + "toThrowErrorMatchingSnapshot": "Use toThrow with the expected message.", + "rejects.toThrowErrorMatchingSnapshot": "Use rejects.toThrow with the expected message.", + "resolves.toMatchSnapshot": "Use explicit assertions such as resolves.toEqual or resolves.toMatchObject so the expected value stays visible in the test file.", + "toMatchFileSnapshot": "Use explicit assertions such as toEqual, toMatchObject or toBe so the expected value stays visible in the test file." + } + ], "vitest/no-standalone-expect": "error", "vitest/prefer-snapshot-hint": "error", "vitest/require-to-throw-message": "error" diff --git a/docs/development/best-practices.md b/docs/development/best-practices.md index 3cf1a8bd8a..db97c73fb3 100644 --- a/docs/development/best-practices.md +++ b/docs/development/best-practices.md @@ -292,9 +292,9 @@ if (end) { - `mockDeep` returns a mock for any property access, so typos in mocked names won't fail the test - Prefer `toEqual` - Use `toMatchObject` for huge objects when only parts need to be tested -- Avoid `toMatchSnapshot`, only use it for: - - huge strings like the Renovate PR body text - - huge complex objects where you only need to test parts +- Do not use snapshot matchers (`toMatchSnapshot`, `toMatchInlineSnapshot`, `toThrowErrorMatchingSnapshot`), write explicit assertions instead + - For huge strings like the Renovate PR body text, assert on the sections the test is about with `toContain`, `toStartWith` or `toEndWith`; compare the whole string with `toBe` only when producing exactly that text is the point of the test + - For huge complex objects where you only need to test parts, use `toMatchObject` - Avoid exporting functions purely for the purpose of testing unless you really need to - Avoid cast or prefer `x as T` instead of `x` cast - Use `partial()` from `test/util` if only a partial object is required