{"id":13149,"library":"eslint-snapshot-test","title":"ESLint Rule Snapshot Testing","description":"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.","status":"active","version":"3.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/fa93hws/eslint-snapshot-test","tags":["javascript","eslint","snapshot-test","typescript"],"install":[{"cmd":"npm install eslint-snapshot-test","lang":"bash","label":"npm"},{"cmd":"yarn add eslint-snapshot-test","lang":"bash","label":"yarn"},{"cmd":"pnpm add eslint-snapshot-test","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required 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.","package":"@typescript-eslint/utils","optional":false}],"imports":[{"note":"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.","wrong":"const SnapshotCreator = require('eslint-snapshot-test').SnapshotCreator;","symbol":"SnapshotCreator","correct":"import { SnapshotCreator } from 'eslint-snapshot-test';"},{"note":"Type import for the result object returned by `render()`. Essential for TypeScript users to correctly type the snapshot test outcomes.","symbol":"SnapshotResult","correct":"import type { SnapshotResult } from 'eslint-snapshot-test';"},{"note":"Type import for the configuration object passed to the `SnapshotCreator` constructor. Useful for ensuring correct ESLint configuration types.","symbol":"ESLintOptions","correct":"import type { ESLintOptions } from 'eslint-snapshot-test';"}],"quickstart":{"code":"import { SnapshotCreator } from 'eslint-snapshot-test';\n// Assuming 'eslint' is installed, you can import built-in rules like 'semi'.\n// For custom rules, import your rule object directly.\nimport { semi } from 'eslint/lib/rules/semi'; // Note: path might vary based on eslint version\n\nconst eslintOptions = {\n  parserOptions: {\n    ecmaVersion: 2017,\n    sourceType: 'module',\n  },\n  // Use @typescript-eslint/parser if testing TS rules and @typescript-eslint/utils is installed\n  // parser: '@typescript-eslint/parser',\n};\n\nconst snapshotCreator = new SnapshotCreator(eslintOptions);\n\nconst codeToTest = \"const a = 1\";\n\n// Create a snapshot for a simple code snippet with the 'semi' rule\nconst { snapshot: simpleSnapshot } = snapshotCreator\n  .mark({ code: codeToTest, ruleName: 'semi', rule: semi })\n  .render();\n\nconsole.log('Simple Snapshot:\\n', simpleSnapshot);\n\n// Create a snapshot with specific rule options and severity\nconst { snapshot: optionsSnapshot } = snapshotCreator\n  .mark({ code: 'let b = 2', ruleName: 'semi', rule: semi })\n  .withOptions([\"always\"], \"warn\") // Ensure semicolons are always required, warn level\n  .render();\n\nconsole.log('Options Snapshot:\\n', optionsSnapshot);\n\n// Example of a code snippet that would trigger a fixable error with 'semi' rule\nconst codeToFix = 'const c = 3';\nconst { snapshot: fixableSnapshot } = snapshotCreator\n  .mark({ code: codeToFix, ruleName: 'semi', rule: semi })\n  .withOptions([\"always\"], \"error\")\n  .render();\n\nconsole.log('Fixable Snapshot (will show fixed code if rule provides one):\\n', fixableSnapshot);\n\n// In a real test environment (e.g., Jest), you would typically use:\n// expect(simpleSnapshot).toMatchSnapshot();","lang":"typescript","description":"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."},"warnings":[{"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.","message":"The API for handling fix actions was unified into the `render` method, potentially requiring changes if you were using older, separate fix APIs.","severity":"breaking","affected_versions":">=3.0.0"},{"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.","message":"The 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.","severity":"breaking","affected_versions":">=1.1.1"},{"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`.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the rule object is correctly imported and passed. For built-in ESLint rules, the path might be `eslint/lib/rules/<rule-name>`.","cause":"The `rule` property passed to `mark()` was either `undefined` or not a valid ESLint rule object.","error":"TypeError: Cannot read properties of undefined (reading 'meta')"},{"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.","cause":"The `parser` option was not specified in `eslintOptions`, or the specified parser could not be resolved, especially for TypeScript or modern JavaScript syntax.","error":"Error: \"parser\" is a required option for `Linter.verifyAndFix` when parsing code in a non-default language or using an experimental feature."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}