Skip to content

Commit

Permalink
Update setFromArgv and Argv types
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed May 2, 2017
1 parent d4c2ed6 commit 6d9d797
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 33 deletions.
6 changes: 1 addition & 5 deletions packages/jest-cli/src/lib/updateArgv.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ type Options = {|
|};
const getTestPathPattern = require('./getTestPathPattern');

module.exports = (
argv: Argv,
mode: 'watch' | 'watchAll',
options: Options,
) => {
module.exports = (argv: Argv, mode: 'watch' | 'watchAll', options: Options) => {
if (mode === 'watch') {
argv.watch = true;
argv.watchAll = false;
Expand Down
68 changes: 68 additions & 0 deletions packages/jest-config/src/__tests__/setFromArgv-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
'use strict';

const setFromArgv = require('../setFromArgv');

test('maps special values to valid options', () => {
const options = {};
const argv = {
coverage: true,
env: 'node',
json: true,
watchAll: true,
};

expect(setFromArgv(options, argv)).toMatchObject({
collectCoverage: true,
testEnvironment: 'node',
useStderr: true,
watch: true,
});
});

test('maps regular values to themselves', () => {
const options = {};
const argv = {
collectCoverageOnlyFrom: ['a', 'b'],
coverageDirectory: 'covDir',
watchman: true,
};

expect(setFromArgv(options, argv)).toMatchObject({
collectCoverageOnlyFrom: ['a', 'b'],
coverageDirectory: 'covDir',
watchman: true,
});
});

test('works with string objects', () => {
const options = {};
const argv = {
moduleNameMapper: '{"types/(.*)": "<rootDir>/src/types/$1"}',
transform: '{"*.js": "<rootDir>/transformer"}',
};
expect(setFromArgv(options, argv)).toMatchObject({
moduleNameMapper: {
'types/(.*)': '<rootDir>/src/types/$1',
},
transform: {
'*.js': '<rootDir>/transformer',
},
});
});

test('--config overrides all', () => {
const options = {};
const argv = {
config: '{"watch": true}',
watch: false,
};
expect(setFromArgv(options, argv)).toMatchObject({watch: true});
});
27 changes: 19 additions & 8 deletions packages/jest-config/src/setFromArgv.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ import type {InitialOptions} from 'types/Config';
import type {Argv} from 'types/Argv';

const specialArgs = ['_', '$0', 'h', 'help', 'config'];
const isJSON = (text: ?string) =>
text &&
typeof text === 'string' &&
text.startsWith('{') &&
text.endsWith('}');

function setFromArgv(config: InitialOptions, argv: Argv) {
function setFromArgv(options: InitialOptions, argv: Argv) {
let configFromArgv;
const argvToConfig = Object.keys(argv)
const argvToOptions = Object.keys(argv)
.filter(key => argv[key] !== undefined && specialArgs.indexOf(key) === -1)
.reduce((options: Object, key) => {
switch (key) {
Expand All @@ -35,20 +40,26 @@ function setFromArgv(config: InitialOptions, argv: Argv) {
break;
case 'config':
break;
case 'coverageThreshold':
case 'globals':
case 'moduleNameMapper':
case 'transform':
case 'haste':
if (isJSON(argv[key])) {
options[key] = JSON.parse(argv[key]);
}
break;
default:
options[key] = argv[key];
}
return options;
}, {});

if (argv.config && typeof argv.config === 'string') {
// If the passed in value looks like JSON, treat it as an object.
if (argv.config.startsWith('{') && argv.config.endsWith('}')) {
configFromArgv = JSON.parse(argv.config);
}
if (isJSON(argv.config)) {
configFromArgv = JSON.parse(argv.config);
}

return Object.assign({}, config, argvToConfig, configFromArgv);
return Object.assign({}, options, argvToOptions, configFromArgv);
}

module.exports = setFromArgv;
2 changes: 1 addition & 1 deletion packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"dependencies": {
"chalk": "^1.1.3",
"graceful-fs": "^4.1.11",
"jest-message-util": "^19.0.0",
"jest-mock": "^19.0.0",
"jest-validate": "^19.0.2",
"jest-message-util": "^19.0.0",
"leven": "^2.1.0",
"mkdirp": "^0.5.1"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-util/src/validateCLIOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

import type {Argv} from 'types/Argv';
const chalk = require('chalk');
const {
ValidationError,
Expand Down Expand Up @@ -48,7 +49,7 @@ const createCLIValidationError = (
return new ValidationError(title, message, comment);
};

const validateCLIOptions = (argv: Object, options: Object) => {
const validateCLIOptions = (argv: Argv, options: Object) => {
const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
const allowedOptions = Object.keys(options).reduce(
(acc, option) => acc.add(option).add(options[option].alias || option),
Expand Down
34 changes: 16 additions & 18 deletions types/Argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,38 @@
*/
'use strict';

import type {Path, Glob, HasteConfig, ConfigGlobals} from 'types/Config';

export type Argv = {
_: Array<string>,
$0: string,
automock: boolean,
bail: boolean,
browser: boolean,
cache: boolean,
cacheDirectory: Path,
cacheDirectory: string,
clearMocks: boolean,
collectCoverage: boolean,
collectCoverageFrom: Array<Glob>,
collectCoverageFrom: Array<string>,
collectCoverageOnlyFrom: Array<string>,
config: string,
coverage: boolean,
coverageDirectory: string,
coveragePathIgnorePatterns: Array<string>,
coverageReporters: Array<string>,
coverageThreshold: {global: {[key: string]: number}},
coverageThreshold: string,
env: string,
expand: boolean,
forceExit: boolean,
globals: ConfigGlobals,
globals: string,
h: boolean,
haste: HasteConfig,
haste: string,
help: boolean,
json: boolean,
logHeapUsage: boolean,
mapCoverage: boolean,
moduleDirectories: Array<string>,
moduleFileExtensions: Array<string>,
moduleLoader: Path,
moduleNameMapper: {[key: string]: string} | Array<[string, string]>,
moduleLoader: string,
moduleNameMapper: string,
modulePathIgnorePatterns: Array<string>,
modulePaths: Array<string>,
name: string,
Expand All @@ -54,15 +52,15 @@ export type Argv = {
replname: ?string,
resetMocks: boolean,
resetModules: boolean,
resolver: ?Path,
rootDir: Path,
roots: Array<Path>,
setupFiles: Array<Path>,
setupTestFrameworkScriptFile: Path,
resolver: ?string,
rootDir: string,
roots: Array<string>,
setupFiles: Array<string>,
setupTestFrameworkScriptFile: string,
silent: boolean,
snapshotSerializers: Array<Path>,
snapshotSerializers: Array<string>,
testEnvironment: string,
testMatch: Array<Glob>,
testMatch: Array<string>,
testNamePattern: string,
testPathIgnorePatterns: Array<string>,
testPathPattern: string,
Expand All @@ -71,8 +69,8 @@ export type Argv = {
testRunner: string,
testURL: string,
timers: 'real' | 'fake',
transform: Array<[string, Path]>,
transformIgnorePatterns: Array<Glob>,
transform: string,
transformIgnorePatterns: Array<string>,
unmockedModulePathPatterns: ?Array<string>,
updateSnapshot: boolean,
useStderr: boolean,
Expand Down

0 comments on commit 6d9d797

Please sign in to comment.