Skip to content

Commit df1563f

Browse files
authored
Flowify reporters in jest-cli #2 (#1151)
* Flowify reporters in jest-cli * Cleanup some type annotations.
1 parent 90e942f commit df1563f

8 files changed

Lines changed: 135 additions & 51 deletions

File tree

.eslintignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
bin/
2-
docs/
1+
**/coverage/**
32
**/node_modules/**
43
**/vendor/**
5-
website/
6-
**/coverage/**
4+
bin/
5+
docs/
76
packages/*/build/**
7+
types/**
8+
website/

packages/jest-cli/src/reporters/DefaultTestReporter.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,34 @@
44
* This source code is licensed under the BSD-style license found in the
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
79
*/
810
'use strict';
911

12+
import type {AggregatedResult, TestResult} from 'types/TestResult';
13+
import type {Config} from 'types/Config';
14+
import type {Process} from 'types/Process';
15+
1016
const chalk = require('chalk');
1117
const formatFailureMessage = require('jest-util').formatFailureMessage;
1218
const path = require('path');
1319
const VerboseLogger = require('./VerboseLogger');
1420

21+
type SnapshotSummary = {
22+
added: number,
23+
didUpdate: boolean,
24+
filesAdded: number,
25+
filesRemoved: number,
26+
filesUnmatched: number,
27+
filesUpdated: number,
28+
matched: number,
29+
total: number,
30+
unchecked: number,
31+
unmatched: number,
32+
updated: number,
33+
};
34+
1535
// Explicitly reset for these messages since they can get written out in the
1636
// middle of error logging (should have listened to Spengler and not crossed the
1737
// streams).
@@ -34,23 +54,31 @@ const pluralize = (word, count) => `${count} ${word}${count === 1 ? '' : 's'}`;
3454

