Skip to content

Commit

Permalink
chore: migrate jest-watcher to TypeScript (jestjs#7843)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and captain-yossarian committed Jul 18, 2019
1 parent a714205 commit 6f6eb9e
Show file tree
Hide file tree
Showing 26 changed files with 185 additions and 133 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@
- `[jest-message-util]`: Migrate to TypeScript ([#7834](https://github.com/facebook/jest/pull/7834))
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))
- `[jest-util]`: Migrate to TypeScript ([#7844](https://github.com/facebook/jest/pull/7844))
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))

### Performance

Expand Down
1 change: 1 addition & 0 deletions docs/WatchPlugins.md
Expand Up @@ -155,6 +155,7 @@ class MyWatchPlugin {
For stability and safety reasons, only part of the global configuration keys can be updated with `updateConfigAndRun`. The current white list is as follows:

- [`bail`](configuration.html#bail-number-boolean)
- [`changedSince`](cli.html#changedsince)
- [`collectCoverage`](configuration.html#collectcoverage-boolean)
- [`collectCoverageFrom`](configuration.html#collectcoveragefrom-array)
- [`collectCoverageOnlyFrom`](configuration.html#collectcoverageonlyfrom-array)
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-cli/src/lib/update_global_config.js
Expand Up @@ -23,11 +23,9 @@ export type Options = {
coverageDirectory?: $PropertyType<GlobalConfig, 'coverageDirectory'>,
coverageReporters?: $PropertyType<GlobalConfig, 'coverageReporters'>,
mode?: 'watch' | 'watchAll',
noSCM?: $PropertyType<GlobalConfig, 'noSCM'>,
notify?: $PropertyType<GlobalConfig, 'notify'>,
notifyMode?: $PropertyType<GlobalConfig, 'notifyMode'>,
onlyFailures?: $PropertyType<GlobalConfig, 'onlyFailures'>,
passWithNoTests?: $PropertyType<GlobalConfig, 'passWithNoTests'>,
reporters?: $PropertyType<GlobalConfig, 'reporters'>,
testNamePattern?: $PropertyType<GlobalConfig, 'testNamePattern'>,
testPathPattern?: $PropertyType<GlobalConfig, 'testPathPattern'>,
Expand Down
1 change: 0 additions & 1 deletion packages/jest-cli/src/plugins/quit.js
Expand Up @@ -21,7 +21,6 @@ class QuitPlugin extends BaseWatchPlugin {

async run() {
if (typeof this._stdin.setRawMode === 'function') {
// $FlowFixMe
this._stdin.setRawMode(false);
}
this._stdout.write('\n');
Expand Down
10 changes: 9 additions & 1 deletion packages/jest-types/src/Config.ts
Expand Up @@ -213,6 +213,14 @@ export type InitialOptions = {

export type SnapshotUpdateState = 'all' | 'new' | 'none';

type NotifyMode =
| 'always'
| 'failure'
| 'success'
| 'change'
| 'success-change'
| 'failure-change';

export type GlobalConfig = {
bail: number;
changedSince: string;
Expand Down Expand Up @@ -260,7 +268,7 @@ export type GlobalConfig = {
nonFlagArgs: Array<string>;
noSCM: boolean | null | undefined;
notify: boolean;
notifyMode: string;
notifyMode: NotifyMode;
outputFile: Path | null | undefined;
onlyChanged: boolean;
onlyFailures: boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-watcher/package.json
Expand Up @@ -4,6 +4,8 @@
"version": "24.0.0",
"main": "build/index.js",
"dependencies": {
"@jest/types": "^24.1.0",
"@types/node": "*",
"ansi-escapes": "^3.0.0",
"chalk": "^2.0.1",
"jest-util": "^24.0.0",
Expand Down
43 changes: 0 additions & 43 deletions packages/jest-watcher/src/BaseWatchPlugin.js

This file was deleted.

38 changes: 38 additions & 0 deletions packages/jest-watcher/src/BaseWatchPlugin.ts
@@ -0,0 +1,38 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {WatchPlugin} from './types';

class BaseWatchPlugin implements WatchPlugin {
_stdin: NodeJS.ReadableStream;
_stdout: NodeJS.WritableStream;

constructor({
stdin,
stdout,
}: {
stdin: NodeJS.ReadableStream;
stdout: NodeJS.WritableStream;
}) {
this._stdin = stdin;
this._stdout = stdout;
}

apply() {}

getUsageInfo() {
return null;
}

onKey() {}

run() {
return Promise.resolve();
}
}

export default BaseWatchPlugin;
Expand Up @@ -3,17 +3,15 @@
*
* 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 {
import {
JestHookSubscriber,
JestHookEmitter,
FileChange,
ShouldRunTestSuite,
TestRunComplete,
} from 'types/JestHooks';
} from './types';

type AvailableHooks =
| 'onFileChange'
Expand All @@ -22,9 +20,9 @@ type AvailableHooks =

class JestHooks {
_listeners: {
onFileChange: Array<FileChange>,
onTestRunComplete: Array<TestRunComplete>,
shouldRunTestSuite: Array<ShouldRunTestSuite>,
onFileChange: Array<FileChange>;
onTestRunComplete: Array<TestRunComplete>;
shouldRunTestSuite: Array<ShouldRunTestSuite>;
};

constructor() {
Expand Down Expand Up @@ -61,14 +59,15 @@ class JestHooks {
this._listeners.onTestRunComplete.forEach(listener =>
listener(results),
),
shouldRunTestSuite: async testSuiteInfo =>
Promise.all(
shouldRunTestSuite: async testSuiteInfo => {
const result = await Promise.all(
this._listeners.shouldRunTestSuite.map(listener =>
listener(testSuiteInfo),
),
).then(result =>
result.every(shouldRunTestSuite => shouldRunTestSuite),
),
);

return result.every(Boolean);
},
};
}
}
Expand Down
Expand Up @@ -3,14 +3,8 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

import type {ScrollOptions} from 'types/Watch';

import chalk from 'chalk';
import ansiEscapes from 'ansi-escapes';
import {specialChars} from 'jest-util';
Expand Down Expand Up @@ -41,18 +35,20 @@ const isValid = (pattern: string) => {
};

export default class PatternPrompt {
_pipe: stream$Writable | tty$WriteStream;
_pipe: NodeJS.WritableStream;
_prompt: Prompt;
_entityName: string;
_currentUsageRows: number;

constructor(pipe: stream$Writable | tty$WriteStream, prompt: Prompt) {
constructor(pipe: NodeJS.WritableStream, prompt: Prompt) {
// TODO: Should come in the constructor
this._entityName = '';
this._pipe = pipe;
this._prompt = prompt;
this._currentUsageRows = usageRows;
}

run(onSuccess: Function, onCancel: Function, options?: {header: string}) {
run(onSuccess: () => void, onCancel: () => void, options?: {header: string}) {
this._pipe.write(ansiEscapes.cursorHide);
this._pipe.write(CLEAR);

Expand Down Expand Up @@ -86,7 +82,7 @@ export default class PatternPrompt {
};
}

_onChange(pattern: string, options: ScrollOptions) {
protected _onChange() {
this._pipe.write(ansiEscapes.eraseLine);
this._pipe.write(ansiEscapes.cursorLeft);
}
Expand Down
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
*/

const isWindows = process.platform === 'win32';
Expand Down
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
*/

export {default as BaseWatchPlugin} from './BaseWatchPlugin';
Expand Down
Expand Up @@ -3,36 +3,44 @@
*
* 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 {ScrollOptions} from 'types/Watch';

import {ScrollOptions} from '../types';
import {KEYS} from '../constants';

export default class Prompt {
_entering: boolean;
_value: string;
_onChange: Function;
_onSuccess: Function;
_onCancel: Function;
_onChange: () => void;
_onSuccess: (value?: string) => void;
_onCancel: (value?: string) => void;
_offset: number;
_promptLength: number;
_selection: string | null;

constructor() {
(this: any)._onResize = this._onResize.bind(this);
// Copied from `enter` to satisfy TS
this._entering = true;
this._value = '';
this._selection = null;
this._offset = -1;
this._promptLength = 0;

this._onChange = () => {};
this._onSuccess = () => {};
this._onCancel = () => {};

this._onResize = this._onResize.bind(this);
}

_onResize() {
this._onChange(this._value);
private _onResize() {
this._onChange();
}

enter(
onChange: (pattern: string, options: ScrollOptions) => void,
onSuccess: Function,
onCancel: Function,
onSuccess: () => void,
onCancel: () => void,
) {
this._entering = true;
this._value = '';
Expand Down
Expand Up @@ -3,12 +3,8 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

import formatTestNameByPattern from '../formatTestNameByPattern';

describe('for multiline test name returns', () => {
Expand Down
Expand Up @@ -6,8 +6,6 @@
*
*/

'use strict';

import Prompt from '../Prompt';
import {KEYS} from '../../constants';

Expand Down
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 chalk from 'chalk';
Expand Down
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 chalk from 'chalk';
Expand All @@ -31,7 +29,7 @@ export default (testName: string, pattern: string, width: number) => {
return chalk.dim(inlineTestName);
}

const startPatternIndex = Math.max(match.index, 0);
const startPatternIndex = Math.max(match.index || 0, 0);
const endPatternIndex = startPatternIndex + match[0].length;

if (inlineTestName.length <= width) {
Expand Down

0 comments on commit 6f6eb9e

Please sign in to comment.