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

[jest-config] Hoist normalize call. #3117

Merged
merged 4 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
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: 14 additions & 16 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);
return {
config: Object.freeze(setFromArgv(config, argv)),
hasDeprecationWarnings,
};
}

const parseConfig = argv => {
if (argv.config && typeof argv.config === 'string') {
Expand All @@ -38,25 +40,21 @@ 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}) => {
return loadFromPackage(root)
.then(config => {
if (config) {
return {
config,
hasDeprecationWarnings,
};
return config;
}

return normalize({rootDir: root}, argv);
return {rootDir: root};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return loadFromPackage(root).then(config => config || {rootDir: root})

maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done.

});
};

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