Skip to content

Commit 25b3662

Browse files
committed
Add tests for react version detection
1 parent 64b9ef5 commit 25b3662

File tree

5 files changed

+64
-17
lines changed

5 files changed

+64
-17
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pids
1414
logs
1515
reports
1616
build
17-
node_modules
17+
/node_modules
1818
npm-debug.log
1919
sftp-config.json
2020

lib/util/version.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,30 @@ const log = require('./log');
99

1010
let warnedForMissingVersion = false;
1111

12+
function detectReactVersion() {
13+
try {
14+
const reactPath = resolve.sync('react', {basedir: process.cwd()});
15+
const react = require(reactPath);
16+
return react.version;
17+
} catch (e) {
18+
if (e.code === 'MODULE_NOT_FOUND') {
19+
log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
20+
'but the "react" package is not installed. Assuming latest React version for linting.');
21+
return '999.999.999';
22+
}
23+
throw e;
24+
}
25+
}
26+
1227
function getReactVersionFromContext(context) {
1328
let confVer = '999.999.999';
1429
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
1530
if (context.settings.react && context.settings.react.version) {
16-
let settingsVer = context.settings.react.version;
17-
if (settingsVer === 'detect') {
18-
try {
19-
const reactPath = resolve.sync('react', {basedir: process.cwd()});
20-
const react = require(reactPath);
21-
settingsVer = react.version;
22-
} catch (e) {
23-
if (e.code === 'MODULE_NOT_FOUND') {
24-
log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
25-
'but the "react" package is not installed. Assuming latest React version for linting.');
26-
settingsVer = '999.999.999';
27-
} else {
28-
throw e;
29-
}
30-
}
31+
let settingsVersion = context.settings.react.version;
32+
if (settingsVersion === 'detect') {
33+
settingsVersion = detectReactVersion();
3134
}
32-
confVer = settingsVer;
35+
confVer = settingsVersion;
3336
} else if (!warnedForMissingVersion) {
3437
log('Warning: React version not specified in eslint-plugin-react settings. ' +
3538
'See https://github.com/yannickcr/eslint-plugin-react#configuration.');

tests/fixtures/version/detect-version/node_modules/react/index.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/version/detect-version/node_modules/react/package.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/util/version.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-env mocha */
2+
'use strict';
3+
4+
const path = require('path');
5+
const assert = require('assert');
6+
const versionUtil = require('../../lib/util/version');
7+
8+
describe('Version', () => {
9+
const base = path.resolve(__dirname, '..', 'fixtures', 'version');
10+
let cwd;
11+
12+
beforeEach(() => {
13+
cwd = process.cwd();
14+
process.chdir(base);
15+
});
16+
17+
afterEach(() => {
18+
process.chdir(cwd);
19+
});
20+
21+
describe('Detect version', () => {
22+
const context = {settings: {react: {version: 'detect'}}};
23+
24+
it('matches detected version', () => {
25+
process.chdir('detect-version');
26+
assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true);
27+
assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false);
28+
});
29+
30+
it('assumes latest version if react is not installed', () => {
31+
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
32+
});
33+
});
34+
});

0 commit comments

Comments
 (0)