index.d.ts 5.2 KB

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