index.d.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { EventEmitter } from 'events';
  2. import { ForkOptions, SpawnOptions, ChildProcess } from 'child_process';
  3. import { Readable } from 'stream';
  4. export interface RuleOpt {
  5. ctx: any;
  6. type: string;
  7. expected: ExpectedType | ExpectedType[];
  8. args?: string[];
  9. isOpposite?: boolean;
  10. }
  11. export class Rule {
  12. constructor(opt: RuleOpt);
  13. validate(message?: string): void;
  14. assert(actual: any, expected: any, message?: string | Error): void;
  15. formatMessage(actual: any, expected: any, message?: string | Error): string;
  16. inspectObj(obj: any): string;
  17. }
  18. export interface Result {
  19. stdout: string;
  20. stderr: string;
  21. code: number;
  22. error: Error | null;
  23. proc: ChildProcess | null;
  24. }
  25. export interface CoffeeOpt<T> {
  26. method: string;
  27. cmd: string;
  28. args?: string[];
  29. opt?: T;
  30. }
  31. export type ExpectedType = number | string | RegExp;
  32. export class Coffee<T = any> extends EventEmitter {
  33. method: string;
  34. cmd: string;
  35. args?: string[];
  36. opt?: T;
  37. constructor(opt: CoffeeOpt<T>);
  38. debug(level?: number | boolean): this;
  39. /**
  40. * Assert type with expected value
  41. *
  42. * @param {String} type - assertion rule type, can be `code`,`stdout`,`stderr`,`error`.
  43. * @param {Array} args - spread args, the first item used to be a test value `{Number|String|RegExp|Array} expected`
  44. * @return {Coffee} return self for chain
  45. */
  46. expect(type: string, ...args: Array<ExpectedType | ExpectedType[]>): this;
  47. /**
  48. * Assert type with not expected value, opposite assertion of `expect`.
  49. *
  50. * @param {String} type - assertion rule type, can be `code`,`stdout`,`stderr`,`error`.
  51. * @param {Array} args - spread args, the first item used to be a test value `{Number|String|RegExp|Array} expected`
  52. * @return {Coffee} return self for chain
  53. */
  54. notExpect(type: string, ...args: Array<ExpectedType | ExpectedType[]>): this;
  55. /**
  56. * allow user to custom rule
  57. * @param {String} type - rule type
  58. * @param {Rule} RuleClz - custom rule class
  59. * @protected
  60. */
  61. setRule(type: string, RuleClz: typeof Rule);
  62. /**
  63. * Write data to stdin of the command
  64. * @param {String} input - input text
  65. * @return {Coffee} return self for chain
  66. */
  67. write(input: string): this;
  68. /**
  69. * Write special key sequence to stdin of the command, if key name not found then write origin key.
  70. * @example `.writeKey('2', 'ENTER', '3')`
  71. * @param {...String} args - input key names, will join as one key
  72. * @return {Coffee} return self for chain
  73. */
  74. writeKey(...args: string[]): this;
  75. /**
  76. * whether set as prompt mode
  77. *
  78. * mark as `prompt`, all stdin call by `write` will wait for `prompt` event then output
  79. * @param {Boolean} [enable] - default to true
  80. * @return {Coffee} return self for chain
  81. */
  82. waitForPrompt(enable?: boolean): this;
  83. /**
  84. * get `end` hook
  85. *
  86. * @param {Function} [cb] - callback, recommended to left undefind and use promise
  87. */
  88. end(cb: (e: Error | undefined, result: Result) => any): void;
  89. /**
  90. * get `end` hook
  91. *
  92. * @return {Promise} - end promise
  93. */
  94. end(): Promise<Result>;
  95. /**
  96. * inject script file for mock purpose
  97. *
  98. * @param {String} scriptFile - script file full path
  99. * @return {Coffee} return self for chain
  100. */
  101. beforeScript(scriptFile: string): this;
  102. restore(): this;
  103. }
  104. /**
  105. * fork a child process to test
  106. * @param {String} modulePath - The module to run in the child
  107. * @param {Array} args - List of string arguments
  108. * @param {Object} opt - fork options
  109. * @see https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options
  110. * @return {Coffee} coffee instance
  111. */
  112. export function fork(modulePath: string, args?: string[], opt?: ForkOptions): Coffee<ForkOptions>;
  113. /**
  114. * fork a child process to test
  115. * @param {String} modulePath - The module to run in the child
  116. * @param {Object} opt - fork options
  117. * @see https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options
  118. * @return {Coffee} coffee instance
  119. */
  120. export function fork(modulePath: string, opt?: ForkOptions): Coffee<ForkOptions>;
  121. /**
  122. * spawn a child process to test
  123. * @param {String} cmd - The command to run
  124. * @param {Array} args - List of string arguments
  125. * @param {Object} opt - spawn options
  126. * @return {Coffee} coffee instance
  127. */
  128. export function spawn(modulePath: string, args?: string[], opt?: SpawnOptions): Coffee<SpawnOptions>;
  129. /**
  130. * spawn a child process to test
  131. * @param {String} cmd - The command to run
  132. * @param {Object} opt - spawn options
  133. * @return {Coffee} coffee instance
  134. */
  135. export function spawn(modulePath: string, opt?: SpawnOptions): Coffee<SpawnOptions>;