Skip to content

Commit

Permalink
refactor: centralize config update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tdd committed Jun 22, 2018
1 parent e09f018 commit a5f5c4b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,9 @@ exports[`Watch mode flows Pressing "P" enters pattern mode 8`] = `

exports[`Watch mode flows Pressing "P" enters pattern mode 9`] = `
Object {
"bail": false,
"collectCoverage": false,
"notify": false,
"onlyChanged": false,
"onlyFailures": false,
"passWithNoTests": true,
"testNamePattern": "",
"testPathPattern": "p.*3",
"verbose": false,
"watch": true,
"watchAll": false,
}
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,6 @@ describe('Watch mode flows', () => {

expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
testNamePattern: 'test',
testPathPattern: '',
watch: true,
watchAll: false,
});
Expand All @@ -659,7 +658,6 @@ describe('Watch mode flows', () => {
await nextTick();

expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
testNamePattern: '',
testPathPattern: 'file',
watch: true,
watchAll: false,
Expand Down
63 changes: 33 additions & 30 deletions packages/jest-cli/src/lib/update_global_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @flow
*/

import {replacePathSepForRegex} from 'jest-regex-util';

import type {
GlobalConfig,
ReporterConfig,
Expand Down Expand Up @@ -41,10 +43,6 @@ export default (globalConfig: GlobalConfig, options: Options): GlobalConfig => {
options = {};
}

if (options.updateSnapshot) {
newConfig.updateSnapshot = options.updateSnapshot;
}

if (options.mode === 'watch') {
newConfig.watch = true;
newConfig.watchAll = false;
Expand All @@ -53,12 +51,13 @@ export default (globalConfig: GlobalConfig, options: Options): GlobalConfig => {
newConfig.watchAll = true;
}

if ('testPathPattern' in options) {
newConfig.testPathPattern = options.testPathPattern || '';
if (options.testNamePattern !== undefined) {
newConfig.testNamePattern = options.testNamePattern || '';
}

if ('testNamePattern' in options) {
newConfig.testNamePattern = options.testNamePattern || '';
if (options.testPathPattern !== undefined) {
newConfig.testPathPattern =
replacePathSepForRegex(options.testPathPattern) || '';
}

newConfig.onlyChanged = false;
Expand All @@ -67,55 +66,59 @@ export default (globalConfig: GlobalConfig, options: Options): GlobalConfig => {
!newConfig.testNamePattern &&
!newConfig.testPathPattern;

if (options.noSCM) {
newConfig.noSCM = true;
}

if (options.passWithNoTests) {
newConfig.passWithNoTests = true;
}

if ('onlyFailures' in options) {
newConfig.onlyFailures = options.onlyFailures || false;
}

if ('bail' in options) {
if (options.bail !== undefined) {
newConfig.bail = options.bail || false;
}

if ('collectCoverage' in options) {
if (options.collectCoverage !== undefined) {
newConfig.collectCoverage = options.collectCoverage || false;
}

if (options.collectCoverageFrom) {
if (options.collectCoverageFrom !== undefined) {
newConfig.collectCoverageFrom = options.collectCoverageFrom;
}

if (options.collectCoverageOnlyFrom) {
if (options.collectCoverageOnlyFrom !== undefined) {
newConfig.collectCoverageOnlyFrom = options.collectCoverageOnlyFrom;
}

if (options.coverageDirectory) {
if (options.coverageDirectory !== undefined) {
newConfig.coverageDirectory = options.coverageDirectory;
}

if (options.coverageReporters) {
if (options.coverageReporters !== undefined) {
newConfig.coverageReporters = options.coverageReporters;
}

if ('notify' in options) {
if (options.noSCM) {
newConfig.noSCM = true;
}

if (options.notify !== undefined) {
newConfig.notify = options.notify || false;
}

if (options.notifyMode) {
if (options.notifyMode !== undefined) {
newConfig.notifyMode = options.notifyMode;
}

if (options.reporters) {
if (options.onlyFailures !== undefined) {
newConfig.onlyFailures = options.onlyFailures || false;
}

if (options.passWithNoTests !== undefined) {
newConfig.passWithNoTests = true;
}

if (options.reporters !== undefined) {
newConfig.reporters = options.reporters;
}

if ('verbose' in options) {
if (options.updateSnapshot !== undefined) {
newConfig.updateSnapshot = options.updateSnapshot;
}

if (options.verbose !== undefined) {
newConfig.verbose = options.verbose || false;
}

Expand Down
58 changes: 14 additions & 44 deletions packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import ansiEscapes from 'ansi-escapes';
import chalk from 'chalk';
import getChangedFilesPromise from './get_changed_files_promise';
import exit from 'exit';
import {replacePathSepForRegex} from 'jest-regex-util';
import HasteMap from 'jest-haste-map';
import isValidPath from './lib/is_valid_path';
import {isInteractive} from 'jest-util';
Expand Down Expand Up @@ -105,51 +104,22 @@ export default function watch(
verbose?: boolean,
} = {}) => {
const previousUpdateSnapshot = globalConfig.updateSnapshot;
// It is unfortunate that Flow isn't smart enough yet to let us
// implement a generic assign loop over a list of white-listed
// option names here :-(
globalConfig = updateGlobalConfig(globalConfig, {
bail: bail !== undefined ? bail : globalConfig.bail,
collectCoverage:
collectCoverage !== undefined
? collectCoverage
: globalConfig.collectCoverage,
collectCoverageFrom:
collectCoverageFrom !== undefined
? collectCoverageFrom
: globalConfig.collectCoverageFrom,
collectCoverageOnlyFrom:
collectCoverageOnlyFrom !== undefined
? collectCoverageOnlyFrom
: globalConfig.collectCoverageOnlyFrom,
coverageDirectory:
coverageDirectory !== undefined
? coverageDirectory
: globalConfig.coverageDirectory,
coverageReporters:
coverageReporters !== undefined
? coverageReporters
: globalConfig.coverageReporters,
bail,
collectCoverage,
collectCoverageFrom,
collectCoverageOnlyFrom,
coverageDirectory,
coverageReporters,
mode,
notify: notify !== undefined ? notify : globalConfig.notify,
notifyMode:
notifyMode !== undefined ? notifyMode : globalConfig.notifyMode,
onlyFailures:
onlyFailures !== undefined ? onlyFailures : globalConfig.onlyFailures,
reporters: reporters !== undefined ? reporters : globalConfig.reporters,
testNamePattern:
testNamePattern !== undefined
? testNamePattern
: globalConfig.testNamePattern,
testPathPattern:
testPathPattern !== undefined
? replacePathSepForRegex(testPathPattern)
: globalConfig.testPathPattern,
updateSnapshot:
updateSnapshot !== undefined
? updateSnapshot
: globalConfig.updateSnapshot,
verbose: verbose !== undefined ? verbose : globalConfig.verbose,
notify,
notifyMode,
onlyFailures,
reporters,
testNamePattern,
testPathPattern,
updateSnapshot,
verbose,
});
startRun(globalConfig);
Expand Down

0 comments on commit a5f5c4b

Please sign in to comment.