UNPKG

5.33 kBTypeScriptView Raw
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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/// <reference types="node" />
8
9import type {AggregatedResult} from '@jest/test-result';
10import type {Config} from '@jest/types';
11import Emittery = require('emittery');
12
13export declare type AllowedConfigOptions = Partial<
14 Pick<
15 Config.GlobalConfig,
16 | 'bail'
17 | 'changedSince'
18 | 'collectCoverage'
19 | 'collectCoverageFrom'
20 | 'coverageDirectory'
21 | 'coverageReporters'
22 | 'findRelatedTests'
23 | 'nonFlagArgs'
24 | 'notify'
25 | 'notifyMode'
26 | 'onlyFailures'
27 | 'reporters'
28 | 'testNamePattern'
29 | 'testPathPattern'
30 | 'updateSnapshot'
31 | 'verbose'
32 > & {
33 mode: 'watch' | 'watchAll';
34 }
35>;
36
37declare type AvailableHooks =
38 | 'onFileChange'
39 | 'onTestRunComplete'
40 | 'shouldRunTestSuite';
41
42export declare abstract class BaseWatchPlugin implements WatchPlugin {
43 protected _stdin: NodeJS.ReadStream;
44 protected _stdout: NodeJS.WriteStream;
45 constructor({
46 stdin,
47 stdout,
48 }: {
49 stdin: NodeJS.ReadStream;
50 stdout: NodeJS.WriteStream;
51 });
52 apply(_hooks: JestHookSubscriber): void;
53 getUsageInfo(_globalConfig: Config.GlobalConfig): UsageData | null;
54 onKey(_key: string): void;
55 run(
56 _globalConfig: Config.GlobalConfig,
57 _updateConfigAndRun: UpdateConfigCallback,
58 ): Promise<void | boolean>;
59}
60
61declare type FileChange = (fs: JestHookExposedFS) => void;
62
63export declare class JestHook {
64 private readonly _listeners;
65 private readonly _subscriber;
66 private readonly _emitter;
67 constructor();
68 isUsed(hook: AvailableHooks): boolean;
69 getSubscriber(): Readonly<JestHookSubscriber>;
70 getEmitter(): Readonly<JestHookEmitter>;
71}
72
73export declare type JestHookEmitter = {
74 onFileChange: (fs: JestHookExposedFS) => void;
75 onTestRunComplete: (results: AggregatedResult) => void;
76 shouldRunTestSuite: (
77 testSuiteInfo: TestSuiteInfo,
78 ) => Promise<boolean> | boolean;
79};
80
81declare type JestHookExposedFS = {
82 projects: Array<{
83 config: Config.ProjectConfig;
84 testPaths: Array<string>;
85 }>;
86};
87
88export declare type JestHookSubscriber = {
89 onFileChange: (fn: FileChange) => void;
90 onTestRunComplete: (fn: TestRunComplete) => void;
91 shouldRunTestSuite: (fn: ShouldRunTestSuite) => void;
92};
93
94export declare const KEYS: {
95 ARROW_DOWN: string;
96 ARROW_LEFT: string;
97 ARROW_RIGHT: string;
98 ARROW_UP: string;
99 BACKSPACE: string;
100 CONTROL_C: string;
101 CONTROL_D: string;
102 CONTROL_U: string;
103 ENTER: string;
104 ESCAPE: string;
105};
106
107export declare abstract class PatternPrompt {
108 protected _pipe: NodeJS.WritableStream;
109 protected _prompt: Prompt;
110 protected _entityName: string;
111 protected _currentUsageRows: number;
112 constructor(
113 _pipe: NodeJS.WritableStream,
114 _prompt: Prompt,
115 _entityName?: string,
116 );
117 run(
118 onSuccess: (value: string) => void,
119 onCancel: () => void,
120 options?: {
121 header: string;
122 },
123 ): void;
124 protected _onChange(_pattern: string, _options: ScrollOptions_2): void;
125}
126
127export declare function printPatternCaret(
128 pattern: string,
129 pipe: NodeJS.WritableStream,
130): void;
131
132export declare function printRestoredPatternCaret(
133 pattern: string,
134 currentUsageRows: number,
135 pipe: NodeJS.WritableStream,
136): void;
137
138export declare class Prompt {
139 private _entering;
140 private _value;
141 private _onChange;
142 private _onSuccess;
143 private _onCancel;
144 private _offset;
145 private _promptLength;
146 private _selection;
147 constructor();
148 private readonly _onResize;
149 enter(
150 onChange: (pattern: string, options: ScrollOptions_2) => void,
151 onSuccess: (pattern: string) => void,
152 onCancel: () => void,
153 ): void;
154 setPromptLength(length: number): void;
155 setPromptSelection(selected: string): void;
156 put(key: string): void;
157 abort(): void;
158 isEntering(): boolean;
159}
160
161declare type ScrollOptions_2 = {
162 offset: number;
163 max: number;
164};
165export {ScrollOptions_2 as ScrollOptions};
166
167declare type ShouldRunTestSuite = (
168 testSuiteInfo: TestSuiteInfo,
169) => Promise<boolean>;
170
171declare type State = {
172 interrupted: boolean;
173};
174
175declare type TestRunComplete = (results: AggregatedResult) => void;
176
177declare type TestSuiteInfo = {
178 config: Config.ProjectConfig;
179 duration?: number;
180 testPath: string;
181};
182
183export declare class TestWatcher extends Emittery<{
184 change: State;
185}> {
186 state: State;
187 private readonly _isWatchMode;
188 constructor({isWatchMode}: {isWatchMode: boolean});
189 setState(state: State): Promise<void>;
190 isInterrupted(): boolean;
191 isWatchMode(): boolean;
192}
193
194export declare type UpdateConfigCallback = (
195 config?: AllowedConfigOptions,
196) => void;
197
198export declare type UsageData = {
199 key: string;
200 prompt: string;
201};
202
203export declare interface WatchPlugin {
204 isInternal?: boolean;
205 apply?: (hooks: JestHookSubscriber) => void;
206 getUsageInfo?: (globalConfig: Config.GlobalConfig) => UsageData | null;
207 onKey?: (value: string) => void;
208 run?: (
209 globalConfig: Config.GlobalConfig,
210 updateConfigAndRun: UpdateConfigCallback,
211 ) => Promise<void | boolean>;
212}
213
214export declare interface WatchPluginClass {
215 new (options: {
216 config: Record<string, unknown>;
217 stdin: NodeJS.ReadStream;
218 stdout: NodeJS.WriteStream;
219 }): WatchPlugin;
220}
221
222export {};