eslint-snapshot-test provides a utility for creating and managing snapshot tests specifically for ESLint rules. It allows developers to verify the output of their custom ESLint rules, including reported errors, warnings, and autofix suggestions, by comparing them against stored snapshots. This ensures consistent rule behavior across code changes. The current stable version is 3.2.0, and it appears to have an active but irregular release cadence, often for dependency updates or minor features. A key differentiator is its focused API for interacting with ESLint's Linter directly, enabling fine-grained control over parsing options, rule options, and even file names during testing, which is crucial for robust ESLint rule development.
npm install eslint-snapshot-testVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing SnapshotCreator, marking code with a rule, and rendering snapshots with different options. It shows how to test basic rule application and rule options.
Review the `render()` method usage and update your test logic to align with the unified API. Previously, `fix` might have been a separate step or part of a different return structure.
Instead of `numError`, iterate through the `lintMessages` array (which contains detailed ESLint `Linter.LintMessage` objects) and count its length, or inspect the messages directly for specific error criteria.
Ensure `npm install @typescript-eslint/parser @typescript-eslint/utils` (or `yarn add`) and include `parser: '@typescript-eslint/parser'` in the `eslintOptions` object passed to `SnapshotCreator`.
Import the specific rule object from its module path (e.g., `import { semi } from 'eslint/lib/rules/semi';` or your custom rule's file) and pass the imported object to the `rule` property.Ensure the rule object is correctly imported and passed. For built-in ESLint rules, the path might be `eslint/lib/rules/<rule-name>`.
Add or correct the `parser` property in your `eslintOptions` object. For TypeScript, use `parser: '@typescript-eslint/parser'` and ensure `@typescript-eslint/parser` is installed. For modern JavaScript, `parser: 'espree'` might be sufficient if not using experimental syntax.