Skip to content

Commit d185d22

Browse files
lorenzorapettiSimenB
authored andcommitted
Migrate @jest/reporters to TypeScript (#7994)
1 parent ae6d6eb commit d185d22

28 files changed

+831
-353
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
- `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968))
7575
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988))
7676
- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987))
77+
- `[@jest/reporters]`: Migrate to TypeScript ([#7994](https://github.com/facebook/jest/pull/7994))
7778
- `[jest-repl]`: Migrate to TypeScript ([#8000](https://github.com/facebook/jest/pull/8000))
7879
- `[jest-validate]`: Migrate to TypeScript ([#7991](https://github.com/facebook/jest/pull/7991))
7980
- `[docs]`: Update CONTRIBUTING.md to add information about running jest with `jest-circus` locally ([#8013](https://github.com/facebook/jest/pull/8013)).

packages/jest-reporters/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,33 @@
33
"description": "Jest's reporters",
44
"version": "24.1.0",
55
"main": "build/index.js",
6+
"types": "build/index.d.ts",
67
"dependencies": {
8+
"@jest/environment": "^24.1.0",
79
"@jest/transform": "^24.1.0",
10+
"@jest/types": "^24.1.0",
811
"chalk": "^2.0.1",
912
"exit": "^0.1.2",
1013
"glob": "^7.1.2",
1114
"istanbul-api": "^2.1.1",
1215
"istanbul-lib-coverage": "^2.0.2",
1316
"istanbul-lib-instrument": "^3.0.1",
1417
"istanbul-lib-source-maps": "^3.0.1",
18+
"jest-haste-map": "^24.0.0",
19+
"jest-resolve": "^24.1.0",
20+
"jest-runtime": "^24.1.0",
1521
"jest-util": "^24.0.0",
1622
"jest-worker": "^24.0.0",
1723
"node-notifier": "^5.2.1",
1824
"slash": "^2.0.0",
25+
"source-map": "^0.6.0",
1926
"string-length": "^2.0.0"
2027
},
2128
"devDependencies": {
2229
"@types/exit": "^0.1.30",
2330
"@types/glob": "^7.1.1",
24-
"@types/istanbul-lib-coverage": "^1.1.0",
2531
"@types/istanbul-lib-instrument": "^1.7.2",
2632
"@types/istanbul-lib-source-maps": "^1.2.1",
27-
"@types/node-notifier": "^0.0.28",
2833
"@types/slash": "^2.0.0",
2934
"@types/string-length": "^2.0.0",
3035
"strip-ansi": "^5.0.0"

packages/jest-reporters/src/Status.js renamed to packages/jest-reporters/src/Status.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {AggregatedResult, TestResult} from 'types/TestResult';
11-
import type {ProjectConfig, Path} from 'types/Config';
12-
import type {ReporterOnStartOptions} from 'types/Reporters';
8+
import {TestResult, Config} from '@jest/types';
139

1410
import chalk from 'chalk';
1511
import stringLength from 'string-length';
12+
import {ReporterOnStartOptions} from './types';
1613
import {
1714
getSummary,
1815
trimAndFormatPath,
@@ -29,13 +26,16 @@ const RUNNING = chalk.reset.inverse.yellow.bold(RUNNING_TEXT) + ' ';
2926
* shifting the whole list.
3027
*/
3128
class CurrentTestList {
32-
_array: Array<{testPath: Path, config: ProjectConfig} | null>;
29+
private _array: Array<{
30+
testPath: Config.Path;
31+
config: Config.ProjectConfig;
32+
} | null>;
3333

3434
constructor() {
3535
this._array = [];
3636
}
3737

38-
add(testPath, config) {
38+
add(testPath: Config.Path, config: Config.ProjectConfig) {
3939
const index = this._array.indexOf(null);
4040
const record = {config, testPath};
4141
if (index !== -1) {
@@ -45,9 +45,9 @@ class CurrentTestList {
4545
}
4646
}
4747

48-
delete(testPath) {
48+
delete(testPath: Config.Path) {
4949
const record = this._array.find(
50-
record => record && record.testPath === testPath,
50+
record => record !== null && record.testPath === testPath,
5151
);
5252
this._array[this._array.indexOf(record || null)] = null;
5353
}
@@ -63,25 +63,22 @@ class CurrentTestList {
6363
* from the terminal.
6464
*/
6565
export default class Status {
66-
_cache: ?{content: string, clear: string};
67-
_callback: () => void;
68-
_currentTests: CurrentTestList;
69-
_done: boolean;
70-
_emitScheduled: boolean;
71-
_estimatedTime: number;
72-
_height: number;
73-
_interval: IntervalID;
74-
_aggregatedResults: AggregatedResult;
75-
_lastUpdated: number;
76-
_showStatus: boolean;
66+
private _cache: {content: string; clear: string} | null;
67+
private _callback?: () => void;
68+
private _currentTests: CurrentTestList;
69+
private _done: boolean;
70+
private _emitScheduled: boolean;
71+
private _estimatedTime: number;
72+
private _interval?: NodeJS.Timeout;
73+
private _aggregatedResults?: TestResult.AggregatedResult;
74+
private _showStatus: boolean;
7775

7876
constructor() {
7977
this._cache = null;
8078
this._currentTests = new CurrentTestList();
8179
this._done = false;
8280
this._emitScheduled = false;
8381
this._estimatedTime = 0;
84-
this._height = 0;
8582
this._showStatus = false;
8683
}
8784

@@ -90,7 +87,7 @@ export default class Status {
9087
}
9188

9289
runStarted(
93-
aggregatedResults: AggregatedResult,
90+
aggregatedResults: TestResult.AggregatedResult,
9491
options: ReporterOnStartOptions,
9592
) {
9693
this._estimatedTime = (options && options.estimatedTime) || 0;
@@ -102,11 +99,11 @@ export default class Status {
10299

103100
runFinished() {
104101
this._done = true;
105-
clearInterval(this._interval);
102+
if (this._interval) clearInterval(this._interval);
106103
this._emit();
107104
}
108105

109-
testStarted(testPath: Path, config: ProjectConfig) {
106+
testStarted(testPath: Config.Path, config: Config.ProjectConfig) {
110107
this._currentTests.add(testPath, config);
111108
if (!this._showStatus) {
112109
this._emit();
@@ -116,9 +113,9 @@ export default class Status {
116113
}
117114

118115
testFinished(
119-
config: ProjectConfig,
120-
testResult: TestResult,
121-
aggregatedResults: AggregatedResult,
116+
_config: Config.ProjectConfig,
117+
testResult: TestResult.TestResult,
118+
aggregatedResults: TestResult.AggregatedResult,
122119
) {
123120
const {testFilePath} = testResult;
124121
this._aggregatedResults = aggregatedResults;
@@ -135,8 +132,7 @@ export default class Status {
135132
return {clear: '', content: ''};
136133
}
137134

138-
// $FlowFixMe
139-
const width: number = process.stdout.columns;
135+
const width: number = process.stdout.columns!;
140136
let content = '\n';
141137
this._currentTests.get().forEach(record => {
142138
if (record) {
@@ -178,13 +174,12 @@ export default class Status {
178174
return (this._cache = {clear, content});
179175
}
180176

181-
_emit() {
177+
private _emit() {
182178
this._cache = null;
183-
this._lastUpdated = Date.now();
184-
this._callback();
179+
if (this._callback) this._callback();
185180
}
186181

187-
_debouncedEmit() {
182+
private _debouncedEmit() {
188183
if (!this._emitScheduled) {
189184
// Perf optimization to avoid two separate renders When
190185
// one test finishes and another test starts executing.
@@ -196,7 +191,7 @@ export default class Status {
196191
}
197192
}
198193

199-
_tick() {
194+
private _tick() {
200195
this._debouncedEmit();
201196
}
202197
}

packages/jest-reporters/src/__tests__/generateEmptyCoverage.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

108
import istanbulCoverage from 'istanbul-lib-coverage';
@@ -13,7 +11,6 @@ import generateEmptyCoverage from '../generateEmptyCoverage';
1311

1412
import path from 'path';
1513
import os from 'os';
16-
//$FlowFixMe: Converted to TS
1714
import {makeGlobalConfig, makeProjectConfig} from '../../../../TestUtils';
1815

1916
jest.mock('@jest/transform', () => ({
@@ -64,6 +61,5 @@ it('generates an empty coverage object for a file without running it', () => {
6461
coverage = sourceMapStore.transformCoverage(coverageMap).map;
6562
}
6663

67-
// $FlowFixMe: IDK...
6864
expect(coverage.data).toMatchSnapshot({path: expect.any(String)});
6965
});

packages/jest-reporters/src/__tests__/get_snapshot_status.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
76
*/
87

98
'use strict';

packages/jest-reporters/src/__tests__/notify_reporter.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
76
*/
87

98
import type {TestSchedulerContext} from 'types/TestScheduler';

packages/jest-reporters/src/__tests__/utils.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
76
*/
87

98
import path from 'path';

packages/jest-reporters/src/__tests__/verbose_reporter.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
76
*/
87

98
'use strict';

packages/jest-reporters/src/base_reporter.js renamed to packages/jest-reporters/src/base_reporter.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,48 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {AggregatedResult, TestResult} from 'types/TestResult';
11-
import type {Context} from 'types/Context';
12-
import type {Test} from 'types/TestRunner';
13-
import type {ReporterOnStartOptions} from 'types/Reporters';
14-
8+
import {TestResult} from '@jest/types';
159
import {preRunMessage} from 'jest-util';
10+
import {ReporterOnStartOptions, Context, Test, Reporter} from './types';
1611

1712
const {remove: preRunMessageRemove} = preRunMessage;
1813

19-
export default class BaseReporter {
20-
_error: ?Error;
14+
export default class BaseReporter implements Reporter {
15+
private _error?: Error;
2116

2217
log(message: string) {
2318
process.stderr.write(message + '\n');
2419
}
2520

26-
onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {
21+
onRunStart(
22+
_results: TestResult.AggregatedResult,
23+
_options: ReporterOnStartOptions,
24+
) {
2725
preRunMessageRemove(process.stderr);
2826
}
2927

30-
onTestResult(test: Test, testResult: TestResult, results: AggregatedResult) {}
28+
onTestResult(
29+
_test: Test,
30+
_testResult: TestResult.TestResult,
31+
_results: TestResult.AggregatedResult,
32+
) {}
3133

32-
onTestStart(test: Test) {}
34+
onTestStart(_test: Test) {}
3335

3436
onRunComplete(
35-
contexts: Set<Context>,
36-
aggregatedResults: AggregatedResult,
37-
): ?Promise<void> {}
37+
_contexts: Set<Context>,
38+
_aggregatedResults: TestResult.AggregatedResult,
39+
): Promise<void> | void {}
3840

39-
_setError(error: Error) {
41+
protected _setError(error: Error) {
4042
this._error = error;
4143
}
4244

4345
// Return an error that occurred during reporting. This error will
4446
// define whether the test run was successful or failed.
45-
getLastError(): ?Error {
47+
getLastError() {
4648
return this._error;
4749
}
4850
}

0 commit comments

Comments
 (0)