3555
class DefaultTestReporter {
3656

37-
constructor(customProcess) {
57+
_config: Config;
58+
_process: Process;
59+
verboseLogger: VerboseLogger;
60+
61+
constructor(customProcess: Process) {
3862
this._process = customProcess || process;
3963
}
4064

41-
log(string) {
42-
this._process.stdout.write(string + '\n');
65+
log(message: string) {
66+
this._process.stdout.write(message + '\n');
4367
}
4468

45-
onRunStart(config, results) {
69+
onRunStart(config: Config, results: AggregatedResult) {
4670
this._config = config;
4771
this._printWaitingOn(results);
4872
if (this._config.verbose) {
4973
this.verboseLogger = new VerboseLogger(this._process);
5074
}
5175
}
5276

53-
onTestResult(config, testResult, results) {
77+
onTestResult(
78+
config: Config,
79+
testResult: TestResult,
80+
results: AggregatedResult,
81+
) {
5482
this._clearWaitingOn();
5583

5684
const pathStr =
@@ -105,7 +133,7 @@ class DefaultTestReporter {
105133
this._printWaitingOn(results);
106134
}
107135

108-
onRunComplete(config, aggregatedResults) {
136+
onRunComplete(config: Config, aggregatedResults: AggregatedResult) {
109137
const totalTestSuites = aggregatedResults.numTotalTestSuites;
110138
const failedTests = aggregatedResults.numFailedTests;
111139
const passedTests = aggregatedResults.numPassedTests;
@@ -158,7 +186,7 @@ class DefaultTestReporter {
158186
return snapshotFailure ? false : aggregatedResults.success;
159187
}
160188

161-
_getSnapshotSummary(aggregatedResults) {
189+
_getSnapshotSummary(aggregatedResults: AggregatedResult): SnapshotSummary {
162190
let added = 0;
163191
let filesAdded = 0;
164192
let filesRemoved = aggregatedResults.snapshotFilesRemoved;
@@ -204,7 +232,7 @@ class DefaultTestReporter {
204232
};
205233
}
206234

207-
_printSnapshotSummary(snapshots) {
235+
_printSnapshotSummary(snapshots: SnapshotSummary) {
208236
if (
209237
snapshots.added ||
210238
snapshots.filesRemoved ||
@@ -265,7 +293,7 @@ class DefaultTestReporter {
265293
}
266294
}
267295

268-
_printSummary(aggregatedResults) {
296+
_printSummary(aggregatedResults: AggregatedResult) {
269297
// If there were any failing tests and there was a large number of tests
270298
// executed, re-print the failing results at the end of execution output.
271299
const failedTests = aggregatedResults.numFailedTests;
@@ -305,7 +333,7 @@ class DefaultTestReporter {
305333
this._process.stdout.write(this._config.noHighlight ? '' : '\r\x1B[K');
306334
}
307335

308-
_printWaitingOn(results) {
336+
_printWaitingOn(results: AggregatedResult) {
309337
const remaining = results.numTotalTestSuites -
310338
results.numPassedTestSuites -
311339
results.numFailedTestSuites -

packages/jest-cli/src/reporters/IstanbulTestReporter.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* This source code is licensed under the BSD-style license found in the
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
79
*/
810
'use strict';
911

@@ -16,8 +18,15 @@ const reporter = new istanbul.Reporter();
1618

1719
const FAIL_COLOR = chalk.bold.red;
1820

21+
import type {Config} from 'types/Config';
22+
import type {AggregatedResult, TestResult} from 'types/TestResult';
23+
1924
class IstanbulTestReporter extends DefaultTestReporter {
20-
onTestResult(config, testResult, aggregatedResults) {
25+
onTestResult(
26+
config: Config,
27+
testResult: TestResult,
28+
aggregatedResults: AggregatedResult,
29+
) {
2130
super.onTestResult(config, testResult, aggregatedResults);
2231

2332
if (config.collectCoverage && testResult.coverage) {
@@ -29,7 +38,7 @@ class IstanbulTestReporter extends DefaultTestReporter {
2938
}
3039
}
3140

32-
onRunComplete(config, aggregatedResults) {
41+
onRunComplete(config: Config, aggregatedResults: AggregatedResult) {
3342
aggregatedResults.success = super.onRunComplete(config, aggregatedResults);
3443

3544
if (config.collectCoverage) {

packages/jest-cli/src/reporters/VerboseLogger.js

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,62 @@
44
* This source code is licensed under the BSD-style license found in the
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
79
*/
810
'use strict';
911

12+
import type {
13+
AssertionResult,
14+
Suite,
15+
} from 'types/TestResult';
16+
import type {Process} from 'types/Process';
17+
1018
const chalk = require('chalk');
1119

1220
class VerboseLogger {
13-
constructor(customProcess) {
21+
_process: Process;
22+
23+
constructor(customProcess?: ?Process) {
1424
this._process = customProcess || process;
1525
}
1626

17-
logTestResults(testResults) {
18-
groupTestsBySuites(testResults).forEach(suite =>
27+
static groupTestsBySuites(testResults: Array<AssertionResult>) {
28+
const root: Suite = {
29+
suites: [],
30+
tests: [],
31+
title: 'Root Suite',
32+
};
33+
34+
testResults.forEach(testResult => {
35+
let targetSuite = root;
36+
37+
// Find the target suite for this test,
38+
// creating nested suites as necessary.
39+
for (const title of testResult.ancestorTitles) {
40+
let matchingSuite = targetSuite.suites.find(s => s.title === title);
41+
if (!matchingSuite) {
42+
matchingSuite = {title, suites: [], tests: []};
43+
targetSuite.suites.push(matchingSuite);
44+
}
45+
targetSuite = matchingSuite;
46+
}
47+
48+
targetSuite.tests.push(testResult);
49+
});
50+
51+
return root.suites;
52+
}
53+
54+
logTestResults(testResults: Array<AssertionResult>) {
55+
VerboseLogger.groupTestsBySuites(testResults).forEach(suite =>
1956
this._logSuite(suite, 0)
2057
);
2158

2259
this._logLine();
2360
}
2461

25-
_logSuite(suite, indentLevel) {
62+
_logSuite(suite: Suite, indentLevel: number) {
2663
this._logLine(suite.title, indentLevel);
2764

2865
suite.tests.forEach(test =>
@@ -34,7 +71,7 @@ class VerboseLogger {
3471
);
3572
}
3673

37-
_getIcon(status) {
74+
_getIcon(status: string) {
3875
if (status === 'failed') {
3976
return chalk.red('\u2715');
4077
} else if (status === 'pending') {
@@ -44,42 +81,15 @@ class VerboseLogger {
4481
}
4582
}
4683

47-
_logTest(test, indentLevel) {
84+
_logTest(test: AssertionResult, indentLevel: number) {
4885
const status = this._getIcon(test.status);
4986
this._logLine(`${status} ${chalk.gray(test.title)}`, indentLevel);
5087
}
5188

52-
_logLine(str, indentLevel) {
53-
str = str || '';
54-
indentLevel = indentLevel || 0;
55-
89+
_logLine(str: string = '', indentLevel: number = 0) {
5690
const indentation = ' '.repeat(indentLevel);
5791
this._process.stdout.write(`${indentation}${str}\n`);
5892
}
5993
}
6094

61-
function groupTestsBySuites(testResults) {
62-
const root = {suites: []};
63-
64-
testResults.forEach(testResult => {
65-
let targetSuite = root;
66-
67-
// Find the target suite for this test,
68-
// creating nested suites as necessary.
69-
for (const title of testResult.ancestorTitles) {
70-
let matchingSuite = targetSuite.suites.find(s => s.title === title);
71-
if (!matchingSuite) {
72-
matchingSuite = {title, suites: [], tests: []};
73-
targetSuite.suites.push(matchingSuite);
74-
}
75-
targetSuite = matchingSuite;
76-
}
77-
78-
targetSuite.tests.push(testResult);
79-
});
80-
81-
return root.suites;
82-
}
83-
84-
VerboseLogger.groupTestsBySuites = groupTestsBySuites;
8595
module.exports = VerboseLogger;

packages/jest-snapshot/src/SnapshotFile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export type MatchResult = {
2525
pass: boolean,
2626
};
2727

28+
type SnapshotData = {[key: string]: string};
29+
2830
type SaveStatus = {
2931
deleted: boolean,
3032
saved: boolean,
@@ -47,7 +49,7 @@ const fileExists = (filePath: Path): boolean => {
4749

4850
class SnapshotFile {
4951

50-
_content: {[key: string]: any};
52+
_content: SnapshotData;
5153
_dirty: boolean;
5254
_filename: Path;
5355
_uncheckedKeys: Set;
@@ -59,7 +61,9 @@ class SnapshotFile {
5961
this._content = Object.create(null);
6062
if (this.fileExists(filename)) {
6163
try {
64+
/* eslint-disable no-useless-call */
6265
Object.assign(this._content, require.call(null, filename));
66+
/* eslint-enable no-useless-call */
6367
} catch (e) {}
6468
}
6569
this._uncheckedKeys = new Set(Object.keys(this._content));

types/Config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export type Config = {
2626
colors: boolean,
2727
coverageCollector: Path,
2828
coverageReporters: Array<string>,
29+
coverageThreshold: {
30+
global: {
31+
[key: string]: number,
32+
},
33+
},
2934
globals: ConfigGlobals,
3035
haste: HasteConfig,
3136
mocksPattern: string,

types/Process.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Process {
2+
stdout : stream$Writable | tty$WriteStream;
3+
exit(code? : number) : void;
4+
};

types/TestResult.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,32 @@ export type AssertionResult = {
3636
numPassingAsserts: number,
3737
};
3838

39+
export type AggregatedResult = {
40+
didUpdate: boolean,
41+
numFailedTests: number,
42+
numFailedTestSuites: number,
43+
numPassedTests: number,
44+
numPassedTestSuites: number,
45+
numPendingTests: number,
46+
numRuntimeErrorTestSuites: number,
47+
numTotalTests: number,
48+
numTotalTestSuites: number,
49+
snapshotFilesRemoved: number,
50+
startTime: number,
51+
success: boolean,
52+
testResults: Array<TestResult>,
53+
};
54+
55+
export type Suite = {
56+
title: string,
57+
suites: Array<Suite>,
58+
tests: Array<AssertionResult>,
59+
};
60+
3961
export type TestResult = {
4062
coverage: ?Coverage,
4163
hasUncheckedKeys: boolean,
64+
message: string,
4265
numFailingTests: number,
4366
numPassingTests: number,
4467
numPendingTests: number,

0 commit comments

Comments
 (0)