Skip to content

Commit

Permalink
[jest-config] Hoist normalize call. (#3117)
Browse files Browse the repository at this point in the history
* [jest-config] Hoist `normalize` call.

* Remove unused argument.

* Simplify return.

* Pass `argv` to `normalize`.
  • Loading branch information
wtgtybhertgeghgtwtg authored and cpojer committed Mar 10, 2017
1 parent a8c0a47 commit bb33afc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 39 deletions.
9 changes: 2 additions & 7 deletions packages/jest-config/src/__tests__/loadFromFile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('loadFromFile', () => {
});

it('loads configuration from a file at `filePath`.', async () => {
const {config} = await loadFromFile('config.js', {});
const config = await loadFromFile('config.js', {});
expect(config.testMatch).toEqual(['match.js']);
});

Expand All @@ -42,12 +42,7 @@ describe('loadFromFile', () => {
});

it('uses the current working directory if `rootDir` is not defined.', async () => {
const {config} = await loadFromFile('config.js', {});
const config = await loadFromFile('config.js', {});
expect(config.rootDir).toEqual(process.cwd());
});

it('resolves `rootDir` if defined.', async () => {
const {config} = await loadFromFile('configWithRootDir.js', {});
expect(path.basename(config.rootDir)).toEqual('testDir');
});
});
9 changes: 2 additions & 7 deletions packages/jest-config/src/__tests__/loadFromPackage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,17 @@ describe('loadFromPackage', () => {
});

it('loads configuration from a `package.json` at `root`', async () => {
const {config} = await loadFromPackage('.', {});
const config = await loadFromPackage('.', {});
expect(config.testMatch).toEqual(['match.js']);
});

it('returns a config object even if `jest` is not defined in `package.json`.', async () => {
const {config} = await loadFromPackage('withoutJest', {});
const config = await loadFromPackage('withoutJest', {});
expect(config).toEqual(expect.anything());
});

it('returns null if the `package.json` at `root` cannot be parsed.', async () => {
const result = await loadFromPackage('broken', {});
expect(result).toBeNull();
});

it('resolves `rootDir` if defined.', async () => {
const {config} = await loadFromPackage('withRootDir', {});
expect(path.basename(config.rootDir)).toEqual('testDir');
});
});
30 changes: 11 additions & 19 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const normalize = require('./normalize');
const setFromArgv = require('./setFromArgv');
const {getTestEnvironment} = require('./utils');

const readConfig = (argv: Object, packageRoot: string) =>
readRawConfig(argv, packageRoot)
.then(({config, hasDeprecationWarnings}) => ({
config: Object.freeze(setFromArgv(config, argv)),
hasDeprecationWarnings,
}));
async function readConfig(argv: Object, packageRoot: string) {
const rawConfig = await readRawConfig(argv, packageRoot);
const {config, hasDeprecationWarnings} = normalize(rawConfig, argv);
return {
config: Object.freeze(setFromArgv(config, argv)),
hasDeprecationWarnings,
};
}

const parseConfig = argv => {
if (argv.config && typeof argv.config === 'string') {
Expand All @@ -38,26 +40,16 @@ const readRawConfig = (argv, root) => {
const rawConfig = parseConfig(argv);

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

if (typeof rawConfig === 'object') {
const config = Object.assign({}, rawConfig);
config.rootDir = config.rootDir || root;
return Promise.resolve(normalize(config, argv));
return Promise.resolve(config);
}

return loadFromPackage(root, argv)
.then(({config, hasDeprecationWarnings}) => {
if (config) {
return {
config,
hasDeprecationWarnings,
};
}

return normalize({rootDir: root}, argv);
});
return loadFromPackage(root).then(config => config || {rootDir: root});
};

module.exports = {
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-config/src/loadFromFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
import type {Path} from 'types/Config';

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

function loadFromFile(filePath: Path, argv: Object) {
function loadFromFile(filePath: Path) {
return pify(fs.readFile)(filePath).then(data => {
const parse = () => {
try {
Expand All @@ -35,7 +34,7 @@ function loadFromFile(filePath: Path, argv: Object) {
config.rootDir = config.rootDir
? path.resolve(path.dirname(filePath), config.rootDir)
: process.cwd();
return normalize(config, argv);
return config;
});
}

Expand Down
5 changes: 2 additions & 3 deletions packages/jest-config/src/loadFromPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@

import type {Path} from 'types/Config';

const normalize = require('./normalize');
const path = require('path');
const readPkg = require('read-pkg');

function loadFromPackage(root: Path, argv: Object) {
function loadFromPackage(root: Path) {
return readPkg(root).then(
packageData => {
const config = packageData.jest || {};
config.rootDir = config.rootDir
? path.resolve(root, config.rootDir)
: root;
return normalize(config, argv);
return config;
},
() => null,
);
Expand Down

0 comments on commit bb33afc

Please sign in to comment.