Skip to content

Commit cc7a543

Browse files
committed
✅ add eslint-node tests
1 parent 466f2f9 commit cc7a543

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

eslint-config-node/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
"license": "MIT",
1818
"main": ".eslintrc.yaml",
1919
"scripts": {
20-
"test": "echo 'No tests'; exit 0"
20+
"test": "node --require ts-node/register --test test/index.ts"
2121
},
2222
"dependencies": {
2323
"@shopify/eslint-plugin": "^43.0.0"
2424
},
2525
"peerDependencies": {
2626
"@dudeofawesome/eslint-config": "^1.0.0"
27+
},
28+
"devDependencies": {
29+
"@swc/core": "^1.3.87",
30+
"@types/eslint": "^8.44.2",
31+
"@types/node": "^20.6.3",
32+
"ts-node": "^10.9.1"
2733
}
2834
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
root: true
2+
extends:
3+
- '@dudeofawesome'
4+
- '@dudeofawesome/node'
5+
parserOptions:
6+
ecmaVersion: 2022

eslint-config-node/test/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)