Skip to content

Commit

Permalink
Migrate @jest/reporters to TypeScript (#7994)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzorapetti authored and SimenB committed Mar 3, 2019
1 parent ae6d6eb commit d185d22
Show file tree
Hide file tree
Showing 28 changed files with 831 additions and 353 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -74,6 +74,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))
- `[jest-repl]`: Migrate to TypeScript ([#8000](https://github.com/facebook/jest/pull/8000))
- `[jest-validate]`: Migrate to TypeScript ([#7991](https://github.com/facebook/jest/pull/7991))
- `[docs]`: Update CONTRIBUTING.md to add information about running jest with `jest-circus` locally ([#8013](https://github.com/facebook/jest/pull/8013)).
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-reporters/package.json
Expand Up @@ -3,28 +3,33 @@
"description": "Jest's reporters",
"version": "24.1.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@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",
"slash": "^2.0.0",
"source-map": "^0.6.0",
"string-length": "^2.0.0"
},
"devDependencies": {
"@types/exit": "^0.1.30",
"@types/glob": "^7.1.1",
"@types/istanbul-lib-coverage": "^1.1.0",
"@types/istanbul-lib-instrument": "^1.7.2",
"@types/istanbul-lib-source-maps": "^1.2.1",
"@types/node-notifier": "^0.0.28",
"@types/slash": "^2.0.0",
"@types/string-length": "^2.0.0",
"strip-ansi": "^5.0.0"
Expand Down
Expand Up @@ -3,16 +3,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 +26,16 @@ const RUNNING = chalk.reset.inverse.yellow.bold(RUNNING_TEXT) + ' ';
* shifting the whole list.
*/
class CurrentTestList {
_array: Array<{testPath: Path, config: ProjectConfig} | null>;
private _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 +45,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,25 +63,22 @@ class CurrentTestList {
* from the terminal.
*/
export default class Status {
_cache: ?{content: string, clear: string};
_callback: () => void;
_currentTests: CurrentTestList;
_done: boolean;
_emitScheduled: boolean;
_estimatedTime: number;
_height: number;
_interval: IntervalID;
_aggregatedResults: AggregatedResult;
_lastUpdated: number;
_showStatus: boolean;
private _cache: {content: string; clear: string} | null;
private _callback?: () => void;
private _currentTests: CurrentTestList;
private _done: boolean;
private _emitScheduled: boolean;
private _estimatedTime: number;
private _interval?: NodeJS.Timeout;
private _aggregatedResults?: TestResult.AggregatedResult;
private _showStatus: boolean;

constructor() {
this._cache = null;
this._currentTests = new CurrentTestList();
this._done = false;
this._emitScheduled = false;
this._estimatedTime = 0;
this._height = 0;
this._showStatus = false;
}

Expand All @@ -90,7 +87,7 @@ export default class Status {
}

runStarted(
aggregatedResults: AggregatedResult,
aggregatedResults: TestResult.AggregatedResult,
options: ReporterOnStartOptions,
) {
this._estimatedTime = (options && options.estimatedTime) || 0;
Expand All @@ -102,11 +99,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 +113,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 +132,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 @@ -178,13 +174,12 @@ export default class Status {
return (this._cache = {clear, content});
}

_emit() {
private _emit() {
this._cache = null;
this._lastUpdated = Date.now();
this._callback();
if (this._callback) this._callback();
}

_debouncedEmit() {
private _debouncedEmit() {
if (!this._emitScheduled) {
// Perf optimization to avoid two separate renders When
// one test finishes and another test starts executing.
Expand All @@ -196,7 +191,7 @@ export default class Status {
}
}

_tick() {
private _tick() {
this._debouncedEmit();
}
}
Expand Up @@ -3,8 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

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

import path from 'path';
import os from 'os';
//$FlowFixMe: Converted to TS
import {makeGlobalConfig, makeProjectConfig} from '../../../../TestUtils';

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

// $FlowFixMe: IDK...
expect(coverage.data).toMatchSnapshot({path: expect.any(String)});
});
Expand Up @@ -3,7 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';
Expand Down
Expand Up @@ -3,7 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import type {TestSchedulerContext} from 'types/TestScheduler';
Expand Down
1 change: 0 additions & 1 deletion packages/jest-reporters/src/__tests__/utils.test.js
Expand Up @@ -3,7 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import path from 'path';
Expand Down
Expand Up @@ -3,7 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';
Expand Down
Expand Up @@ -3,46 +3,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 {
private _error?: Error;

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) {}
onTestStart(_test: Test) {}

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

_setError(error: Error) {
protected _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;
}
}

0 comments on commit d185d22

Please sign in to comment.