Registry / testing / eslint-snapshot-test

eslint-snapshot-test

JSON →
library3.2.0jsnpmunverified

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-test
INSTALL
IMPORT
SIG · ESLINT-SNAPSHOT-TE
E
eslint-snapshot-test
testingjavascriptv3.2.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SnapshotCreator
✓ import { SnapshotCreator } from 'eslint-snapshot-test';
✗ const SnapshotCreator = require('eslint-snapshot-test').SnapshotCreator;
The library primarily uses named exports. While CommonJS `require` might technically work in some environments, ESM is the idiomatic way to import in modern TypeScript/JavaScript projects, especially given the peer dependency on `@typescript-eslint/utils` which is often ESM-focused.
SnapshotResult
✓ import type { SnapshotResult } from 'eslint-snapshot-test';
Type import for the result object returned by `render()`. Essential for TypeScript users to correctly type the snapshot test outcomes.
ESLintOptions
✓ import type { ESLintOptions } from 'eslint-snapshot-test';
Type import for the configuration object passed to the `SnapshotCreator` constructor. Useful for ensuring correct ESLint configuration types.

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.

import { SnapshotCreator } from 'eslint-snapshot-test'; // Assuming 'eslint' is installed, you can import built-in rules like 'semi'. // For custom rules, import your rule object directly. import { semi } from 'eslint/lib/rules/semi'; // Note: path might vary based on eslint version const eslintOptions = { parserOptions: { ecmaVersion: 2017, sourceType: 'module', }, // Use @typescript-eslint/parser if testing TS rules and @typescript-eslint/utils is installed // parser: '@typescript-eslint/parser', }; const snapshotCreator = new SnapshotCreator(eslintOptions); const codeToTest = "const a = 1"; // Create a snapshot for a simple code snippet with the 'semi' rule const { snapshot: simpleSnapshot } = snapshotCreator .mark({ code: codeToTest, ruleName: 'semi', rule: semi }) .render(); console.log('Simple Snapshot:\n', simpleSnapshot); // Create a snapshot with specific rule options and severity const { snapshot: optionsSnapshot } = snapshotCreator .mark({ code: 'let b = 2', ruleName: 'semi', rule: semi }) .withOptions(["always"], "warn") // Ensure semicolons are always required, warn level .render(); console.log('Options Snapshot:\n', optionsSnapshot); // Example of a code snippet that would trigger a fixable error with 'semi' rule const codeToFix = 'const c = 3'; const { snapshot: fixableSnapshot } = snapshotCreator .mark({ code: codeToFix, ruleName: 'semi', rule: semi }) .withOptions(["always"], "error") .render(); console.log('Fixable Snapshot (will show fixed code if rule provides one):\n', fixableSnapshot); // In a real test environment (e.g., Jest), you would typically use: // expect(simpleSnapshot).toMatchSnapshot();
Debug
Known issues
breakingThe API for handling fix actions was unified into the `render` method, potentially requiring changes if you were using older, separate fix APIs.
fix
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.
affects: >=3.0.0
breakingThe return type for `numError` was replaced with `lintMessages` in `v1.1.1`. If you were relying on `numError` to count issues, you need to adapt to the new `lintMessages` structure.
fix
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.
affects: >=1.1.1
gotchaWhen testing TypeScript rules or code, you must install `@typescript-eslint/parser` and configure it in `eslintOptions.parser`. Also, `@typescript-eslint/utils` is a peer dependency and must be installed separately.
fix
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`.
affects: >=1.0.0
gotchaWhen providing a custom rule object to `mark({ rule })`, ensure it is the actual rule definition object (e.g., `semi` from `eslint/lib/rules/semi`) and not just its name string. Incorrectly passing a rule name will lead to errors.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'meta')
The `rule` property passed to `mark()` was either `undefined` or not a valid ESLint rule object.
fix
Ensure the rule object is correctly imported and passed. For built-in ESLint rules, the path might be `eslint/lib/rules/<rule-name>`.
Error: "parser" is a required option for `Linter.verifyAndFix` when parsing code in a non-default language or using an experimental feature.
The `parser` option was not specified in `eslintOptions`, or the specified parser could not be resolved, especially for TypeScript or modern JavaScript syntax.
fix
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.
Upgrade
Version history
3.2.0latest on npm
Audit
Dependencies
@typescript-eslint/utilsrequiredRequired for parsing TypeScript code and potentially for defining ESLint rule types if the tested rules are TypeScript-aware. It is a peer dependency, meaning the user must install it.
Agent activity
4 hits · last 30 days
node
4
Resources
eslint-snapshot-test — npm install eslint-snapshot-test · libregistry