Skip to content

Commit e891f90

Browse files
committed
✅ add some more tests
1 parent f2b2576 commit e891f90

File tree

8 files changed

+94
-47
lines changed

8 files changed

+94
-47
lines changed

eslint-config-node/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@dudeofawesome/eslint-config": "^1.0.0"
2727
},
2828
"devDependencies": {
29-
"@swc/core": "^1.3.87",
3029
"@types/eslint": "^8.44.2",
3130
"@types/node": "^20.6.3",
3231
"ts-node": "^10.9.1"

eslint-config-node/test/index.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { describe, it } from 'node:test';
2-
import { strictEqual } from 'node:assert';
32
import { ESLint } from 'eslint';
3+
import { testRuleFail, noLintMessage } from '../../utils/testing/eslint';
44

5-
const linter = new ESLint({
6-
overrideConfigFile: `${__dirname}/.eslintrc.yaml`,
7-
});
5+
const linter = new ESLint({ cwd: __dirname });
86

97
describe('eslint-config-node', () => {
108
describe('passes', () => {
@@ -19,34 +17,3 @@ describe('eslint-config-node', () => {
1917
testRuleFail(linter, `if (Number == true) Number();\n`, 'eqeqeq'));
2018
});
2119
});
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-
}

eslint-config/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
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",
@@ -27,5 +27,10 @@
2727
},
2828
"peerDependencies": {
2929
"prettier": "^3.0.3"
30+
},
31+
"devDependencies": {
32+
"@types/eslint": "^8.44.2",
33+
"@types/node": "^20.6.3",
34+
"ts-node": "^10.9.1"
3035
}
3136
}

eslint-config/test/.eslintrc.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root: true
2+
extends:
3+
- '@dudeofawesome'
4+
parserOptions:
5+
ecmaVersion: 2022

eslint-config/test/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it } from 'node:test';
2+
import { ESLint } from 'eslint';
3+
import { testRuleFail } from '../../utils/testing/eslint';
4+
5+
const linter = new ESLint({ cwd: __dirname });
6+
7+
describe('eslint-config-node', () => {
8+
describe('passes', () => {});
9+
describe('fails', () => {
10+
it(`should fail eqeqeq`, () =>
11+
testRuleFail(linter, `if (Number == true) Number();\n`, 'eqeqeq'));
12+
13+
it(`should fail radix`, async () =>
14+
testRuleFail(linter, `parseInt('10');\n`, 'radix'));
15+
});
16+
});

package-lock.json

Lines changed: 25 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"test": "npm run test --ws"
3535
},
3636
"devDependencies": {
37-
"@dudeofawesome/code-style": "^1.0.0"
37+
"@dudeofawesome/code-style": "^1.0.0",
38+
"@types/eslint": "^8.44.2",
39+
"@types/node": "^20.6.3"
3840
}
3941
}

utils/testing/eslint.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { strictEqual } from 'node:assert';
2+
import { ESLint } from 'eslint';
3+
4+
export async function testRuleFail(
5+
linter: ESLint,
6+
code: string,
7+
ruleId: string,
8+
) {
9+
const res = await linter.lintText(code);
10+
singleLintMessage(res);
11+
strictEqual(res[0].source, code);
12+
strictEqual(res[0].messages[0].ruleId, ruleId);
13+
}
14+
15+
export function noLintMessage(lint_results: ESLint.LintResult[]) {
16+
strictEqual(lint_results.length, 1, `Expected there to be no lint results.`);
17+
strictEqual(
18+
lint_results[0].errorCount,
19+
0,
20+
`Expected there to be no lint results.`,
21+
);
22+
}
23+
24+
export function singleLintMessage(lint_results: ESLint.LintResult[]) {
25+
strictEqual(
26+
lint_results.length,
27+
1,
28+
`Expected there to be only one lint result.`,
29+
);
30+
strictEqual(
31+
lint_results[0].messages.length,
32+
1,
33+
`Expected there to be one lint message, but there were ${
34+
lint_results[0].messages.length
35+
}:\n${lint_results[0].messages.map((m) => ` "${m.message}"`).join('\n')}`,
36+
);
37+
}

0 commit comments

Comments
 (0)