Skip to content

Commit

Permalink
[Tests] test plugin in flat configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Jun 15, 2024
1 parent 31ef12a commit 740bf7c
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"ignorePatterns": [
"coverage/",
".nyc_output/",
"tests/fixtures/flat-config/"
],
"rules": {
"comma-dangle": [2, "always-multiline"],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "npm run unit-test",
"posttest": "aud --production",
"type-check": "tsc",
"unit-test": "istanbul cover node_modules/mocha/bin/_mocha tests/lib/**/*.js tests/util/**/*.js tests/index.js",
"unit-test": "istanbul cover node_modules/mocha/bin/_mocha tests/lib/**/*.js tests/util/**/*.js tests/index.js tests/flat-config.js",
"update:eslint-docs": "eslint-doc-generator"
},
"repository": {
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/flat-config/config-all/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const reactAll = require('../../../../configs/all.js');

module.exports = [{
files: ['**/*.jsx'],
...reactAll,
languageOptions: {
...reactAll.languageOptions
}
}];
3 changes: 3 additions & 0 deletions tests/fixtures/flat-config/config-all/test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div foo="hello">
test
</div>
15 changes: 15 additions & 0 deletions tests/fixtures/flat-config/config-jsx-runtime/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const reactRecommended = require('../../../../configs/recommended.js');
const reactJSXRuntime = require('../../../../configs/jsx-runtime.js');

module.exports = [
{
files: ['**/*.jsx'],
...reactRecommended,
languageOptions: {
...reactRecommended.languageOptions
}
},
reactJSXRuntime
];
3 changes: 3 additions & 0 deletions tests/fixtures/flat-config/config-jsx-runtime/test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div foo="hello">
test
</div>
11 changes: 11 additions & 0 deletions tests/fixtures/flat-config/config-recommended/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const reactRecommended = require('../../../../configs/recommended.js');

module.exports = [{
files: ['**/*.jsx'],
...reactRecommended,
languageOptions: {
...reactRecommended.languageOptions
}
}];
3 changes: 3 additions & 0 deletions tests/fixtures/flat-config/config-recommended/test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div foo="hello">
test
</div>
18 changes: 18 additions & 0 deletions tests/fixtures/flat-config/plugin-and-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const react = require('../../../..');
const reactRecommended = require('../../../../configs/recommended.js');

module.exports = [
{
files: ['**/*.jsx'],
plugins: { react }
},
{
files: ['**/*.jsx'],
...reactRecommended,
languageOptions: {
...reactRecommended.languageOptions
}
}
];
3 changes: 3 additions & 0 deletions tests/fixtures/flat-config/plugin-and-config/test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div foo="hello">
test
</div>
20 changes: 20 additions & 0 deletions tests/fixtures/flat-config/plugin/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const react = require('../../../..');

module.exports = [{
files: ['**/*.jsx'],
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
plugins: {
react,
},
rules: {
'react/jsx-no-literals': 1,
},
}];
3 changes: 3 additions & 0 deletions tests/fixtures/flat-config/plugin/test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div foo="hello">
test
</div>
109 changes: 109 additions & 0 deletions tests/flat-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* eslint-env mocha */

'use strict';

const semver = require('semver');
const eslintPkg = require('eslint/package.json');

if (!semver.satisfies(eslintPkg.version, '>= 8.57.0')) {
return;
}

const ESLint = semver.major(eslintPkg.version) < 9
? require('eslint/use-at-your-own-risk').FlatESLint // eslint-disable-line import/no-unresolved -- false positive
: require('eslint').ESLint;

const path = require('path');
const assert = require('assert');

describe('eslint-plugin-react in flat config', () => {
const fixturesdDir = path.resolve(__dirname, 'fixtures', 'flat-config');

it('should work when the plugin is used directly', () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'plugin'),
});

return eslint.lintFiles(['test.jsx']).then((results) => {
const result = results[0];

assert.strictEqual(result.messages.length, 1);
assert.strictEqual(result.messages[0].severity, 1);
assert.strictEqual(result.messages[0].ruleId, 'react/jsx-no-literals');
assert.strictEqual(result.messages[0].messageId, 'literalNotInJSXExpression');
});
});

it('should work when the plugin is used with "all" config', () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'config-all'),
});

return eslint.lintFiles(['test.jsx']).then((results) => {
const result = results[0];

assert.strictEqual(result.messages.length, 3);
assert.strictEqual(result.messages[0].severity, 2);
assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope');
assert.strictEqual(result.messages[0].messageId, 'notInScope');
assert.strictEqual(result.messages[1].severity, 2);
assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property');
assert.strictEqual(result.messages[1].messageId, 'unknownProp');
assert.strictEqual(result.messages[2].severity, 2);
assert.strictEqual(result.messages[2].ruleId, 'react/jsx-no-literals');
assert.strictEqual(result.messages[2].messageId, 'literalNotInJSXExpression');
});
});

it('should work when the plugin is used with "recommended" config', () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'config-recommended'),
});

return eslint.lintFiles(['test.jsx']).then((results) => {
const result = results[0];

assert.strictEqual(result.messages.length, 2);
assert.strictEqual(result.messages[0].severity, 2);
assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope');
assert.strictEqual(result.messages[0].messageId, 'notInScope');
assert.strictEqual(result.messages[1].severity, 2);
assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property');
assert.strictEqual(result.messages[1].messageId, 'unknownProp');
});
});

it('should work when the plugin is used with "recommended" and "jsx-runtime" configs', () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'config-jsx-runtime'),
});

return eslint.lintFiles(['test.jsx']).then((results) => {
const result = results[0];

assert.strictEqual(result.messages.length, 1);
assert.strictEqual(result.messages[0].severity, 2);
assert.strictEqual(result.messages[0].ruleId, 'react/no-unknown-property');
assert.strictEqual(result.messages[0].messageId, 'unknownProp');
});
});

// https://github.com/jsx-eslint/eslint-plugin-react/issues/3693
it('should work when the plugin is used directly and with "recommended" config', () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'plugin-and-config'),
});

return eslint.lintFiles(['test.jsx']).then((results) => {
const result = results[0];

assert.strictEqual(result.messages.length, 2);
assert.strictEqual(result.messages[0].severity, 2);
assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope');
assert.strictEqual(result.messages[0].messageId, 'notInScope');
assert.strictEqual(result.messages[1].severity, 2);
assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property');
assert.strictEqual(result.messages[1].messageId, 'unknownProp');
});
});
});

0 comments on commit 740bf7c

Please sign in to comment.