index.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Application, Context } from 'egg';
  2. import { MockMate } from 'mm';
  3. import { Test } from 'supertest';
  4. interface EggTest extends Test {
  5. unexpectHeader(name: string, b?: Function): EggTest;
  6. expectHeader(name: string, b?: Function): EggTest;
  7. }
  8. type Methods = 'get' | 'post' | 'delete' | 'del' | 'put' | 'head' | 'options' | 'patch' | 'trace' | 'connect';
  9. export interface BaseMockApplication<T, C> extends Application { // tslint:disble-line
  10. ready(): Promise<void>;
  11. close(): Promise<void>;
  12. callback(): any;
  13. /**
  14. * mock Context
  15. */
  16. mockContext(data?: any): C;
  17. /**
  18. * mock cookie session
  19. */
  20. mockSession(data: any): T;
  21. mockCookies(cookies: any): T;
  22. mockHeaders(headers: any): T;
  23. /**
  24. * Mock service
  25. */
  26. mockService(service: string, methodName: string, fn: any): T;
  27. /**
  28. * mock service that return error
  29. */
  30. mockServiceError(service: string, methodName: string, err?: Error): T;
  31. mockHttpclient(mockUrl: string | RegExp, mockMethod: string | string[], mockResult: MockHttpClientResult): Application;
  32. mockHttpclient(mockUrl: string | RegExp, mockResult: MockHttpClientResult): Application;
  33. /**
  34. * mock csrf
  35. */
  36. mockCsrf(): T;
  37. /**
  38. * http request helper
  39. */
  40. httpRequest(): {
  41. [key in Methods]: (url: string) => EggTest;
  42. } & {
  43. [key: string]: (url: string) => EggTest;
  44. };
  45. }
  46. interface ResultObject {
  47. data?: string | object;
  48. status?: number;
  49. headers?: any;
  50. }
  51. type ResultFunction = (url?: string, opts?: any) => ResultObject | string | void;
  52. type MockHttpClientResult = ResultObject | ResultFunction | string;
  53. export interface MockOption {
  54. /**
  55. * The directory of the application
  56. */
  57. baseDir?: string;
  58. /**
  59. * Custom you plugins
  60. */
  61. plugins?: any;
  62. /**
  63. * The directory of the egg framework
  64. */
  65. framework?: string;
  66. /**
  67. * Cache application based on baseDir
  68. */
  69. cache?: boolean;
  70. /**
  71. * Swtich on process coverage, but it'll be slower
  72. */
  73. coverage?: boolean;
  74. /**
  75. * Remove $baseDir/logs
  76. */
  77. clean?: boolean;
  78. }
  79. type EnvType = 'default' | 'test' | 'prod' | 'local' | 'unittest';
  80. type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE';
  81. export interface MockApplication extends BaseMockApplication<Application, Context> { }
  82. export interface EggMock extends MockMate {
  83. /**
  84. * Create a egg mocked application
  85. */
  86. app: (option?: MockOption) => MockApplication;
  87. /**
  88. * Create a mock cluster server, but you can't use API in application, you should test using supertest
  89. */
  90. cluster: (option?: MockOption) => MockApplication;
  91. /**
  92. * mock the serverEnv of Egg
  93. */
  94. env: (env: EnvType) => void;
  95. /**
  96. * mock console level
  97. */
  98. consoleLevel: (level: LogLevel) => void;
  99. /**
  100. * set EGG_HOME path
  101. */
  102. home: (homePath: string) => void;
  103. /**
  104. * restore mock
  105. */
  106. restore: () => any;
  107. }
  108. declare const mm: EggMock;
  109. export default mm;