Skip to content

Commit

Permalink
More Flow annotations. (#1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer authored Jun 16, 2016
1 parent 8eb78b0 commit 13167dd
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 101 deletions.
46 changes: 24 additions & 22 deletions packages/jest-cli/src/SearchSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ type SearchSourceConfig = {
testPathIgnorePatterns: Array<RegExp>,
};

type DataInfo = {
type SearchResult = {
paths: Array<Path>,
stats: {[key: string]: number},
total: number,
stats?: {[key: string]: number},
total?: number,
};

type StrOrRegExpPattern = RegExp | string;

type PatternInfo = {
onlyChanged: boolean,
testPathPattern: string,
input: string,
shouldTreatInputAsPattern: string,
onlyChanged?: boolean,
watch?: boolean,
testPathPattern?: string,
input?: string,
shouldTreatInputAsPattern?: boolean,
};

const git = changedFiles.git;
Expand Down Expand Up @@ -95,7 +96,7 @@ class SearchSource {
_filterTestPathsWithStats(
allPaths: Array<Path>,
testPathPattern?: StrOrRegExpPattern,
): DataInfo {
): SearchResult {
const data = {
paths: [],
stats: {},
Expand Down Expand Up @@ -125,7 +126,7 @@ class SearchSource {

_getAllTestPaths(
testPathPattern: StrOrRegExpPattern
): Promise<DataInfo> {
): Promise<SearchResult> {
return this._hasteMap.then(data => (
this._filterTestPathsWithStats(
Object.keys(data.moduleMap.files),
Expand All @@ -142,7 +143,7 @@ class SearchSource {

findMatchingTests(
testPathPattern: StrOrRegExpPattern
): Promise<DataInfo> {
): Promise<SearchResult> {
if (testPathPattern && !(testPathPattern instanceof RegExp)) {
const maybeFile = path.resolve(process.cwd(), testPathPattern);
if (Resolver.fileExists(maybeFile)) {
Expand All @@ -155,7 +156,7 @@ class SearchSource {
return this._getAllTestPaths(testPathPattern);
}

findRelatedTests(allPaths: Set<Path>): Promise<{paths: Array<Path>}> {
findRelatedTests(allPaths: Set<Path>): Promise<SearchResult> {
return this._hasteMap
.then(data => ({
paths: data.resolver.resolveInverseDependencies(
Expand All @@ -168,7 +169,7 @@ class SearchSource {
}));
}

findOnlyChangedTestPaths(): Promise<{paths: Array<Path>}> {
findOnlyChangedTestPaths(): Promise<SearchResult> {
return Promise.all(this._config.testPathDirs.map(determineSCM))
.then(repos => {
if (!repos.every(result => result[0] || result[1])) {
Expand All @@ -192,32 +193,31 @@ class SearchSource {
getNoTestsFoundMessage(
patternInfo: PatternInfo,
config: {[key: string]: string},
data: DataInfo,
data: SearchResult,
): string {
if (patternInfo.onlyChanged) {
const guide = patternInfo.watch
? 'starting Jest with `jest --watch=all`'
: 'running Jest without `-o`';
return 'No tests found related to changed and uncommitted files.\n' +
'Note: If you are using dynamic `require`-calls or no tests related ' +
'to your changed files can be found, consider ' + guide + '.';
'Note: If you are using dynamic `require`-calls or no tests related ' +
'to your changed files can be found, consider ' + guide + '.';
}

const pattern = patternInfo.testPathPattern;
const input = patternInfo.input;
const shouldTreatInputAsPattern = patternInfo.shouldTreatInputAsPattern;

const formattedPattern = `/${pattern}/`;
const formattedInput = shouldTreatInputAsPattern
const formattedInput = patternInfo.shouldTreatInputAsPattern
? `/${input}/`
: `"${input}"`;
const testPathPattern =
(input === pattern) ? formattedInput : formattedPattern;

const statsMessage = Object.keys(data.stats).map(key => {
const stats = data.stats || {};
const statsMessage = Object.keys(stats).map(key => {
const value = key === 'testPathPattern' ? testPathPattern : config[key];
if (value) {
const matches = pluralize('match', data.stats[key], 'es');
const matches = pluralize('match', stats[key], 'es');
return ` ${key}: ${chalk.yellow(value)} - ${matches}`;
}
return null;
Expand All @@ -229,11 +229,13 @@ class SearchSource {
);
}

getTestPaths(patternInfo: PatternInfo): Promise<{paths: Array<Path>}> {
getTestPaths(patternInfo: PatternInfo): Promise<SearchResult> {
if (patternInfo.onlyChanged) {
return this.findOnlyChangedTestPaths();
} else {
} else if (patternInfo.testPathPattern != null) {
return this.findMatchingTests(patternInfo.testPathPattern);
} else {
return Promise.resolve({paths: []});
}
}

Expand Down
14 changes: 13 additions & 1 deletion packages/jest-cli/src/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
* 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.
*
* @flow
*/
'use strict';

import type {Path, Config} from 'types/Config';
import type Resolver from '../../jest-resolve/src';

const Console = require('./Console');
const NullConsole = require('./NullConsole');

class Test {

constructor(path, config, resolver) {
_path: Path;
_config: Config;
_resolver: Resolver;

constructor(path: Path, config: Config, resolver: Resolver) {
this._path = path;
this._config = config;
this._resolver = resolver;
Expand All @@ -22,8 +31,11 @@ class Test {
const path = this._path;
const config = this._config;
const resolver = this._resolver;
/* $FlowFixMe */
const TestEnvironment = require(config.testEnvironment);
/* $FlowFixMe */
const TestRunner = require(config.testRunner);
/* $FlowFixMe */
const ModuleLoader = require(config.moduleLoader);

const env = new TestEnvironment(config);
Expand Down
25 changes: 9 additions & 16 deletions packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
*
* @flow
*/

'use strict';

import type {Config, Path} from 'types/Config';
import type {HasteResolverContext} from './types';

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

const fs = require('graceful-fs');
Expand All @@ -18,8 +20,6 @@ const promisify = require('./lib/promisify');
const snapshot = require('jest-snapshot');
const workerFarm = require('worker-farm');

import type HasteMap from 'jest-haste-map';

type AggregatedResults = {
didUpdate?: boolean,
snapshotFilesRemoved?: Array<string>,
Expand All @@ -28,13 +28,6 @@ type AggregatedResults = {
testResults: Array<TestResult>,
};

type Config = {
cacheDirectory: string,
name: string,
testReporter: string,
updateSnapshot: boolean,
};

type Options = {
maxWorkers: number,
};
Expand Down Expand Up @@ -74,13 +67,13 @@ const TEST_WORKER_PATH = require.resolve('./TestWorker');

class TestRunner {

_hasteMap: Promise<HasteMap>;
_hasteMap: Promise<HasteResolverContext>;
_config: Config;
_options: Options;
_testPerformanceCache: Object | null;

constructor(
hasteMap: Promise<HasteMap>,
hasteMap: Promise<HasteResolverContext>,
config: Config,
options: Options
) {
Expand Down Expand Up @@ -242,9 +235,9 @@ class TestRunner {
}

_createTestRun(
testPaths: Array<string>,
testPaths: Array<Path>,
onTestResult: OnTestResult,
onRunFailure: (path: string, err: mixed) => void,
onRunFailure: (path: Path, err: mixed) => void,
) {
if (this._options.maxWorkers <= 1 || testPaths.length <= 1) {
return this._createInBandTestRun(testPaths, onTestResult, onRunFailure);
Expand All @@ -254,7 +247,7 @@ class TestRunner {
}

_createInBandTestRun(
testPaths: Array<string>,
testPaths: Array<Path>,
onTestResult: OnTestResult,
onRunFailure: OnRunFailure
) {
Expand All @@ -269,7 +262,7 @@ class TestRunner {
}

_createParallelTestRun(
testPaths: Array<string>,
testPaths: Array<Path>,
onTestResult: OnTestResult,
onRunFailure: OnRunFailure,
) {
Expand Down
10 changes: 6 additions & 4 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
* 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.
*
* @flow
*/

'use strict';

function wrap(desc) {
function wrap(desccription: string) {
const indent = '\n ';
return indent + desc.split(' ').reduce((wrappedDesc, word) => {
return indent + desccription.split(' ').reduce((wrappedDesc, word) => {
const lastLineIdx = wrappedDesc.length - 1;
const lastLine = wrappedDesc[lastLineIdx];

Expand All @@ -26,7 +28,7 @@ function wrap(desc) {
}, ['']).join(indent);
}

const check = argv => {
const check = (argv: Object) => {
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
throw new Error(
'Both --runInBand and --maxWorkers were specified, but these two ' +
Expand Down Expand Up @@ -55,7 +57,7 @@ const check = argv => {
return true;
};

const warnAboutUnrecognizedOptions = (argv, options) => {
const warnAboutUnrecognizedOptions = (argv: Object, options: Object) => {
const yargsSpecialOptions = ['$0', '_'];
const allowedOptions = Object.keys(options).reduce((acc, option) => (
acc
Expand Down
12 changes: 9 additions & 3 deletions packages/jest-cli/src/cli/getJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@
* 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.
*
* @flow
*/

'use strict';

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

const fs = require('graceful-fs');
const path = require('path');

function getJest(cwdPackageRoot) {
const packageJSONPath = path.join(cwdPackageRoot, 'package.json');
const binPath = path.join(cwdPackageRoot, 'node_modules/jest-cli');
function getJest(packageRoot: Path) {
const packageJSONPath = path.join(packageRoot, 'package.json');
const binPath = path.join(packageRoot, 'node_modules/jest-cli');
if (fs.existsSync(binPath)) {
/* $FlowFixMe */
return require(binPath);
} else {
const jest = require('../jest');
// Check if Jest is specified in `package.json` but not installed.
if (fs.existsSync(packageJSONPath)) {
/* $FlowFixMe */
const packageJSON = require(packageJSONPath);
const dependencies = packageJSON.dependencies;
const devDependencies = packageJSON.devDependencies;
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* 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.
*
* @flow
*/

'use strict';
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-cli/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* 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.
*
* @flow
*/

'use strict';
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-cli/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
* 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.
*
* @flow
*/
'use strict';

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

require('jest-haste-map').fastpath.replace();

const realFs = require('fs');
Expand Down Expand Up @@ -97,6 +101,7 @@ function runJest(config, argv, pipe, onComplete) {
)
.then(runResults => {
if (config.testResultsProcessor) {
/* $FlowFixMe */
const processor = require(config.testResultsProcessor);
processor(runResults);
}
Expand All @@ -123,7 +128,7 @@ function runJest(config, argv, pipe, onComplete) {
});
}

function runCLI(argv, root, onComplete) {
function runCLI(argv: Object, root: Path, onComplete: () => void) {
const pipe = argv.json ? process.stderr : process.stdout;

argv = argv || {};
Expand All @@ -140,6 +145,7 @@ function runCLI(argv, root, onComplete) {
chalk.enabled = false;
}

/* $FlowFixMe */
const testFramework = require(config.testRunner);
const info = [`v${constants.VERSION}`, testFramework.name];
if (config.usesBabelJest) {
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-cli/src/lib/formatTestResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ const formatResult = (
function formatTestResults(
results: AggregatedTestResults,
config: Config,
codeCoverageFormatter: CodeCoverageFormatter,
reporter: CodeCoverageReporter,
codeCoverageFormatter?: CodeCoverageFormatter,
reporter?: CodeCoverageReporter,
): Object {
if (!codeCoverageFormatter) {
codeCoverageFormatter = coverage => coverage;
}
const formatter = codeCoverageFormatter || (coverage => coverage);

const testResults = results.testResults.map(testResult => formatResult(
testResult,
config,
codeCoverageFormatter,
formatter,
reporter
));

Expand Down
Loading

0 comments on commit 13167dd

Please sign in to comment.