Skip to content

Commit 4ec2a68

Browse files
committed
test: add custom rule tester for testing library
1 parent 0fec322 commit 4ec2a68

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

lib/detect-testing-library-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function detectTestingLibraryUtils<
7676
},
7777

7878
/**
79-
* Gets if file name being analyzed is valid or not.
79+
* Gets if name of the file being analyzed is valid or not.
8080
*
8181
* This is based on "testing-library/file-name" setting.
8282
*/

tests/create-testing-library-rule.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const ruleTester = createRuleTester();
66
ruleTester.run(RULE_NAME, rule, {
77
valid: [
88
{
9-
filename: 'MyComponent.test.js',
109
code: `
1110
// case: nothing related to Testing Library at all
1211
import { shallow } from 'enzyme';
@@ -15,7 +14,6 @@ ruleTester.run(RULE_NAME, rule, {
1514
`,
1615
},
1716
{
18-
filename: 'MyComponent.test.js',
1917
code: `
2018
// case: render imported from other than custom module
2119
import { render } from '@somewhere/else'
@@ -27,7 +25,6 @@ ruleTester.run(RULE_NAME, rule, {
2725
},
2826
},
2927
{
30-
filename: 'MyComponent.test.js',
3128
code: `
3229
// case: prevent import which should trigger an error since it's imported
3330
// from other than custom module
@@ -38,7 +35,6 @@ ruleTester.run(RULE_NAME, rule, {
3835
},
3936
},
4037
{
41-
filename: 'MyComponent.test.js',
4238
code: `
4339
// case: import module forced to be reported but not matching file name
4440
import { foo } from 'report-me'
@@ -50,7 +46,6 @@ ruleTester.run(RULE_NAME, rule, {
5046
],
5147
invalid: [
5248
{
53-
filename: 'MyComponent.test.js',
5449
code: `
5550
// case: import module forced to be reported
5651
import { foo } from 'report-me'
@@ -77,7 +72,6 @@ ruleTester.run(RULE_NAME, rule, {
7772
errors: [{ line: 3, column: 7, messageId: 'fakeError' }],
7873
},
7974
{
80-
filename: 'MyComponent.test.js',
8175
code: `
8276
// case: render imported from any module by default (aggressive reporting)
8377
import { render } from '@somewhere/else'
@@ -94,7 +88,6 @@ ruleTester.run(RULE_NAME, rule, {
9488
],
9589
},
9690
{
97-
filename: 'MyComponent.test.js',
9891
code: `
9992
// case: render imported from Testing Library module
10093
import { render } from '@testing-library/react'
@@ -111,7 +104,6 @@ ruleTester.run(RULE_NAME, rule, {
111104
],
112105
},
113106
{
114-
filename: 'MyComponent.test.js',
115107
code: `
116108
// case: render imported from config custom module
117109
import { render } from 'test-utils'
@@ -131,7 +123,6 @@ ruleTester.run(RULE_NAME, rule, {
131123
],
132124
},
133125
{
134-
filename: 'MyComponent.test.js',
135126
code: `
136127
// case: render imported from Testing Library module if
137128
// custom module setup

tests/lib/test-utils.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
11
import { resolve } from 'path';
22
import { TSESLint } from '@typescript-eslint/experimental-utils';
33

4+
// TODO: remove ecmaFeatures object from other tests files
5+
6+
const DEFAULT_TEST_CASE_CONFIG = {
7+
filename: 'MyComponent.test.js',
8+
};
9+
10+
class TestingLibraryRuleTester extends TSESLint.RuleTester {
11+
run<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(
12+
ruleName: string,
13+
rule: TSESLint.RuleModule<TMessageIds, TOptions>,
14+
tests: TSESLint.RunTests<TMessageIds, TOptions>
15+
): void {
16+
const { valid, invalid } = tests;
17+
18+
const finalValid = valid.map((testCase) => {
19+
if (typeof testCase === 'string') {
20+
return {
21+
...DEFAULT_TEST_CASE_CONFIG,
22+
code: testCase,
23+
};
24+
}
25+
26+
return { ...DEFAULT_TEST_CASE_CONFIG, ...testCase };
27+
});
28+
const finalInvalid = invalid.map((testCase) => ({
29+
...DEFAULT_TEST_CASE_CONFIG,
30+
...testCase,
31+
}));
32+
33+
super.run(ruleName, rule, { valid: finalValid, invalid: finalInvalid });
34+
}
35+
}
36+
437
export const createRuleTester = (
538
parserOptions: Partial<TSESLint.ParserOptions> = {}
6-
): TSESLint.RuleTester =>
7-
new TSESLint.RuleTester({
39+
): TSESLint.RuleTester => {
40+
return new TestingLibraryRuleTester({
841
parser: resolve('./node_modules/@typescript-eslint/parser'),
942
parserOptions: {
1043
ecmaVersion: 2018,
1144
sourceType: 'module',
12-
// TODO: we should deep merge here
45+
// TODO: should we deep merge here?
1346
ecmaFeatures: {
1447
jsx: true,
1548
},
1649
...parserOptions,
1750
},
1851
});
52+
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "es6",
44
"module": "commonjs",
55
"moduleResolution": "node",
66
"esModuleInterop": true,

0 commit comments

Comments
 (0)