From 74e167005711fd0f850feac3d2bf6d6d10ecc0c0 Mon Sep 17 00:00:00 2001 From: cpojer Date: Sat, 18 Mar 2017 08:44:18 +0900 Subject: [PATCH] =?UTF-8?q?Pass=20=E2=80=9CTest=E2=80=9Ds=20to=20reporters?= =?UTF-8?q?;=20update=20Test=20to=20contain=20contexts.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/jest-cli/src/TestRunner.js | 33 ++++++++-------- packages/jest-cli/src/TestSequencer.js | 20 +++++----- .../jest-cli/src/__tests__/TestRunner-test.js | 13 +++---- .../src/__tests__/TestSequencer-test.js | 38 ++++++++++--------- .../jest-cli/src/reporters/BaseReporter.js | 11 ++---- .../src/reporters/CoverageReporter.js | 3 +- .../jest-cli/src/reporters/DefaultReporter.js | 19 +++++++--- .../jest-cli/src/reporters/VerboseReporter.js | 6 +-- packages/jest-cli/src/runJest.js | 10 +++-- types/TestRunner.js | 5 ++- 10 files changed, 85 insertions(+), 73 deletions(-) diff --git a/packages/jest-cli/src/TestRunner.js b/packages/jest-cli/src/TestRunner.js index 2ce201365029..d1a0242cb499 100644 --- a/packages/jest-cli/src/TestRunner.js +++ b/packages/jest-cli/src/TestRunner.js @@ -121,7 +121,7 @@ class TestRunner { return; } addResult(aggregatedResults, testResult); - this._dispatcher.onTestResult(config, testResult, aggregatedResults); + this._dispatcher.onTestResult(test, testResult, aggregatedResults); this._bailIfNeeded(aggregatedResults, watcher); }; @@ -132,11 +132,11 @@ class TestRunner { const testResult = buildFailureTestResult(test.path, error); testResult.failureMessage = formatExecError( testResult, - test.config, + test.context.config, test.path, ); addResult(aggregatedResults, testResult); - this._dispatcher.onTestResult(config, testResult, aggregatedResults); + this._dispatcher.onTestResult(test, testResult, aggregatedResults); }; const updateSnapshotState = () => { @@ -199,8 +199,12 @@ class TestRunner { throw new CancelRun(); } - this._dispatcher.onTestStart(test.config, test.path); - return runTest(test.path, test.config, this._context.resolver); + this._dispatcher.onTestStart(test); + return runTest( + test.path, + test.context.config, + test.context.resolver, + ); }) .then(result => onResult(test, result)) .catch(err => onFailure(test, err))), @@ -228,17 +232,17 @@ class TestRunner { // Send test suites to workers continuously instead of all at once to track // the start time of individual tests. - const runTestInWorker = ({config, path}) => + const runTestInWorker = test => mutex(() => { if (watcher.isInterrupted()) { return Promise.reject(); } - this._dispatcher.onTestStart(config, path); + this._dispatcher.onTestStart(test); return worker({ - config, - path, + config: test.context.config, + path: test.path, rawModuleMap: watcher.isWatchMode() - ? this._context.moduleMap.getRawModuleMap() + ? test.context.moduleMap.getRawModuleMap() : null, }); }); @@ -439,14 +443,13 @@ class ReporterDispatcher { ); } - onTestResult(config, testResult, results) { + onTestResult(test, testResult, results) { this._reporters.forEach(reporter => - reporter.onTestResult(config, testResult, results, this._runnerContext)); + reporter.onTestResult(test, testResult, results, this._runnerContext)); } - onTestStart(config, path) { - this._reporters.forEach(reporter => - reporter.onTestStart(config, path, this._runnerContext)); + onTestStart(test) { + this._reporters.forEach(reporter => reporter.onTestStart(test)); } onRunStart(config, results, options) { diff --git a/packages/jest-cli/src/TestSequencer.js b/packages/jest-cli/src/TestSequencer.js index 4af0d39add77..37f46c58447a 100644 --- a/packages/jest-cli/src/TestSequencer.js +++ b/packages/jest-cli/src/TestSequencer.js @@ -10,7 +10,7 @@ 'use strict'; import type {AggregatedResult} from 'types/TestResult'; -import type {Config} from 'types/Config'; +import type {Context} from 'types/Context'; import type {Tests} from 'types/TestRunner'; const fs = require('fs'); @@ -20,19 +20,17 @@ const FAIL = 0; const SUCCESS = 1; class TestSequencer { - _config: Config; + _context: Context; _cache: Object; - constructor(config: Config) { - this._config = config; + constructor(context: Context) { + this._context = context; this._cache = {}; } _getTestPerformanceCachePath() { - return getCacheFilePath( - this._config.cacheDirectory, - 'perf-cache-' + this._config.name, - ); + const {config} = this._context; + return getCacheFilePath(config.cacheDirectory, 'perf-cache-' + config.name); } // When running more tests than we have workers available, sort the tests @@ -44,7 +42,7 @@ class TestSequencer { // subsequent runs we use that to run the slowest tests first, yielding the // fastest results. sort(testPaths: Array): Tests { - const config = this._config; + const context = this._context; const stats = {}; const fileSize = filePath => stats[filePath] || (stats[filePath] = fs.statSync(filePath).size); @@ -54,7 +52,7 @@ class TestSequencer { this._cache = {}; try { - if (this._config.cache) { + if (context.config.cache) { this._cache = JSON.parse( fs.readFileSync(this._getTestPerformanceCachePath(), 'utf8'), ); @@ -82,7 +80,7 @@ class TestSequencer { }); return testPaths.map(path => ({ - config, + context, duration: this._cache[path] && this._cache[path][1], path, })); diff --git a/packages/jest-cli/src/__tests__/TestRunner-test.js b/packages/jest-cli/src/__tests__/TestRunner-test.js index a00188c525d3..d5c186d904a0 100644 --- a/packages/jest-cli/src/__tests__/TestRunner-test.js +++ b/packages/jest-cli/src/__tests__/TestRunner-test.js @@ -42,13 +42,12 @@ describe('_createInBandTestRun()', () => { test('injects the rawModuleMap to each the worker in watch mode', () => { const config = {watch: true}; const rawModuleMap = jest.fn(); - const hasteContext = {moduleMap: {getRawModuleMap: () => rawModuleMap}}; - - const runner = new TestRunner(hasteContext, config, {maxWorkers: 2}); + const context = {config, moduleMap: {getRawModuleMap: () => rawModuleMap}}; + const runner = new TestRunner(context, config, {maxWorkers: 2}); return runner ._createParallelTestRun( - [{config, path: './file-test.js'}, {config, path: './file2-test.js'}], + [{context, path: './file-test.js'}, {context, path: './file2-test.js'}], new TestWatcher({isWatchMode: config.watch}), () => {}, () => {}, @@ -70,12 +69,12 @@ describe('_createInBandTestRun()', () => { test('does not inject the rawModuleMap in non watch mode', () => { const config = {watch: false}; - - const runner = new TestRunner({}, config, {maxWorkers: 1}); + const context = {config}; + const runner = new TestRunner(context, config, {maxWorkers: 1}); return runner ._createParallelTestRun( - [{config, path: './file-test.js'}, {config, path: './file2-test.js'}], + [{context, path: './file-test.js'}, {context, path: './file2-test.js'}], new TestWatcher({isWatchMode: config.watch}), () => {}, () => {}, diff --git a/packages/jest-cli/src/__tests__/TestSequencer-test.js b/packages/jest-cli/src/__tests__/TestSequencer-test.js index 3465a3da0a48..e381e545109d 100644 --- a/packages/jest-cli/src/__tests__/TestSequencer-test.js +++ b/packages/jest-cli/src/__tests__/TestSequencer-test.js @@ -18,14 +18,16 @@ const SUCCESS = 1; let sequencer; -const config = { - cache: true, - cacheDirectory: '/cache', - name: 'test', +const context = { + config: { + cache: true, + cacheDirectory: '/cache', + name: 'test', + }, }; beforeEach(() => { - sequencer = new TestSequencer(config); + sequencer = new TestSequencer(context); fs.readFileSync = jest.fn(() => '{}'); fs.statSync = jest.fn(filePath => ({size: filePath.length})); @@ -33,8 +35,8 @@ beforeEach(() => { test('sorts by file size if there is no timing information', () => { expect(sequencer.sort(['/test-a.js', '/test-ab.js'])).toEqual([ - {config, duration: undefined, path: '/test-ab.js'}, - {config, duration: undefined, path: '/test-a.js'}, + {context, duration: undefined, path: '/test-ab.js'}, + {context, duration: undefined, path: '/test-a.js'}, ]); }); @@ -45,8 +47,8 @@ test('sorts based on timing information', () => { '/test-ab.js': [SUCCESS, 3], })); expect(sequencer.sort(['/test-a.js', '/test-ab.js'])).toEqual([ - {config, duration: 5, path: '/test-a.js'}, - {config, duration: 3, path: '/test-ab.js'}, + {context, duration: 5, path: '/test-a.js'}, + {context, duration: 3, path: '/test-ab.js'}, ]); }); @@ -61,10 +63,10 @@ test('sorts based on failures and timing information', () => { expect( sequencer.sort(['/test-a.js', '/test-ab.js', '/test-c.js', '/test-d.js']), ).toEqual([ - {config, duration: 6, path: '/test-c.js'}, - {config, duration: 0, path: '/test-ab.js'}, - {config, duration: 5, path: '/test-a.js'}, - {config, duration: 2, path: '/test-d.js'}, + {context, duration: 6, path: '/test-c.js'}, + {context, duration: 0, path: '/test-ab.js'}, + {context, duration: 5, path: '/test-a.js'}, + {context, duration: 2, path: '/test-d.js'}, ]); }); @@ -86,11 +88,11 @@ test('sorts based on failures, timing information and file size', () => { '/test-efg.js', ]), ).toEqual([ - {config, duration: undefined, path: '/test-efg.js'}, - {config, duration: undefined, path: '/test-c.js'}, - {config, duration: 1, path: '/test-ab.js'}, - {config, duration: 5, path: '/test-a.js'}, - {config, duration: 2, path: '/test-d.js'}, + {context, duration: undefined, path: '/test-efg.js'}, + {context, duration: undefined, path: '/test-c.js'}, + {context, duration: 1, path: '/test-ab.js'}, + {context, duration: 5, path: '/test-a.js'}, + {context, duration: 2, path: '/test-d.js'}, ]); }); diff --git a/packages/jest-cli/src/reporters/BaseReporter.js b/packages/jest-cli/src/reporters/BaseReporter.js index 6d6a8702b213..49ace2212b7d 100644 --- a/packages/jest-cli/src/reporters/BaseReporter.js +++ b/packages/jest-cli/src/reporters/BaseReporter.js @@ -10,7 +10,8 @@ 'use strict'; import type {AggregatedResult, TestResult} from 'types/TestResult'; -import type {Config, Path} from 'types/Config'; +import type {Config} from 'types/Config'; +import type {Test} from 'types/TestRunner'; import type {ReporterOnStartOptions, RunnerContext} from 'types/Reporters'; const preRunMessage = require('../preRunMessage'); @@ -31,13 +32,9 @@ class BaseReporter { preRunMessage.remove(process.stderr); } - onTestResult( - config: Config, - testResult: TestResult, - results: AggregatedResult, - ) {} + onTestResult(test: Test, testResult: TestResult, results: AggregatedResult) {} - onTestStart(config: Config, path: Path, runnerContext: RunnerContext) {} + onTestStart(test: Test) {} onRunComplete( config: Config, diff --git a/packages/jest-cli/src/reporters/CoverageReporter.js b/packages/jest-cli/src/reporters/CoverageReporter.js index 3efb31f2701c..4a5b46231f56 100644 --- a/packages/jest-cli/src/reporters/CoverageReporter.js +++ b/packages/jest-cli/src/reporters/CoverageReporter.js @@ -11,6 +11,7 @@ import type {AggregatedResult, CoverageMap, TestResult} from 'types/TestResult'; import type {Config} from 'types/Config'; +import type {Test} from 'types/TestRunner'; import type {RunnerContext} from 'types/Reporters'; const BaseReporter = require('./BaseReporter'); @@ -40,7 +41,7 @@ class CoverageReporter extends BaseReporter { } onTestResult( - config: Config, + test: Test, testResult: TestResult, aggregatedResults: AggregatedResult, ) { diff --git a/packages/jest-cli/src/reporters/DefaultReporter.js b/packages/jest-cli/src/reporters/DefaultReporter.js index cdb365885685..987090add930 100644 --- a/packages/jest-cli/src/reporters/DefaultReporter.js +++ b/packages/jest-cli/src/reporters/DefaultReporter.js @@ -14,6 +14,7 @@ import type {AggregatedResult, TestResult} from 'types/TestResult'; import type {Config, Path} from 'types/Config'; +import type {Test} from 'types/TestRunner'; import type {ReporterOnStartOptions, RunnerContext} from 'types/Reporters'; const BaseReporter = require('./BaseReporter'); @@ -119,8 +120,8 @@ class DefaultReporter extends BaseReporter { this._status.runStarted(aggregatedResults, options); } - onTestStart(config: Config, testPath: Path) { - this._status.testStarted(testPath, config); + onTestStart(test: Test) { + this._status.testStarted(test.path, test.context.config); } onRunComplete() { @@ -133,12 +134,20 @@ class DefaultReporter extends BaseReporter { } onTestResult( - config: Config, + test: Test, testResult: TestResult, aggregatedResults: AggregatedResult, ) { - this._status.testFinished(config, testResult, aggregatedResults); - this._printTestFileSummary(testResult.testFilePath, config, testResult); + this._status.testFinished( + test.context.config, + testResult, + aggregatedResults, + ); + this._printTestFileSummary( + testResult.testFilePath, + test.context.config, + testResult, + ); } _printTestFileSummary(testPath: Path, config: Config, result: TestResult) { diff --git a/packages/jest-cli/src/reporters/VerboseReporter.js b/packages/jest-cli/src/reporters/VerboseReporter.js index f4a83f550b71..f76062692e7b 100644 --- a/packages/jest-cli/src/reporters/VerboseReporter.js +++ b/packages/jest-cli/src/reporters/VerboseReporter.js @@ -9,13 +9,13 @@ */ 'use strict'; -import type {Config} from 'types/Config'; import type { AggregatedResult, AssertionResult, Suite, TestResult, } from 'types/TestResult'; +import type {Test} from 'types/TestRunner'; const DefaultReporter = require('./DefaultReporter'); const chalk = require('chalk'); @@ -59,11 +59,11 @@ class VerboseReporter extends DefaultReporter { } onTestResult( - config: Config, + test: Test, result: TestResult, aggregatedResults: AggregatedResult, ) { - super.onTestResult(config, result, aggregatedResults); + super.onTestResult(test, result, aggregatedResults); if (!result.testExecError && !result.skipped) { this._logTestResults(result.testResults); } diff --git a/packages/jest-cli/src/runJest.js b/packages/jest-cli/src/runJest.js index ac42aa346b0a..9bebf350cd1a 100644 --- a/packages/jest-cli/src/runJest.js +++ b/packages/jest-cli/src/runJest.js @@ -84,11 +84,13 @@ const runJest = async ( if ( data.paths.length === 1 && - config.silent !== true && - config.verbose !== false + hasteContext.config.silent !== true && + hasteContext.config.verbose !== false ) { // $FlowFixMe - config = Object.assign({}, config, {verbose: true}); + config = (hasteContext.config = Object.assign({}, hasteContext.config, { + verbose: true, + })); } return data; @@ -131,7 +133,7 @@ const runJest = async ( const data = await source.getTestPaths(patternInfo); processTests(data); - const sequencer = new TestSequencer(config); + const sequencer = new TestSequencer(hasteContext); const tests = sequencer.sort(data.paths); const results = await runTests(tests); sequencer.cacheResults(tests, results); diff --git a/types/TestRunner.js b/types/TestRunner.js index 50a345dfed36..04fbd9554dc3 100644 --- a/types/TestRunner.js +++ b/types/TestRunner.js @@ -9,10 +9,11 @@ */ 'use strict'; -import type {Config, Path} from './Config'; +import type {Context} from './Context'; +import type {Path} from './Config'; export type Test = { - config: Config, + context: Context, path: Path, duration?: number, };