Skip to content

Commit 740bf7c

Browse files
committed
[Tests] test plugin in flat configs
1 parent 31ef12a commit 740bf7c

File tree

13 files changed

+201
-1
lines changed

13 files changed

+201
-1
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ignorePatterns": [
1717
"coverage/",
1818
".nyc_output/",
19+
"tests/fixtures/flat-config/"
1920
],
2021
"rules": {
2122
"comma-dangle": [2, "always-multiline"],

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test": "npm run unit-test",
1616
"posttest": "aud --production",
1717
"type-check": "tsc",
18-
"unit-test": "istanbul cover node_modules/mocha/bin/_mocha tests/lib/**/*.js tests/util/**/*.js tests/index.js",
18+
"unit-test": "istanbul cover node_modules/mocha/bin/_mocha tests/lib/**/*.js tests/util/**/*.js tests/index.js tests/flat-config.js",
1919
"update:eslint-docs": "eslint-doc-generator"
2020
},
2121
"repository": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const reactAll = require('../../../../configs/all.js');
4+
5+
module.exports = [{
6+
files: ['**/*.jsx'],
7+
...reactAll,
8+
languageOptions: {
9+
...reactAll.languageOptions
10+
}
11+
}];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div foo="hello">
2+
test
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const reactRecommended = require('../../../../configs/recommended.js');
4+
const reactJSXRuntime = require('../../../../configs/jsx-runtime.js');
5+
6+
module.exports = [
7+
{
8+
files: ['**/*.jsx'],
9+
...reactRecommended,
10+
languageOptions: {
11+
...reactRecommended.languageOptions
12+
}
13+
},
14+
reactJSXRuntime
15+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div foo="hello">
2+
test
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const reactRecommended = require('../../../../configs/recommended.js');
4+
5+
module.exports = [{
6+
files: ['**/*.jsx'],
7+
...reactRecommended,
8+
languageOptions: {
9+
...reactRecommended.languageOptions
10+
}
11+
}];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div foo="hello">
2+
test
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const react = require('../../../..');
4+
const reactRecommended = require('../../../../configs/recommended.js');
5+
6+
module.exports = [
7+
{
8+
files: ['**/*.jsx'],
9+
plugins: { react }
10+
},
11+
{
12+
files: ['**/*.jsx'],
13+
...reactRecommended,
14+
languageOptions: {
15+
...reactRecommended.languageOptions
16+
}
17+
}
18+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div foo="hello">
2+
test
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const react = require('../../../..');
4+
5+
module.exports = [{
6+
files: ['**/*.jsx'],
7+
languageOptions: {
8+
parserOptions: {
9+
ecmaFeatures: {
10+
jsx: true,
11+
},
12+
},
13+
},
14+
plugins: {
15+
react,
16+
},
17+
rules: {
18+
'react/jsx-no-literals': 1,
19+
},
20+
}];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div foo="hello">
2+
test
3+
</div>

tests/flat-config.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* eslint-env mocha */
2+
3+
'use strict';
4+
5+
const semver = require('semver');
6+
const eslintPkg = require('eslint/package.json');
7+
8+
if (!semver.satisfies(eslintPkg.version, '>= 8.57.0')) {
9+
return;
10+
}
11+
12+
const ESLint = semver.major(eslintPkg.version) < 9
13+
? require('eslint/use-at-your-own-risk').FlatESLint // eslint-disable-line import/no-unresolved -- false positive
14+
: require('eslint').ESLint;
15+
16+
const path = require('path');
17+
const assert = require('assert');
18+
19+
describe('eslint-plugin-react in flat config', () => {
20+
const fixturesdDir = path.resolve(__dirname, 'fixtures', 'flat-config');
21+
22+
it('should work when the plugin is used directly', () => {
23+
const eslint = new ESLint({
24+
cwd: path.resolve(fixturesdDir, 'plugin'),
25+
});
26+
27+
return eslint.lintFiles(['test.jsx']).then((results) => {
28+
const result = results[0];
29+
30+
assert.strictEqual(result.messages.length, 1);
31+
assert.strictEqual(result.messages[0].severity, 1);
32+
assert.strictEqual(result.messages[0].ruleId, 'react/jsx-no-literals');
33+
assert.strictEqual(result.messages[0].messageId, 'literalNotInJSXExpression');
34+
});
35+
});
36+
37+
it('should work when the plugin is used with "all" config', () => {
38+
const eslint = new ESLint({
39+
cwd: path.resolve(fixturesdDir, 'config-all'),
40+
});
41+
42+
return eslint.lintFiles(['test.jsx']).then((results) => {
43+
const result = results[0];
44+
45+
assert.strictEqual(result.messages.length, 3);
46+
assert.strictEqual(result.messages[0].severity, 2);
47+
assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope');
48+
assert.strictEqual(result.messages[0].messageId, 'notInScope');
49+
assert.strictEqual(result.messages[1].severity, 2);
50+
assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property');
51+
assert.strictEqual(result.messages[1].messageId, 'unknownProp');
52+
assert.strictEqual(result.messages[2].severity, 2);
53+
assert.strictEqual(result.messages[2].ruleId, 'react/jsx-no-literals');
54+
assert.strictEqual(result.messages[2].messageId, 'literalNotInJSXExpression');
55+
});
56+
});
57+
58+
it('should work when the plugin is used with "recommended" config', () => {
59+
const eslint = new ESLint({
60+
cwd: path.resolve(fixturesdDir, 'config-recommended'),
61+
});
62+
63+
return eslint.lintFiles(['test.jsx']).then((results) => {
64+
const result = results[0];
65+
66+
assert.strictEqual(result.messages.length, 2);
67+
assert.strictEqual(result.messages[0].severity, 2);
68+
assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope');
69+
assert.strictEqual(result.messages[0].messageId, 'notInScope');
70+
assert.strictEqual(result.messages[1].severity, 2);
71+
assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property');
72+
assert.strictEqual(result.messages[1].messageId, 'unknownProp');
73+
});
74+
});
75+
76+
it('should work when the plugin is used with "recommended" and "jsx-runtime" configs', () => {
77+
const eslint = new ESLint({
78+
cwd: path.resolve(fixturesdDir, 'config-jsx-runtime'),
79+
});
80+
81+
return eslint.lintFiles(['test.jsx']).then((results) => {
82+
const result = results[0];
83+
84+
assert.strictEqual(result.messages.length, 1);
85+
assert.strictEqual(result.messages[0].severity, 2);
86+
assert.strictEqual(result.messages[0].ruleId, 'react/no-unknown-property');
87+
assert.strictEqual(result.messages[0].messageId, 'unknownProp');
88+
});
89+
});
90+
91+
// https://github.com/jsx-eslint/eslint-plugin-react/issues/3693
92+
it('should work when the plugin is used directly and with "recommended" config', () => {
93+
const eslint = new ESLint({
94+
cwd: path.resolve(fixturesdDir, 'plugin-and-config'),
95+
});
96+
97+
return eslint.lintFiles(['test.jsx']).then((results) => {
98+
const result = results[0];
99+
100+
assert.strictEqual(result.messages.length, 2);
101+
assert.strictEqual(result.messages[0].severity, 2);
102+
assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope');
103+
assert.strictEqual(result.messages[0].messageId, 'notInScope');
104+
assert.strictEqual(result.messages[1].severity, 2);
105+
assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property');
106+
assert.strictEqual(result.messages[1].messageId, 'unknownProp');
107+
});
108+
});
109+
});

0 commit comments

Comments
 (0)