Skip to content

Commit 842f389

Browse files
authored
chore: migrate jest-watcher to TypeScript (#7843)
1 parent f57e288 commit 842f389

26 files changed

+185
-133
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
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))
2424
- `[jest-util]`: Migrate to TypeScript ([#7844](https://github.com/facebook/jest/pull/7844))
25+
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
2526

2627
### Performance
2728

docs/WatchPlugins.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class MyWatchPlugin {
155155
For stability and safety reasons, only part of the global configuration keys can be updated with `updateConfigAndRun`. The current white list is as follows:
156156

157157
- [`bail`](configuration.html#bail-number-boolean)
158+
- [`changedSince`](cli.html#changedsince)
158159
- [`collectCoverage`](configuration.html#collectcoverage-boolean)
159160
- [`collectCoverageFrom`](configuration.html#collectcoveragefrom-array)
160161
- [`collectCoverageOnlyFrom`](configuration.html#collectcoverageonlyfrom-array)

packages/jest-cli/src/lib/update_global_config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ export type Options = {
2323
coverageDirectory?: $PropertyType<GlobalConfig, 'coverageDirectory'>,
2424
coverageReporters?: $PropertyType<GlobalConfig, 'coverageReporters'>,
2525
mode?: 'watch' | 'watchAll',
26-
noSCM?: $PropertyType<GlobalConfig, 'noSCM'>,
2726
notify?: $PropertyType<GlobalConfig, 'notify'>,
2827
notifyMode?: $PropertyType<GlobalConfig, 'notifyMode'>,
2928
onlyFailures?: $PropertyType<GlobalConfig, 'onlyFailures'>,
30-
passWithNoTests?: $PropertyType<GlobalConfig, 'passWithNoTests'>,
3129
reporters?: $PropertyType<GlobalConfig, 'reporters'>,
3230
testNamePattern?: $PropertyType<GlobalConfig, 'testNamePattern'>,
3331
testPathPattern?: $PropertyType<GlobalConfig, 'testPathPattern'>,

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-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(Boolean);
70+
},
7271
};
7372
}
7473
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +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-
12-
import type {ScrollOptions} from 'types/Watch';
13-
148
import chalk from 'chalk';
159
import ansiEscapes from 'ansi-escapes';
1610
import {specialChars} from 'jest-util';
@@ -28,18 +22,20 @@ const usage = (entity: string) =>
2822
const usageRows = usage('').split('\n').length;
2923

3024
export default class PatternPrompt {
31-
_pipe: stream$Writable | tty$WriteStream;
25+
_pipe: NodeJS.WritableStream;
3226
_prompt: Prompt;
3327
_entityName: string;
3428
_currentUsageRows: number;
3529

36-
constructor(pipe: stream$Writable | tty$WriteStream, prompt: Prompt) {
30+
constructor(pipe: NodeJS.WritableStream, prompt: Prompt) {
31+
// TODO: Should come in the constructor
32+
this._entityName = '';
3733
this._pipe = pipe;
3834
this._prompt = prompt;
3935
this._currentUsageRows = usageRows;
4036
}
4137

42-
run(onSuccess: Function, onCancel: Function, options?: {header: string}) {
38+
run(onSuccess: () => void, onCancel: () => void, options?: {header: string}) {
4339
this._pipe.write(ansiEscapes.cursorHide);
4440
this._pipe.write(CLEAR);
4541

@@ -56,7 +52,7 @@ export default class PatternPrompt {
5652
this._prompt.enter(this._onChange.bind(this), onSuccess, onCancel);
5753
}
5854

59-
_onChange(pattern: string, options: ScrollOptions) {
55+
protected _onChange() {
6056
this._pipe.write(ansiEscapes.eraseLine);
6157
this._pipe.write(ansiEscapes.cursorLeft);
6258
}

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) {

0 commit comments

Comments
 (0)