|
| 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 | + ['root', 'deep'].forEach((configAccess) => { |
| 38 | + const overrideConfigFile = `eslint.config-${configAccess}.js`; |
| 39 | + |
| 40 | + it(`should work when the plugin is used with "all" config (${configAccess})`, () => { |
| 41 | + const eslint = new ESLint({ |
| 42 | + cwd: path.resolve(fixturesdDir, 'config-all'), |
| 43 | + overrideConfigFile, |
| 44 | + }); |
| 45 | + |
| 46 | + return eslint.lintFiles(['test.jsx']).then((results) => { |
| 47 | + const result = results[0]; |
| 48 | + |
| 49 | + assert.strictEqual(result.messages.length, 3); |
| 50 | + assert.strictEqual(result.messages[0].severity, 2); |
| 51 | + assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope'); |
| 52 | + assert.strictEqual(result.messages[0].messageId, 'notInScope'); |
| 53 | + assert.strictEqual(result.messages[1].severity, 2); |
| 54 | + assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property'); |
| 55 | + assert.strictEqual(result.messages[1].messageId, 'unknownProp'); |
| 56 | + assert.strictEqual(result.messages[2].severity, 2); |
| 57 | + assert.strictEqual(result.messages[2].ruleId, 'react/jsx-no-literals'); |
| 58 | + assert.strictEqual(result.messages[2].messageId, 'literalNotInJSXExpression'); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it(`should work when the plugin is used with "recommended" config (${configAccess})`, () => { |
| 63 | + const eslint = new ESLint({ |
| 64 | + cwd: path.resolve(fixturesdDir, 'config-recommended'), |
| 65 | + overrideConfigFile, |
| 66 | + }); |
| 67 | + |
| 68 | + return eslint.lintFiles(['test.jsx']).then((results) => { |
| 69 | + const result = results[0]; |
| 70 | + |
| 71 | + assert.strictEqual(result.messages.length, 2); |
| 72 | + assert.strictEqual(result.messages[0].severity, 2); |
| 73 | + assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope'); |
| 74 | + assert.strictEqual(result.messages[0].messageId, 'notInScope'); |
| 75 | + assert.strictEqual(result.messages[1].severity, 2); |
| 76 | + assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property'); |
| 77 | + assert.strictEqual(result.messages[1].messageId, 'unknownProp'); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it(`should work when the plugin is used with "recommended" and "jsx-runtime" configs (${configAccess})`, () => { |
| 82 | + const eslint = new ESLint({ |
| 83 | + cwd: path.resolve(fixturesdDir, 'config-jsx-runtime'), |
| 84 | + overrideConfigFile, |
| 85 | + }); |
| 86 | + |
| 87 | + return eslint.lintFiles(['test.jsx']).then((results) => { |
| 88 | + const result = results[0]; |
| 89 | + |
| 90 | + assert.strictEqual(result.messages.length, 1); |
| 91 | + assert.strictEqual(result.messages[0].severity, 2); |
| 92 | + assert.strictEqual(result.messages[0].ruleId, 'react/no-unknown-property'); |
| 93 | + assert.strictEqual(result.messages[0].messageId, 'unknownProp'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + // https://github.com/jsx-eslint/eslint-plugin-react/issues/3693 |
| 98 | + it(`should work when the plugin is used directly and with "recommended" config (${configAccess})`, () => { |
| 99 | + const eslint = new ESLint({ |
| 100 | + cwd: path.resolve(fixturesdDir, 'plugin-and-config'), |
| 101 | + overrideConfigFile, |
| 102 | + }); |
| 103 | + |
| 104 | + return eslint.lintFiles(['test.jsx']).then((results) => { |
| 105 | + const result = results[0]; |
| 106 | + |
| 107 | + assert.strictEqual(result.messages.length, 2); |
| 108 | + assert.strictEqual(result.messages[0].severity, 2); |
| 109 | + assert.strictEqual(result.messages[0].ruleId, 'react/react-in-jsx-scope'); |
| 110 | + assert.strictEqual(result.messages[0].messageId, 'notInScope'); |
| 111 | + assert.strictEqual(result.messages[1].severity, 2); |
| 112 | + assert.strictEqual(result.messages[1].ruleId, 'react/no-unknown-property'); |
| 113 | + assert.strictEqual(result.messages[1].messageId, 'unknownProp'); |
| 114 | + }); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments