Skip to content

Commit 759ffc7

Browse files
committed
✅ add eslint tests
1 parent e891f90 commit 759ffc7

File tree

8 files changed

+131
-41
lines changed

8 files changed

+131
-41
lines changed

eslint-config-node/test/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { describe, it } from 'node:test';
22
import { ESLint } from 'eslint';
3-
import { testRuleFail, noLintMessage } from '../../utils/testing/eslint';
3+
import {
4+
testRuleFail,
5+
testNoFail,
6+
defaultTestSet,
7+
} from '../../utils/testing/eslint';
48

59
const linter = new ESLint({ cwd: __dirname });
610

711
describe('eslint-config-node', () => {
12+
defaultTestSet(linter);
13+
814
describe('passes', () => {
915
it(`should pass radix`, async () =>
10-
linter.lintText(`parseInt('10');\n`).then(noLintMessage));
16+
testNoFail(linter, `parseInt('10');\n`));
1117
});
18+
1219
describe('fails', () => {
1320
it(`should fail no-console`, () =>
1421
testRuleFail(linter, `console.log('foo');\n`, 'no-console'));
15-
16-
it(`should fail eqeqeq`, () =>
17-
testRuleFail(linter, `if (Number == true) Number();\n`, 'eqeqeq'));
1822
});
1923
});
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
extends:
22
- 'plugin:@typescript-eslint/recommended'
3-
- 'plugin:@shopify/typescript'
43
- 'plugin:import/typescript'
5-
- 'plugin:prettier/recommended'
6-
plugins:
7-
- '@typescript-eslint'
8-
9-
parser: '@typescript-eslint/parser'
104

115
rules:
12-
'id-length': 'off'
13-
'no-negated-condition': 'off'
14-
'no-nested-ternary': 'off'
15-
'@typescript-eslint/no-explicit-any': 'error'
166
'@typescript-eslint/no-inferrable-types': 'off'
177
'@typescript-eslint/no-unused-vars':
188
['warn', { args: 'none', destructuredArrayIgnorePattern: '^_' }]
19-
'@typescript-eslint/no-use-before-define': 'error'
209
'@typescript-eslint/naming-convention': 'off'
21-
'prettier/prettier': 'warn'
22-
'import/order':
23-
- 'error'
24-
- groups:
25-
- 'builtin'
26-
- 'external'
27-
newlines-between: 'ignore'
28-
'import/no-cycle': 'off'
2910
'@shopify/binary-assignment-parens': 'off'

eslint-config-typescript/package.json

Lines changed: 1 addition & 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
"@typescript-eslint/eslint-plugin": "^5.30.7",
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/typescript'
5+
parserOptions:
6+
ecmaVersion: 2022
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, it } from 'node:test';
2+
import { ESLint } from 'eslint';
3+
import {
4+
defaultTestSet,
5+
testNoFail,
6+
testRuleFail,
7+
} from '../../utils/testing/eslint';
8+
9+
const linter = new ESLint({ cwd: __dirname });
10+
11+
describe('eslint-config-typescript', () => {
12+
defaultTestSet(linter);
13+
14+
describe('passes', () => {
15+
it(`should parse typescript`, () =>
16+
testNoFail(linter, `((a: string): string[] => a.split(''))()\n`, true));
17+
18+
it(`should parse javascript`, () =>
19+
testNoFail(linter, `((a) => a.split(''))()\n`));
20+
21+
it(`should not give eslint error on use before define`, () =>
22+
testNoFail(
23+
linter,
24+
`Number(a);
25+
const a = 10;\n`,
26+
true,
27+
));
28+
});
29+
30+
describe('fails', () => {
31+
it(`should fail radix`, async () =>
32+
testRuleFail(linter, `parseInt('10');\n`, 'radix', true));
33+
34+
// TODO: test for shopify rule
35+
36+
// TODO: test for prettier rule
37+
});
38+
});

eslint-config/.eslintrc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extends:
33
- 'plugin:prettier/recommended'
44
parserOptions:
55
ecmaVersion: 2022 # Version is inline with Node 16
6+
sourceType: 'module'
67

