From 13167dd848067754587dcae4aa42d064cc2e3a7a Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Thu, 16 Jun 2016 10:39:26 -0700 Subject: [PATCH] More Flow annotations. (#1171) --- packages/jest-cli/src/SearchSource.js | 46 ++++++++++--------- packages/jest-cli/src/Test.js | 14 +++++- packages/jest-cli/src/TestRunner.js | 25 ++++------ packages/jest-cli/src/cli/args.js | 10 ++-- packages/jest-cli/src/cli/getJest.js | 12 +++-- packages/jest-cli/src/cli/index.js | 2 + packages/jest-cli/src/constants.js | 2 + packages/jest-cli/src/jest.js | 8 +++- .../jest-cli/src/lib/formatTestResults.js | 10 ++-- packages/jest-haste-map/src/constants.js | 4 +- packages/jest-haste-map/src/crawlers/node.js | 10 ++-- .../jest-haste-map/src/crawlers/watchman.js | 10 ++-- packages/jest-haste-map/src/fastpath.js | 4 +- packages/jest-haste-map/src/index.js | 9 ++-- packages/jest-haste-map/src/lib/docblock.js | 8 ++-- .../jest-haste-map/src/lib/extractRequires.js | 7 ++- .../src/lib/getPlatformExtension.js | 7 ++- packages/jest-haste-map/src/types.js | 17 ++++--- packages/jest-haste-map/src/worker.js | 4 +- types/Config.js | 2 +- types/TestResult.js | 2 +- 21 files changed, 112 insertions(+), 101 deletions(-) diff --git a/packages/jest-cli/src/SearchSource.js b/packages/jest-cli/src/SearchSource.js index 472a2c3b01f6..a588ef2935a9 100644 --- a/packages/jest-cli/src/SearchSource.js +++ b/packages/jest-cli/src/SearchSource.js @@ -27,19 +27,20 @@ type SearchSourceConfig = { testPathIgnorePatterns: Array, }; -type DataInfo = { +type SearchResult = { paths: Array, - 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; @@ -95,7 +96,7 @@ class SearchSource { _filterTestPathsWithStats( allPaths: Array, testPathPattern?: StrOrRegExpPattern, - ): DataInfo { + ): SearchResult { const data = { paths: [], stats: {}, @@ -125,7 +126,7 @@ class SearchSource { _getAllTestPaths( testPathPattern: StrOrRegExpPattern - ): Promise { + ): Promise { return this._hasteMap.then(data => ( this._filterTestPathsWithStats( Object.keys(data.moduleMap.files), @@ -142,7 +143,7 @@ class SearchSource { findMatchingTests( testPathPattern: StrOrRegExpPattern - ): Promise { + ): Promise { if (testPathPattern && !(testPathPattern instanceof RegExp)) { const maybeFile = path.resolve(process.cwd(), testPathPattern); if (Resolver.fileExists(maybeFile)) { @@ -155,7 +156,7 @@ class SearchSource { return this._getAllTestPaths(testPathPattern); } - findRelatedTests(allPaths: Set): Promise<{paths: Array}> { + findRelatedTests(allPaths: Set): Promise { return this._hasteMap .then(data => ({ paths: data.resolver.resolveInverseDependencies( @@ -168,7 +169,7 @@ class SearchSource { })); } - findOnlyChangedTestPaths(): Promise<{paths: Array}> { + findOnlyChangedTestPaths(): Promise { return Promise.all(this._config.testPathDirs.map(determineSCM)) .then(repos => { if (!repos.every(result => result[0] || result[1])) { @@ -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; @@ -229,11 +229,13 @@ class SearchSource { ); } - getTestPaths(patternInfo: PatternInfo): Promise<{paths: Array}> { + getTestPaths(patternInfo: PatternInfo): Promise { if (patternInfo.onlyChanged) { return this.findOnlyChangedTestPaths(); - } else { + } else if (patternInfo.testPathPattern != null) { return this.findMatchingTests(patternInfo.testPathPattern); + } else { + return Promise.resolve({paths: []}); } } diff --git a/packages/jest-cli/src/Test.js b/packages/jest-cli/src/Test.js index 2e7962f038d4..75f4093cc3c1 100644 --- a/packages/jest-cli/src/Test.js +++ b/packages/jest-cli/src/Test.js @@ -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; @@ -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); diff --git a/packages/jest-cli/src/TestRunner.js b/packages/jest-cli/src/TestRunner.js index fa5808c5ef93..1467aa17935e 100644 --- a/packages/jest-cli/src/TestRunner.js +++ b/packages/jest-cli/src/TestRunner.js @@ -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'); @@ -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, @@ -28,13 +28,6 @@ type AggregatedResults = { testResults: Array, }; -type Config = { - cacheDirectory: string, - name: string, - testReporter: string, - updateSnapshot: boolean, -}; - type Options = { maxWorkers: number, }; @@ -74,13 +67,13 @@ const TEST_WORKER_PATH = require.resolve('./TestWorker'); class TestRunner { - _hasteMap: Promise; + _hasteMap: Promise; _config: Config; _options: Options; _testPerformanceCache: Object | null; constructor( - hasteMap: Promise, + hasteMap: Promise, config: Config, options: Options ) { @@ -242,9 +235,9 @@ class TestRunner { } _createTestRun( - testPaths: Array, + testPaths: Array, 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); @@ -254,7 +247,7 @@ class TestRunner { } _createInBandTestRun( - testPaths: Array, + testPaths: Array, onTestResult: OnTestResult, onRunFailure: OnRunFailure ) { @@ -269,7 +262,7 @@ class TestRunner { } _createParallelTestRun( - testPaths: Array, + testPaths: Array, onTestResult: OnTestResult, onRunFailure: OnRunFailure, ) { diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 62055c5e0144..ddf083c06b50 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -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]; @@ -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 ' + @@ -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 diff --git a/packages/jest-cli/src/cli/getJest.js b/packages/jest-cli/src/cli/getJest.js index 5d8db6d199e9..9573d618a286 100644 --- a/packages/jest-cli/src/cli/getJest.js +++ b/packages/jest-cli/src/cli/getJest.js @@ -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; diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index c66dfa58b54c..e746e50ae6a4 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -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'; diff --git a/packages/jest-cli/src/constants.js b/packages/jest-cli/src/constants.js index 78235f71261e..aace6fd3ecae 100644 --- a/packages/jest-cli/src/constants.js +++ b/packages/jest-cli/src/constants.js @@ -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'; diff --git a/packages/jest-cli/src/jest.js b/packages/jest-cli/src/jest.js index 2c051f05f649..e1fd625f3d28 100644 --- a/packages/jest-cli/src/jest.js +++ b/packages/jest-cli/src/jest.js @@ -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'); @@ -97,6 +101,7 @@ function runJest(config, argv, pipe, onComplete) { ) .then(runResults => { if (config.testResultsProcessor) { + /* $FlowFixMe */ const processor = require(config.testResultsProcessor); processor(runResults); } @@ -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 || {}; @@ -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) { diff --git a/packages/jest-cli/src/lib/formatTestResults.js b/packages/jest-cli/src/lib/formatTestResults.js index f11a6da798e2..3b3b6492aac9 100644 --- a/packages/jest-cli/src/lib/formatTestResults.js +++ b/packages/jest-cli/src/lib/formatTestResults.js @@ -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 )); diff --git a/packages/jest-haste-map/src/constants.js b/packages/jest-haste-map/src/constants.js index ad39ea90776c..acc7874d4cc4 100644 --- a/packages/jest-haste-map/src/constants.js +++ b/packages/jest-haste-map/src/constants.js @@ -1,11 +1,11 @@ /** * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. * - * @flow - * * 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 */ /* diff --git a/packages/jest-haste-map/src/crawlers/node.js b/packages/jest-haste-map/src/crawlers/node.js index ec42db0c3d4b..b59814bc3eb6 100644 --- a/packages/jest-haste-map/src/crawlers/node.js +++ b/packages/jest-haste-map/src/crawlers/node.js @@ -1,14 +1,12 @@ - /** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * @flow +/** + * 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. + * + * @flow */ - 'use strict'; import type {HasteMap} from 'types/HasteMap'; diff --git a/packages/jest-haste-map/src/crawlers/watchman.js b/packages/jest-haste-map/src/crawlers/watchman.js index a2269b2513c9..c38e44934f61 100644 --- a/packages/jest-haste-map/src/crawlers/watchman.js +++ b/packages/jest-haste-map/src/crawlers/watchman.js @@ -1,14 +1,12 @@ - /** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * @flow +/** + * 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. + * + * @flow */ - 'use strict'; import type {HasteMap} from 'types/HasteMap'; diff --git a/packages/jest-haste-map/src/fastpath.js b/packages/jest-haste-map/src/fastpath.js index f312c1f08231..de33fa62fdc4 100644 --- a/packages/jest-haste-map/src/fastpath.js +++ b/packages/jest-haste-map/src/fastpath.js @@ -1,13 +1,11 @@ /** * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. * - * @flow - * * 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. * - * @emails oncall+jsinfra + * @flow */ /*! diff --git a/packages/jest-haste-map/src/index.js b/packages/jest-haste-map/src/index.js index 524efb106ab8..f30148a8f3a7 100644 --- a/packages/jest-haste-map/src/index.js +++ b/packages/jest-haste-map/src/index.js @@ -1,12 +1,11 @@ - /** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * @flow +/** + * 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. + * + * @flow */ 'use strict'; diff --git a/packages/jest-haste-map/src/lib/docblock.js b/packages/jest-haste-map/src/lib/docblock.js index 26489614cb0f..58af6864a594 100644 --- a/packages/jest-haste-map/src/lib/docblock.js +++ b/packages/jest-haste-map/src/lib/docblock.js @@ -1,14 +1,12 @@ /** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * @flow + * 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. + * + * @flow */ - 'use strict'; const commentEndRe = /\*\/$/; diff --git a/packages/jest-haste-map/src/lib/extractRequires.js b/packages/jest-haste-map/src/lib/extractRequires.js index 04cf21d2dbc9..db91af3bde39 100644 --- a/packages/jest-haste-map/src/lib/extractRequires.js +++ b/packages/jest-haste-map/src/lib/extractRequires.js @@ -1,12 +1,11 @@ /** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * @flow + * 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. + * + * @flow */ 'use strict'; diff --git a/packages/jest-haste-map/src/lib/getPlatformExtension.js b/packages/jest-haste-map/src/lib/getPlatformExtension.js index 851a494c17d8..fa211d075c64 100644 --- a/packages/jest-haste-map/src/lib/getPlatformExtension.js +++ b/packages/jest-haste-map/src/lib/getPlatformExtension.js @@ -1,12 +1,11 @@ /** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * @flow + * 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. + * + * @flow */ 'use strict'; diff --git a/packages/jest-haste-map/src/types.js b/packages/jest-haste-map/src/types.js index 74ce71720a08..42e1c6458771 100644 --- a/packages/jest-haste-map/src/types.js +++ b/packages/jest-haste-map/src/types.js @@ -1,13 +1,12 @@ /** -* Copyright (c) 2015-present, Facebook, Inc. -* All rights reserved. -* -* @flow -* -* 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. -*/ + * 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. + * + * @flow + */ 'use strict'; export type IgnoreMatcher = (item: string) => boolean; diff --git a/packages/jest-haste-map/src/worker.js b/packages/jest-haste-map/src/worker.js index 0c3240d23388..09cd92bc9037 100644 --- a/packages/jest-haste-map/src/worker.js +++ b/packages/jest-haste-map/src/worker.js @@ -1,11 +1,11 @@ /** * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. * - * @flow - * * 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'; diff --git a/types/Config.js b/types/Config.js index bf55f6598936..754a76f8329c 100644 --- a/types/Config.js +++ b/types/Config.js @@ -70,6 +70,6 @@ export type Config = BaseConfig & { testcheckOptions: {}, testFileExtensions: Array, testDirectoryName: string, - updateSnapshot: {}, + updateSnapshot: boolean, watchman: boolean, }; diff --git a/types/TestResult.js b/types/TestResult.js index 8ae68e24b5fc..951f893060a3 100644 --- a/types/TestResult.js +++ b/types/TestResult.js @@ -104,5 +104,5 @@ export type CodeCoverageReporter = any; export type CodeCoverageFormatter = ( coverage: ?CodeCoverageResult, - reporter: CodeCoverageReporter, + reporter?: CodeCoverageReporter, ) => ?Object;