Skip to content

Commit

Permalink
Add tests for react version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev committed Oct 14, 2018
1 parent 64ffe23 commit c09f3b6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -14,7 +14,7 @@ pids
logs
reports
build
node_modules
/node_modules
npm-debug.log
sftp-config.json

Expand Down
35 changes: 19 additions & 16 deletions lib/util/version.js
Expand Up @@ -9,27 +9,30 @@ const log = require('./log');

let warnedForMissingVersion = false;

function detectReactVersion() {
try {
const reactPath = resolve.sync('react', {basedir: process.cwd()});
const react = require(reactPath);
return react.version;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
'but the "react" package is not installed. Assuming latest React version for linting.');
return '999.999.999';
}
throw e;
}
}

function getReactVersionFromContext(context) {
let confVer = '999.999.999';
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
if (context.settings.react && context.settings.react.version) {
let settingsVer = context.settings.react.version;
if (settingsVer === 'detect') {
try {
const reactPath = resolve.sync('react', {basedir: process.cwd()});
const react = require(reactPath);
settingsVer = react.version;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
'but the "react" package is not installed. Assuming latest React version for linting.');
settingsVer = '999.999.999';
} else {
throw e;
}
}
let settingsVersion = context.settings.react.version;
if (settingsVersion === 'detect') {
settingsVersion = detectReactVersion();
}
confVer = settingsVer;
confVer = settingsVersion;
} else if (!warnedForMissingVersion) {
log('Warning: React version not specified in eslint-plugin-react settings. ' +
'See https://github.com/yannickcr/eslint-plugin-react#configuration.');
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions tests/util/version.js
@@ -0,0 +1,34 @@
/* eslint-env mocha */
'use strict';

const path = require('path');
const assert = require('assert');
const versionUtil = require('../../lib/util/version');

describe('Version', () => {
const base = path.resolve(__dirname, '..', 'fixtures', 'version');
let cwd;

beforeEach(() => {
cwd = process.cwd();
process.chdir(base);
});

afterEach(() => {
process.chdir(cwd);
});

describe('Detect version', () => {
const context = {settings: {react: {version: 'detect'}}};

it('matches detected version', () => {
process.chdir('detect-version');
assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true);
assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false);
});

it('assumes latest version if react is not installed', () => {
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
});
});
});

0 comments on commit c09f3b6

Please sign in to comment.