Skip to content

Commit 459d0f8

Browse files
committed
chore: migrate jest-watcher to TypeScript
1 parent dc35500 commit 459d0f8

23 files changed

+182
-132
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- `[jest-serializer]`: Migrate to TypeScript ([#7841](https://github.com/facebook/jest/pull/7841))
2222
- `[jest-message-util]`: Migrate to TypeScript ([#7834](https://github.com/facebook/jest/pull/7834))
2323
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))
24+
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
2425

2526
### Performance
2627

packages/jest-cli/src/plugins/quit.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class QuitPlugin extends BaseWatchPlugin {
2121

2222
async run() {
2323
if (typeof this._stdin.setRawMode === 'function') {
24-
// $FlowFixMe
2524
this._stdin.setRawMode(false);
2625
}
2726
this._stdout.write('\n');

packages/jest-runner/src/runTest.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ async function runTestInternal(
8989
if (customEnvironment) {
9090
testEnvironment = getTestEnvironment({
9191
...config,
92-
// $FlowFixMe
9392
testEnvironment: customEnvironment,
9493
});
9594
}

packages/jest-types/src/Config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ export type InitialOptions = {
213213

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

216+
type NotifyMode =
217+
| 'always'
218+
| 'failure'
219+
| 'success'
220+
| 'change'
221+
| 'success-change'
222+
| 'failure-change';
223+
216224
export type GlobalConfig = {
217225
bail: number;
218226
changedSince: string;
@@ -260,7 +268,7 @@ export type GlobalConfig = {
260268
nonFlagArgs: Array<string>;
261269
noSCM: boolean | null | undefined;
262270
notify: boolean;
263-
notifyMode: string;
271+
notifyMode: NotifyMode;
264272
outputFile: Path | null | undefined;
265273
onlyChanged: boolean;
266274
onlyFailures: boolean;

packages/jest-watcher/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"version": "24.0.0",
55
"main": "build/index.js",
66
"dependencies": {
7+
"@jest/types": "^24.1.0",
8+
"@types/node": "*",
79
"ansi-escapes": "^3.0.0",
810
"chalk": "^2.0.1",
911
"jest-util": "^24.0.0",

packages/jest-watcher/src/BaseWatchPlugin.js

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {WatchPlugin} from './types';
9+
10+
class BaseWatchPlugin implements WatchPlugin {
11+
_stdin: NodeJS.ReadableStream;
12+
_stdout: NodeJS.WritableStream;
13+
14+
constructor({
15+
stdin,
16+
stdout,
17+
}: {
18+
stdin: NodeJS.ReadableStream;
19+
stdout: NodeJS.WritableStream;
20+
}) {
21+
this._stdin = stdin;
22+
this._stdout = stdout;
23+
}
24+
25+
apply() {}
26+
27+
getUsageInfo() {
28+
return null;
29+
}
30+
31+
onKey() {}
32+
33+
run() {
34+
return Promise.resolve();
35+
}
36+
}
37+
38+
export default BaseWatchPlugin;

packages/jest-watcher/src/JestHooks.js renamed to packages/jest-watcher/src/JestHooks.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
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 {
8+
import {
119
JestHookSubscriber,
1210
JestHookEmitter,
1311
FileChange,
1412
ShouldRunTestSuite,
1513
TestRunComplete,
16-
} from 'types/JestHooks';
14+
} from './types';
1715

1816
type AvailableHooks =
1917
| 'onFileChange'
@@ -22,9 +20,9 @@ type AvailableHooks =
2220

2321
class JestHooks {
2422
_listeners: {
25-
onFileChange: Array<FileChange>,
26-
onTestRunComplete: Array<TestRunComplete>,
27-
shouldRunTestSuite: Array<ShouldRunTestSuite>,
23+
onFileChange: Array<FileChange>;
24+
onTestRunComplete: Array<TestRunComplete>;
25+
shouldRunTestSuite: Array<ShouldRunTestSuite>;
2826
};
2927

3028
constructor() {
@@ -61,14 +59,15 @@ class JestHooks {
6159
this._listeners.onTestRunComplete.forEach(listener =>
6260
listener(results),
6361
),
64-
shouldRunTestSuite: async testSuiteInfo =>
65-
Promise.all(
62+
shouldRunTestSuite: async testSuiteInfo => {
63+
const result = await Promise.all(
6664
this._listeners.shouldRunTestSuite.map(listener =>
6765
listener(testSuiteInfo),
6866
),
69-
).then(result =>
70-
result.every(shouldRunTestSuite => shouldRunTestSuite),
71-
),
67+
);
68+
69+
return result.every(shouldRunTestSuite => shouldRunTestSuite);
70+
},
7271
};
7372
}
7473
}

packages/jest-watcher/src/PatternPrompt.js renamed to packages/jest-watcher/src/PatternPrompt.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
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-
'use strict';
11-
12-
import type {ScrollOptions} from 'types/Watch';
13-
148
import chalk from 'chalk';
159
import ansiEscapes from 'ansi-escapes';
10+
// @ts-ignore: Not yet migrated
1611
import {specialChars} from 'jest-util';
1712
import Prompt from './lib/Prompt';
1813

@@ -28,18 +23,20 @@ const usage = (entity: string) =>
2823
const usageRows = usage('').split('\n').length;
2924

3025
export default class PatternPrompt {
31-
_pipe: stream$Writable | tty$WriteStream;
26+
_pipe: NodeJS.WritableStream;
3227
_prompt: Prompt;
3328
_entityName: string;
3429
_currentUsageRows: number;
3530

36-
constructor(pipe: stream$Writable | tty$WriteStream, prompt: Prompt) {
31+
constructor(pipe: NodeJS.WritableStream, prompt: Prompt) {
32+
// TODO: Should come in the constructor
33+
this._entityName = '';
3734
this._pipe = pipe;
3835
this._prompt = prompt;
3936
this._currentUsageRows = usageRows;
4037
}
4138

42-
run(onSuccess: Function, onCancel: Function, options?: {header: string}) {
39+
run(onSuccess: () => void, onCancel: () => void, options?: {header: string}) {
4340
this._pipe.write(ansiEscapes.cursorHide);
4441
this._pipe.write(CLEAR);
4542

@@ -56,7 +53,7 @@ export default class PatternPrompt {
5653
this._prompt.enter(this._onChange.bind(this), onSuccess, onCancel);
5754
}
5855

59-
_onChange(pattern: string, options: ScrollOptions) {
56+
protected _onChange() {
6057
this._pipe.write(ansiEscapes.eraseLine);
6158
this._pipe.write(ansiEscapes.cursorLeft);
6259
}

packages/jest-watcher/src/constants.js renamed to packages/jest-watcher/src/constants.ts

Lines changed: 0 additions & 2 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
const isWindows = process.platform === 'win32';

packages/jest-watcher/src/index.js renamed to packages/jest-watcher/src/index.ts

Lines changed: 0 additions & 2 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
export {default as BaseWatchPlugin} from './BaseWatchPlugin';

packages/jest-watcher/src/lib/Prompt.js renamed to packages/jest-watcher/src/lib/Prompt.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,44 @@
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 {ScrollOptions} from 'types/Watch';
11-
8+
import {ScrollOptions} from '../types';
129
import {KEYS} from '../constants';
1310

1411
export default class Prompt {
1512
_entering: boolean;
1613
_value: string;
17-
_onChange: Function;
18-
_onSuccess: Function;
19-
_onCancel: Function;
14+
_onChange: () => void;
15+
_onSuccess: (value?: string) => void;
16+
_onCancel: (value?: string) => void;
2017
_offset: number;
2118
_promptLength: number;
2219
_selection: string | null;
2320

2421
constructor() {
25-
(this: any)._onResize = this._onResize.bind(this);
22+
// Copied from `enter` to satisfy TS
23+
this._entering = true;
24+
this._value = '';
25+
this._selection = null;
26+
this._offset = -1;
27+
this._promptLength = 0;
28+
29+
this._onChange = () => {};
30+
this._onSuccess = () => {};
31+
this._onCancel = () => {};
32+
33+
this._onResize = this._onResize.bind(this);
2634
}
2735

28-
_onResize() {
29-
this._onChange(this._value);
36+
private _onResize() {
37+
this._onChange();
3038
}
3139

3240
enter(
3341
onChange: (pattern: string, options: ScrollOptions) => void,
34-
onSuccess: Function,
35-
onCancel: Function,
42+
onSuccess: () => void,
43+
onCancel: () => void,
3644
) {
3745
this._entering = true;
3846
this._value = '';

packages/jest-watcher/src/lib/__tests__/formatTestNameByPattern.test.js renamed to packages/jest-watcher/src/lib/__tests__/formatTestNameByPattern.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
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-
'use strict';
11-
128
import formatTestNameByPattern from '../formatTestNameByPattern';
139

1410
describe('for multiline test name returns', () => {

packages/jest-watcher/src/lib/__tests__/prompt.test.js renamed to packages/jest-watcher/src/lib/__tests__/prompt.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
*
77
*/
88

9-
'use strict';
10-
119
import Prompt from '../Prompt';
1210
import {KEYS} from '../../constants';
1311

packages/jest-watcher/src/lib/colorize.js renamed to packages/jest-watcher/src/lib/colorize.ts

Lines changed: 0 additions & 2 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 chalk from 'chalk';

packages/jest-watcher/src/lib/formatTestNameByPattern.js renamed to packages/jest-watcher/src/lib/formatTestNameByPattern.ts

Lines changed: 1 addition & 3 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 chalk from 'chalk';
@@ -31,7 +29,7 @@ export default (testName: string, pattern: string, width: number) => {
3129
return chalk.dim(inlineTestName);
3230
}
3331

34-
const startPatternIndex = Math.max(match.index, 0);
32+
const startPatternIndex = Math.max(match.index || 0, 0);
3533
const endPatternIndex = startPatternIndex + match[0].length;
3634

3735
if (inlineTestName.length <= width) {

packages/jest-watcher/src/lib/patternModeHelpers.js renamed to packages/jest-watcher/src/lib/patternModeHelpers.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
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-
'use strict';
11-
128
import chalk from 'chalk';
139
import ansiEscapes from 'ansi-escapes';
1410
import stringLength from 'string-length';
1511

1612
export const printPatternCaret = (
1713
pattern: string,
18-
pipe: stream$Writable | tty$WriteStream,
14+
pipe: NodeJS.WritableStream,
1915
) => {
2016
const inputText = `${chalk.dim(' pattern \u203A')} ${pattern}`;
2117

@@ -27,7 +23,7 @@ export const printPatternCaret = (
2723
export const printRestoredPatternCaret = (
2824
pattern: string,
2925
currentUsageRows: number,
30-
pipe: stream$Writable | tty$WriteStream,
26+
pipe: NodeJS.WritableStream,
3127
) => {
3228
const inputText = `${chalk.dim(' pattern \u203A')} ${pattern}`;
3329

0 commit comments

Comments
 (0)