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
This commit is contained in:
Sebastian Poxhofer
2026-09-04 10:28:56 +00:00
committed by GitHub
parent df8c4051ff
commit 0ff377b688
2 changed files with 13 additions and 3 deletions
+10
View File
@@ -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"
+3 -3
View File
@@ -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 `<T>x` cast
- Use `partial<T>()` from `test/util` if only a partial object is required