Skip to content

Commit

Permalink
Pass “Test”s to reporters; update Test to contain contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Apr 12, 2017
1 parent b0f470e commit 74e1670
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 73 deletions.
33 changes: 18 additions & 15 deletions packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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))),
Expand Down Expand Up @@ -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,
});
});
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 9 additions & 11 deletions packages/jest-cli/src/TestSequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -44,7 +42,7 @@ class TestSequencer {
// subsequent runs we use that to run the slowest tests first, yielding the
// fastest results.
sort(testPaths: Array<string>): Tests {
const config = this._config;
const context = this._context;
const stats = {};
const fileSize = filePath =>
stats[filePath] || (stats[filePath] = fs.statSync(filePath).size);
Expand All @@ -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'),
);
Expand Down Expand Up @@ -82,7 +80,7 @@ class TestSequencer {
});

return testPaths.map(path => ({
config,
context,
duration: this._cache[path] && this._cache[path][1],
path,
}));
Expand Down
13 changes: 6 additions & 7 deletions packages/jest-cli/src/__tests__/TestRunner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}),
() => {},
() => {},
Expand All @@ -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}),
() => {},
() => {},
Expand Down
38 changes: 20 additions & 18 deletions packages/jest-cli/src/__tests__/TestSequencer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ 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}));
});

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'},
]);
});

Expand All @@ -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'},
]);
});

Expand All @@ -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'},
]);
});

Expand All @@ -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'},
]);
});

Expand Down
11 changes: 4 additions & 7 deletions packages/jest-cli/src/reporters/BaseReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-cli/src/reporters/CoverageReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -40,7 +41,7 @@ class CoverageReporter extends BaseReporter {
}

onTestResult(
config: Config,
test: Test,
testResult: TestResult,
aggregatedResults: AggregatedResult,
) {
Expand Down
19 changes: 14 additions & 5 deletions packages/jest-cli/src/reporters/DefaultReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-cli/src/reporters/VerboseReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 6 additions & 4 deletions packages/jest-cli/src/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 74e1670

Please sign in to comment.