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 16, 2024
1 parent 4be7f0e commit a32c0ad
Show file tree
Hide file tree
Showing 17 changed files with 251 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-deep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

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

module.exports = [{
files: ['**/*.jsx'],
...reactAll,
languageOptions: {
...reactAll.languageOptions
}
}];
8 changes: 8 additions & 0 deletions tests/fixtures/flat-config/config-all/eslint.config-root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

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

module.exports = [{
files: ['**/*.jsx'],
...reactPlugin.configs['flat/all']
}];
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

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

module.exports = [
{
files: ['**/*.jsx'],
...reactRecommended,
languageOptions: {
...reactRecommended.languageOptions
}
},
reactJSXRuntime
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

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

module.exports = [
{
files: ['**/*.jsx'],
...reactPlugin.configs['flat/recommended']
},
reactPlugin.configs['flat/jsx-runtime']
];
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

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

module.exports = [{
files: ['**/*.jsx'],
...reactRecommended,
languageOptions: {
...reactRecommended.languageOptions
}
}];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

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

module.exports = [{
files: ['**/*.jsx'],
...reactPlugin.configs['flat/recommended']
}];
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-deep.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');

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

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

module.exports = [
{
files: ['**/*.jsx'],
plugins: { react }
},
{
files: ['**/*.jsx'],
...react.configs['flat/recommended']
}
];
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>
117 changes: 117 additions & 0 deletions tests/flat-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* 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');
});
});

['root', 'deep'].forEach((configAccess) => {
const overrideConfigFile = `eslint.config-${configAccess}.js`;

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

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 (${configAccess})`, () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'config-recommended'),
overrideConfigFile,
});

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 (${configAccess})`, () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'config-jsx-runtime'),
overrideConfigFile,
});

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 (${configAccess})`, () => {
const eslint = new ESLint({
cwd: path.resolve(fixturesdDir, 'plugin-and-config'),
overrideConfigFile,
});

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 a32c0ad

Please sign in to comment.