From 21cd16ff8c70f94c19c07370b90a824abe8adab7 Mon Sep 17 00:00:00 2001 From: wtgtybhertgeghgtwtg Date: Fri, 10 Mar 2017 02:48:11 -0700 Subject: [PATCH 1/4] [jest-config] Hoist `normalize` call. --- .../src/__tests__/loadFromFile-test.js | 9 ++---- .../src/__tests__/loadFromPackage-test.js | 9 ++---- packages/jest-config/src/index.js | 28 +++++++++---------- packages/jest-config/src/loadFromFile.js | 5 ++-- packages/jest-config/src/loadFromPackage.js | 3 +- 5 files changed, 20 insertions(+), 34 deletions(-) diff --git a/packages/jest-config/src/__tests__/loadFromFile-test.js b/packages/jest-config/src/__tests__/loadFromFile-test.js index 7996d3f141f3..23de439c52be 100644 --- a/packages/jest-config/src/__tests__/loadFromFile-test.js +++ b/packages/jest-config/src/__tests__/loadFromFile-test.js @@ -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']); }); @@ -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'); - }); }); diff --git a/packages/jest-config/src/__tests__/loadFromPackage-test.js b/packages/jest-config/src/__tests__/loadFromPackage-test.js index 582c30641325..62ac6a0a5aff 100644 --- a/packages/jest-config/src/__tests__/loadFromPackage-test.js +++ b/packages/jest-config/src/__tests__/loadFromPackage-test.js @@ -39,12 +39,12 @@ 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()); }); @@ -52,9 +52,4 @@ describe('loadFromPackage', () => { 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'); - }); }); diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index c59950bb1921..0c8a6b415ca4 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -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') { @@ -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}) => { + .then(config => { if (config) { - return { - config, - hasDeprecationWarnings, - }; + return config; } - - return normalize({rootDir: root}, argv); + return {rootDir: root}; }); }; diff --git a/packages/jest-config/src/loadFromFile.js b/packages/jest-config/src/loadFromFile.js index dec12125749c..0582a8c7e083 100644 --- a/packages/jest-config/src/loadFromFile.js +++ b/packages/jest-config/src/loadFromFile.js @@ -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 { @@ -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; }); } diff --git a/packages/jest-config/src/loadFromPackage.js b/packages/jest-config/src/loadFromPackage.js index d1922cd7f3a0..a875c011e19b 100644 --- a/packages/jest-config/src/loadFromPackage.js +++ b/packages/jest-config/src/loadFromPackage.js @@ -12,7 +12,6 @@ import type {Path} from 'types/Config'; -const normalize = require('./normalize'); const path = require('path'); const readPkg = require('read-pkg'); @@ -23,7 +22,7 @@ function loadFromPackage(root: Path, argv: Object) { config.rootDir = config.rootDir ? path.resolve(root, config.rootDir) : root; - return normalize(config, argv); + return config; }, () => null, ); From defb5bbef5c9c29227fbe1317630d0a989eea404 Mon Sep 17 00:00:00 2001 From: wtgtybhertgeghgtwtg Date: Fri, 10 Mar 2017 02:54:52 -0700 Subject: [PATCH 2/4] Remove unused argument. --- packages/jest-config/src/index.js | 2 +- packages/jest-config/src/loadFromPackage.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 0c8a6b415ca4..38bc8cd77cd8 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -49,7 +49,7 @@ const readRawConfig = (argv, root) => { return Promise.resolve(config); } - return loadFromPackage(root, argv) + return loadFromPackage(root) .then(config => { if (config) { return config; diff --git a/packages/jest-config/src/loadFromPackage.js b/packages/jest-config/src/loadFromPackage.js index a875c011e19b..7260ff3acb05 100644 --- a/packages/jest-config/src/loadFromPackage.js +++ b/packages/jest-config/src/loadFromPackage.js @@ -15,7 +15,7 @@ import type {Path} from 'types/Config'; 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 || {}; From bf85c4075bcee2c05b602af17dc18ef1bfc5f309 Mon Sep 17 00:00:00 2001 From: wtgtybhertgeghgtwtg Date: Fri, 10 Mar 2017 03:02:10 -0700 Subject: [PATCH 3/4] Simplify return. --- packages/jest-config/src/index.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 38bc8cd77cd8..c200c7c6e106 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -49,13 +49,7 @@ const readRawConfig = (argv, root) => { return Promise.resolve(config); } - return loadFromPackage(root) - .then(config => { - if (config) { - return config; - } - return {rootDir: root}; - }); + return loadFromPackage(root).then(config => config || {rootDir: root}); }; module.exports = { From 5f896da9a75bbd9def2cae7f8dba1510b3262845 Mon Sep 17 00:00:00 2001 From: wtgtybhertgeghgtwtg Date: Fri, 10 Mar 2017 03:30:38 -0700 Subject: [PATCH 4/4] Pass `argv` to `normalize`. --- packages/jest-config/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index c200c7c6e106..ac5b03c6c031 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -19,7 +19,7 @@ const {getTestEnvironment} = require('./utils'); async function readConfig(argv: Object, packageRoot: string) { const rawConfig = await readRawConfig(argv, packageRoot); - const {config, hasDeprecationWarnings} = normalize(rawConfig); + const {config, hasDeprecationWarnings} = normalize(rawConfig, argv); return { config: Object.freeze(setFromArgv(config, argv)), hasDeprecationWarnings,