Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate @jest/reporters to TypeScript #7994

Merged
merged 11 commits into from Mar 3, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -61,6 +61,7 @@
- `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968))
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988))
- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987))
- `[@jest/reporters]`: Migrate to TypeScript ([#7994](https://github.com/facebook/jest/pull/7994))

### Performance

Expand Down
7 changes: 7 additions & 0 deletions packages/jest-reporters/package.json
Expand Up @@ -3,15 +3,22 @@
"description": "Jest's reporters",
"version": "24.1.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/core": "^24.1.0",
SimenB marked this conversation as resolved.
Show resolved Hide resolved
"@jest/environment": "^24.1.0",
"@jest/transform": "^24.1.0",
"@jest/types": "^24.1.0",
"chalk": "^2.0.1",
"exit": "^0.1.2",
"glob": "^7.1.2",
"istanbul-api": "^2.1.1",
"istanbul-lib-coverage": "^2.0.2",
"istanbul-lib-instrument": "^3.0.1",
"istanbul-lib-source-maps": "^3.0.1",
"jest-haste-map": "^24.0.0",
"jest-resolve": "^24.1.0",
"jest-runtime": "^24.1.0",
"jest-util": "^24.0.0",
"jest-worker": "^24.0.0",
"node-notifier": "^5.2.1",
Expand Down
Expand Up @@ -4,15 +4,13 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {AggregatedResult, TestResult} from 'types/TestResult';
import type {ProjectConfig, Path} from 'types/Config';
import type {ReporterOnStartOptions} from 'types/Reporters';
import {TestResult, Config} from '@jest/types';

import chalk from 'chalk';
import stringLength from 'string-length';
import {ReporterOnStartOptions} from './types';
import {
getSummary,
trimAndFormatPath,
Expand All @@ -29,13 +27,13 @@ const RUNNING = chalk.reset.inverse.yellow.bold(RUNNING_TEXT) + ' ';
* shifting the whole list.
*/
class CurrentTestList {
_array: Array<{testPath: Path, config: ProjectConfig} | null>;
_array: Array<{testPath: Config.Path; config: Config.ProjectConfig} | null>;

constructor() {
this._array = [];
}

add(testPath, config) {
add(testPath: Config.Path, config: Config.ProjectConfig) {
const index = this._array.indexOf(null);
const record = {config, testPath};
if (index !== -1) {
Expand All @@ -45,9 +43,9 @@ class CurrentTestList {
}
}

delete(testPath) {
delete(testPath: Config.Path) {
const record = this._array.find(
record => record && record.testPath === testPath,
record => record !== null && record.testPath === testPath,
);
this._array[this._array.indexOf(record || null)] = null;
}
Expand All @@ -63,16 +61,16 @@ class CurrentTestList {
* from the terminal.
*/
export default class Status {
_cache: ?{content: string, clear: string};
_callback: () => void;
_cache: {content: string; clear: string} | null;
SimenB marked this conversation as resolved.
Show resolved Hide resolved
SimenB marked this conversation as resolved.
Show resolved Hide resolved
_callback?: () => void;
_currentTests: CurrentTestList;
_done: boolean;
_emitScheduled: boolean;
_estimatedTime: number;
_height: number;
_interval: IntervalID;
_aggregatedResults: AggregatedResult;
_lastUpdated: number;
_interval?: NodeJS.Timeout;
_aggregatedResults?: TestResult.AggregatedResult;
_lastUpdated?: number;
_showStatus: boolean;

constructor() {
Expand All @@ -90,7 +88,7 @@ export default class Status {
}

runStarted(
aggregatedResults: AggregatedResult,
aggregatedResults: TestResult.AggregatedResult,
options: ReporterOnStartOptions,
) {
this._estimatedTime = (options && options.estimatedTime) || 0;
Expand All @@ -102,11 +100,11 @@ export default class Status {

runFinished() {
this._done = true;
clearInterval(this._interval);
if (this._interval) clearInterval(this._interval);
this._emit();
}

testStarted(testPath: Path, config: ProjectConfig) {
testStarted(testPath: Config.Path, config: Config.ProjectConfig) {
this._currentTests.add(testPath, config);
if (!this._showStatus) {
this._emit();
Expand All @@ -116,9 +114,9 @@ export default class Status {
}

testFinished(
config: ProjectConfig,
testResult: TestResult,
aggregatedResults: AggregatedResult,
config: Config.ProjectConfig,
testResult: TestResult.TestResult,
aggregatedResults: TestResult.AggregatedResult,
) {
const {testFilePath} = testResult;
this._aggregatedResults = aggregatedResults;
Expand All @@ -135,8 +133,7 @@ export default class Status {
return {clear: '', content: ''};
}

// $FlowFixMe
const width: number = process.stdout.columns;
const width: number = process.stdout.columns!;
let content = '\n';
this._currentTests.get().forEach(record => {
if (record) {
Expand Down Expand Up @@ -181,7 +178,7 @@ export default class Status {
_emit() {
this._cache = null;
this._lastUpdated = Date.now();
this._callback();
if (this._callback) this._callback();
}

_debouncedEmit() {
Expand Down
Expand Up @@ -4,45 +4,48 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {AggregatedResult, TestResult} from 'types/TestResult';
import type {Context} from 'types/Context';
import type {Test} from 'types/TestRunner';
import type {ReporterOnStartOptions} from 'types/Reporters';

import {TestResult} from '@jest/types';
import {preRunMessage} from 'jest-util';
import {ReporterOnStartOptions, Context, Test, Reporter} from './types';

const {remove: preRunMessageRemove} = preRunMessage;

export default class BaseReporter {
_error: ?Error;
export default class BaseReporter implements Reporter {
_error?: Error;
SimenB marked this conversation as resolved.
Show resolved Hide resolved

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

onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {
onRunStart(
results: TestResult.AggregatedResult,
options: ReporterOnStartOptions,
) {
preRunMessageRemove(process.stderr);
}

onTestResult(test: Test, testResult: TestResult, results: AggregatedResult) {}
onTestResult(
test: Test,
testResult: TestResult.TestResult,
results: TestResult.AggregatedResult,
) {}

onTestStart(test: Test) {}

onRunComplete(
contexts: Set<Context>,
aggregatedResults: AggregatedResult,
): ?Promise<void> {}
aggregatedResults: TestResult.AggregatedResult,
): Promise<void> | void {}

_setError(error: Error) {
this._error = error;
}

// Return an error that occurred during reporting. This error will
// define whether the test run was successful or failed.
getLastError(): ?Error {
getLastError() {
return this._error;
}
}