Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and consolidate config resolution. #3472

Merged
merged 1 commit into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions packages/jest-config/src/__tests__/loadFromFile-test.js

This file was deleted.

38 changes: 35 additions & 3 deletions packages/jest-config/src/findConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,53 @@
import type {InitialOptions, Path} from 'types/Config';

const fs = require('fs');
const jsonlint = require('./vendor/jsonlint');
const path = require('path');

const JEST_CONFIG = 'jest.config.js';
const PACAKAGE_JSON = 'package.json';
const PACKAGE_JSON = 'package.json';

const findConfig = (root: Path): InitialOptions => {
let directory = root;
// $FlowFixMe
let options: InitialOptions = {};
let directory = root;
const isJS = directory.endsWith('.js');
if (isJS || directory.endsWith('.json')) {
const filePath = path.resolve(process.cwd(), directory);
if (isJS) {
// $FlowFixMe
options = require(filePath);
} else {
let pkg;
try {
// $FlowFixMe
pkg = require(filePath);
} catch (error) {
throw new Error(
`Jest: Failed to parse config file ${filePath}\n` +
` ${jsonlint.errors(fs.readFileSync(filePath, 'utf8'))}`,
);
}
if (directory.endsWith(PACKAGE_JSON)) {
options = pkg.jest || options;
} else {
options = pkg;
}
}
options.rootDir = options.rootDir
? path.resolve(path.dirname(directory), options.rootDir)
: path.dirname(directory);
return options;
}

do {
const configJsFilePath = path.join(directory, JEST_CONFIG);
if (fs.existsSync(configJsFilePath)) {
// $FlowFixMe
options = require(configJsFilePath);
break;
}
const packageJsonFilePath = path.join(directory, PACAKAGE_JSON);
const packageJsonFilePath = path.join(directory, PACKAGE_JSON);
if (fs.existsSync(packageJsonFilePath)) {
// $FlowFixMe
const pkg = require(packageJsonFilePath);
Expand All @@ -41,9 +71,11 @@ const findConfig = (root: Path): InitialOptions => {
break;
}
} while (directory !== (directory = path.dirname(directory)));

options.rootDir = options.rootDir
? path.resolve(root, options.rootDir)
: root;

return options;
};

Expand Down
9 changes: 4 additions & 5 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type {GlobalConfig, ProjectConfig} from 'types/Config';

const {getTestEnvironment, isJSONString} = require('./utils');
const findConfig = require('./findConfig');
const loadFromFile = require('./loadFromFile');
const normalize = require('./normalize');
const path = require('path');

Expand Down Expand Up @@ -43,16 +42,16 @@ const parseConfig = argv =>
const readOptions = (argv, root) => {
const rawOptions = parseConfig(argv);

if (typeof rawOptions === 'string') {
return loadFromFile(path.resolve(process.cwd(), rawOptions));
}

if (typeof rawOptions === 'object') {
const config = Object.assign({}, rawOptions);
config.rootDir = config.rootDir || root;
return config;
}

if (typeof rawOptions === 'string') {
root = path.resolve(process.cwd(), rawOptions);
}

return findConfig(root);
};

Expand Down
44 changes: 0 additions & 44 deletions packages/jest-config/src/loadFromFile.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function normalize(options: InitialOptions, argv: Argv) {
value.hasteImplModulePath = resolve(
options.rootDir,
'haste.hasteImplModulePath',
value.hasteImplModulePath,
_replaceRootDirInPath(options.rootDir, value.hasteImplModulePath),
);
}
break;
Expand Down
11 changes: 10 additions & 1 deletion scripts/test_examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ examples.forEach(exampleDirectory => {
link(exampleDirectory, BABEL_JEST_PATH);
});

runCommand(JEST_BIN_PATH, ['--projects'].concat(examples), EXAMPLES_DIR);
runCommand(
JEST_BIN_PATH,
['--projects'].concat(
examples.map(
(example, index) =>
example + (index > 3 ? path.sep + 'package.json' : '')
)
),
EXAMPLES_DIR
);