78
rules:
89
'@babel/new-cap': 'off'
@@ -13,6 +14,7 @@ rules:
1314
- 'builtin'
1415
- 'external'
1516
- 'internal'
17+
newlines-between: 'ignore'
1618
import/no-default-export: 'error'
1719
line-comment-position: 'off'
1820
no-empty-function: 'warn'
@@ -32,4 +34,5 @@ rules:
3234
lines-between-class-members: 'off'
3335
no-useless-return: 'off'
3436
no-use-before-define: 'off'
37+
eslint-comments/no-use: 'warn'
3538
'@shopify/binary-assignment-parens': 'off'

eslint-config/test/index.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
import { describe, it } from 'node:test';
22
import { ESLint } from 'eslint';
3-
import { testRuleFail } from '../../utils/testing/eslint';
3+
import {
4+
defaultTestSet,
5+
testNoFail,
6+
testRuleFail,
7+
} from '../../utils/testing/eslint';
8+
import { equal, strictEqual } from 'node:assert';
49

510
const linter = new ESLint({ cwd: __dirname });
611

7-
describe('eslint-config-node', () => {
12+
describe('eslint-config', () => {
13+
defaultTestSet(linter);
14+
815
describe('passes', () => {});
9-
describe('fails', () => {
10-
it(`should fail eqeqeq`, () =>
11-
testRuleFail(linter, `if (Number == true) Number();\n`, 'eqeqeq'));
1216

17+
describe('fails', () => {
1318
it(`should fail radix`, async () =>
1419
testRuleFail(linter, `parseInt('10');\n`, 'radix'));
20+
21+
it(`should not parse typescript`, () =>
22+
linter
23+
.lintText(`((a: string): string[] => a.split(''))()\n`, {
24+
filePath: 'input.ts',
25+
})
26+
.then((res) => {
27+
equal(res[0].messages[0].ruleId, null);
28+
strictEqual(
29+
res[0].messages[0].message,
30+
'Parsing error: Unexpected token, expected "," (1:3)',
31+
);
32+
}));
33+
34+
// TODO: test for shopify rule
35+
36+
// TODO: test for prettier rule
1537
});
1638
});

utils/testing/eslint.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1+
import { describe, it } from 'node:test';
12
import { strictEqual } from 'node:assert';
23
import { ESLint } from 'eslint';
34

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);
5+
export function defaultTestSet(linter: ESLint) {
6+
describe('[standard tests] passes', () => {
7+
it(`should allow nested ternaries`, () =>
8+
testNoFail(
9+
linter,
10+
`(() => (Number === true ? 1 : Boolean === true ? 2 : 3))();\n`,
11+
true,
12+
));
13+
});
14+
describe('[standard tests] fails', () => {
15+
it(`should fail eqeqeq`, () =>
16+
testRuleFail(linter, `if (Number == true) Number();\n`, 'eqeqeq'));
17+
18+
it(`should warn on prettier`, () =>
19+
testRuleFail(linter, `Number( '5')`, 'prettier/prettier'));
20+
21+
// TODO: test for shopify rule
22+
});
1323
}
1424

1525
export function noLintMessage(lint_results: ESLint.LintResult[]) {
16-
strictEqual(lint_results.length, 1, `Expected there to be no lint results.`);
1726
strictEqual(
1827
lint_results[0].errorCount,
1928
0,
20-
`Expected there to be no lint results.`,
29+
`Expected there to be no lint errors, but found:\n${lint_results[0].messages
30+
.map((m) => ` "${m.ruleId}": ${m.message}`)
31+
.join('\n')}`,
2132
);
2233
}
2334

@@ -35,3 +46,28 @@ export function singleLintMessage(lint_results: ESLint.LintResult[]) {
3546
}:\n${lint_results[0].messages.map((m) => ` "${m.message}"`).join('\n')}`,
3647
);
3748
}
49+
50+
export async function testRuleFail(
51+
linter: ESLint,
52+
code: string,
53+
ruleId: string,
54+
typescript: boolean = false,
55+
) {
56+
const res = await linter.lintText(code, {
57+
filePath: `index.${typescript ? 'ts' : 'js'}`,
58+
});
59+
singleLintMessage(res);
60+
strictEqual(res[0].source, code);
61+
strictEqual(res[0].messages[0].ruleId, ruleId);
62+
}
63+
64+
export async function testNoFail(
65+
linter: ESLint,
66+
code: string,
67+
typescript: boolean = false,
68+
) {
69+
const res = await linter.lintText(code, {
70+
filePath: `index.${typescript ? 'ts' : 'js'}`,
71+
});
72+
noLintMessage(res);
73+
}

0 commit comments

Comments
 (0)