|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import { strictEqual } from 'node:assert'; |
| 3 | +import { ESLint } from 'eslint'; |
| 4 | + |
| 5 | +const linter = new ESLint({ |
| 6 | + overrideConfigFile: `${__dirname}/.eslintrc.yaml`, |
| 7 | +}); |
| 8 | + |
| 9 | +describe('eslint-config-node', () => { |
| 10 | + describe('passes', () => { |
| 11 | + it(`should pass radix`, async () => |
| 12 | + linter.lintText(`parseInt('10');\n`).then(noLintMessage)); |
| 13 | + }); |
| 14 | + describe('fails', () => { |
| 15 | + it(`should fail no-console`, () => |
| 16 | + testRuleFail(linter, `console.log('foo');\n`, 'no-console')); |
| 17 | + |
| 18 | + it(`should fail eqeqeq`, () => |
| 19 | + testRuleFail(linter, `if (Number == true) Number();\n`, 'eqeqeq')); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +async function testRuleFail(linter: ESLint, code: string, ruleId: string) { |
| 24 | + const res = await linter.lintText(code); |
| 25 | + singleLintMessage(res); |
| 26 | + strictEqual(res[0].source, code); |
| 27 | + strictEqual(res[0].messages[0].ruleId, ruleId); |
| 28 | +} |
| 29 | + |
| 30 | +function noLintMessage(lint_results: ESLint.LintResult[]) { |
| 31 | + strictEqual(lint_results.length, 1, `Expected there to be no lint results.`); |
| 32 | + strictEqual( |
| 33 | + lint_results[0].errorCount, |
| 34 | + 0, |
| 35 | + `Expected there to be no lint results.`, |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +function singleLintMessage(lint_results: ESLint.LintResult[]) { |
| 40 | + strictEqual( |
| 41 | + lint_results.length, |
| 42 | + 1, |
| 43 | + `Expected there to be only one lint result.`, |
| 44 | + ); |
| 45 | + strictEqual( |
| 46 | + lint_results[0].messages.length, |
| 47 | + 1, |
| 48 | + `Expected there to be one lint message, but there were ${ |
| 49 | + lint_results[0].messages.length |
| 50 | + }: ${lint_results[0].messages.map((m) => m.message)}`, |
| 51 | + ); |
| 52 | +} |
0 commit comments