What is @typescript-eslint/rule-tester?
@typescript-eslint/rule-tester is a utility package designed to help developers test custom ESLint rules for TypeScript code. It provides a framework for writing unit tests for ESLint rules, ensuring that they behave as expected when applied to TypeScript code.
Testing ESLint Rules
This feature allows you to test custom ESLint rules by providing valid and invalid code samples. The RuleTester runs the rule against these samples and checks if the expected errors are reported.
const { RuleTester } = require('@typescript-eslint/rule-tester');
const rule = require('path/to/your/custom-rule');
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});
ruleTester.run('your-rule-name', rule, {
valid: [
{
code: 'const x: number = 42;',
},
],
invalid: [
{
code: 'const x: string = 42;',
errors: [{ messageId: 'expectedNumber' }],
},
],
});