UNPKG

12.5 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 {AssertionError} from 'assert';
10import type {AsymmetricMatchers} from '@jest/expect';
11import type {Circus} from '@jest/types';
12import type {Config} from '@jest/types';
13import type {FailedAssertion} from '@jest/test-result';
14import type {JestEnvironment} from '@jest/environment';
15import type Runtime from 'jest-runtime';
16import type {Status} from '@jest/test-result';
17import type {TestResult} from '@jest/test-result';
18
19declare interface AssertionErrorWithStack extends AssertionError {
20 stack: string;
21}
22
23declare type Attributes = {
24 id: string;
25 resultCallback: (result: Spec['result']) => void;
26 description: Circus.TestNameLike;
27 throwOnExpectationFailure: unknown;
28 getTestPath: () => string;
29 queueableFn: QueueableFn;
30 beforeAndAfterFns: () => {
31 befores: Array<QueueableFn>;
32 afters: Array<QueueableFn>;
33 };
34 userContext: () => unknown;
35 onStart: (context: Spec) => void;
36 getSpecName: (spec: Spec) => string;
37 queueRunnerFactory: typeof queueRunner;
38};
39
40declare type Attributes_2 = {
41 id: string;
42 parentSuite?: Suite;
43 description: Circus.TestNameLike;
44 throwOnExpectationFailure?: boolean;
45 getTestPath: () => string;
46};
47
48declare class CallTracker {
49 track: (context: Context) => void;
50 any: () => boolean;
51 count: () => number;
52 argsFor: (index: number) => Array<unknown>;
53 all: () => Array<Context>;
54 allArgs: () => Array<unknown>;
55 first: () => Context;
56 mostRecent: () => Context;
57 reset: () => void;
58 constructor();
59}
60
61/**
62 * Copyright (c) Meta Platforms, Inc. and affiliates.
63 *
64 * This source code is licensed under the MIT license found in the
65 * LICENSE file in the root directory of this source tree.
66 *
67 */
68declare type Context = {
69 object: unknown;
70 args: Array<unknown>;
71 returnValue?: unknown;
72};
73
74declare function createSpy(name: string, originalFn: Fn): Spy;
75
76declare interface DoneFn {
77 (error?: any): void;
78 fail: (error: Error) => void;
79}
80
81declare class ExpectationFailed extends Error {}
82
83declare function expectationResultFactory(
84 options: Options,
85 initError?: Error,
86): FailedAssertion;
87
88declare interface Fn extends Record<string, unknown> {
89 (): unknown;
90}
91
92export declare type Jasmine = {
93 _DEFAULT_TIMEOUT_INTERVAL: number;
94 DEFAULT_TIMEOUT_INTERVAL: number;
95 currentEnv_: ReturnType<typeof jasmineEnv>['prototype'];
96 getEnv: () => ReturnType<typeof jasmineEnv>['prototype'];
97 createSpy: typeof createSpy;
98 Env: ReturnType<typeof jasmineEnv>;
99 JsApiReporter: typeof JsApiReporter;
100 ReportDispatcher: typeof ReportDispatcher;
101 Spec: typeof Spec;
102 SpyRegistry: typeof SpyRegistry;
103 Suite: typeof Suite;
104 Timer: typeof Timer;
105 version: string;
106 testPath: string;
107 addMatchers: (matchers: JasmineMatchersObject) => void;
108} & AsymmetricMatchers & {
109 process: NodeJS.Process;
110 };
111
112declare function jasmine2(
113 globalConfig: Config.GlobalConfig,
114 config: Config.ProjectConfig,
115 environment: JestEnvironment,
116 runtime: Runtime,
117 testPath: string,
118): Promise<TestResult>;
119export default jasmine2;
120
121declare function jasmineEnv(j$: Jasmine): {
122 new (): {
123 specFilter: (spec: Spec) => boolean;
124 catchExceptions: (value: unknown) => boolean;
125 throwOnExpectationFailure: (value: unknown) => void;
126 catchingExceptions: () => boolean;
127 topSuite: () => Suite;
128 fail: (error: Error | AssertionErrorWithStack) => void;
129 pending: (message: string) => void;
130 afterAll: (afterAllFunction: QueueableFn['fn'], timeout?: number) => void;
131 fit: (
132 description: Circus.TestNameLike,
133 fn: QueueableFn['fn'],
134 timeout?: number,
135 ) => Spec;
136 throwingExpectationFailures: () => boolean;
137 randomizeTests: (value: unknown) => void;
138 randomTests: () => boolean;
139 seed: (value: unknown) => unknown;
140 execute: (
141 runnablesToRun?: Array<string>,
142 suiteTree?: Suite,
143 ) => Promise<void>;
144 fdescribe: (
145 description: Circus.TestNameLike,
146 specDefinitions: SpecDefinitionsFn,
147 ) => Suite;
148 spyOn: (
149 obj: Record<string, Spy>,
150 methodName: string,
151 accessType?: keyof PropertyDescriptor,
152 ) => Spy;
153 beforeEach: (
154 beforeEachFunction: QueueableFn['fn'],
155 timeout?: number,
156 ) => void;
157 afterEach: (afterEachFunction: QueueableFn['fn'], timeout?: number) => void;
158 clearReporters: () => void;
159 addReporter: (reporterToAdd: Reporter) => void;
160 it: (
161 description: Circus.TestNameLike,
162 fn: QueueableFn['fn'],
163 timeout?: number,
164 ) => Spec;
165 xdescribe: (
166 description: Circus.TestNameLike,
167 specDefinitions: SpecDefinitionsFn,
168 ) => Suite;
169 xit: (
170 description: Circus.TestNameLike,
171 fn: QueueableFn['fn'],
172 timeout?: number,
173 ) => Spec;
174 beforeAll: (beforeAllFunction: QueueableFn['fn'], timeout?: number) => void;
175 todo: () => Spec;
176 provideFallbackReporter: (reporterToAdd: Reporter) => void;
177 allowRespy: (allow: boolean) => void;
178 describe: (
179 description: Circus.TestNameLike,
180 specDefinitions: SpecDefinitionsFn,
181 ) => Suite;
182 };
183};
184
185declare type JasmineMatcher = {
186 (matchersUtil: unknown, context: unknown): JasmineMatcher;
187 compare(...args: Array<unknown>): unknown;
188 negativeCompare(...args: Array<unknown>): unknown;
189};
190
191declare type JasmineMatchersObject = {
192 [id: string]: JasmineMatcher;
193};
194
195declare class JsApiReporter implements Reporter {
196 started: boolean;
197 finished: boolean;
198 runDetails: RunDetails;
199 jasmineStarted: (runDetails: RunDetails) => void;
200 jasmineDone: (runDetails: RunDetails) => void;
201 status: () => unknown;
202 executionTime: () => unknown;
203 suiteStarted: (result: SuiteResult) => void;
204 suiteDone: (result: SuiteResult) => void;
205 suiteResults: (index: number, length: number) => Array<SuiteResult>;
206 suites: () => Record<string, SuiteResult>;
207 specResults: (index: number, length: number) => Array<SpecResult>;
208 specDone: (result: SpecResult) => void;
209 specs: () => Array<SpecResult>;
210 specStarted: (spec: SpecResult) => void;
211 constructor(options: {timer?: Timer});
212}
213
214declare type Options = {
215 matcherName: string;
216 passed: boolean;
217 actual?: any;
218 error?: any;
219 expected?: any;
220 message?: string | null;
221};
222
223declare type Options_2 = {
224 clearTimeout: (typeof globalThis)['clearTimeout'];
225 fail: (error: Error) => void;
226 onException: (error: Error) => void;
227 queueableFns: Array<QueueableFn>;
228 setTimeout: (typeof globalThis)['setTimeout'];
229 userContext: unknown;
230};
231
232declare type PromiseCallback =
233 | (() => void | PromiseLike<void>)
234 | undefined
235 | null;
236
237declare type QueueableFn = {
238 fn: (done: DoneFn) => void;
239 timeout?: () => number;
240 initError?: Error;
241};
242
243declare function queueRunner(options: Options_2): PromiseLike<void> & {
244 cancel: () => void;
245 catch: (onRejected?: PromiseCallback) => Promise<void>;
246};
247
248declare class ReportDispatcher implements Reporter {
249 addReporter: (reporter: Reporter) => void;
250 provideFallbackReporter: (reporter: Reporter) => void;
251 clearReporters: () => void;
252 jasmineDone: (runDetails: RunDetails) => void;
253 jasmineStarted: (runDetails: RunDetails) => void;
254 specDone: (result: SpecResult) => void;
255 specStarted: (spec: SpecResult) => void;
256 suiteDone: (result: SuiteResult) => void;
257 suiteStarted: (result: SuiteResult) => void;
258 constructor(methods: Array<keyof Reporter>);
259}
260
261declare type Reporter = {
262 jasmineDone: (runDetails: RunDetails) => void;
263 jasmineStarted: (runDetails: RunDetails) => void;
264 specDone: (result: SpecResult) => void;
265 specStarted: (spec: SpecResult) => void;
266 suiteDone: (result: SuiteResult) => void;
267 suiteStarted: (result: SuiteResult) => void;
268};
269
270declare type RunDetails = {
271 totalSpecsDefined?: number;
272 failedExpectations?: SuiteResult['failedExpectations'];
273};
274
275declare class Spec {
276 id: string;
277 description: string;
278 resultCallback: (result: SpecResult) => void;
279 queueableFn: QueueableFn;
280 beforeAndAfterFns: () => {
281 befores: Array<QueueableFn>;
282 afters: Array<QueueableFn>;
283 };
284 userContext: () => unknown;
285 onStart: (spec: Spec) => void;
286 getSpecName: (spec: Spec) => string;
287 queueRunnerFactory: typeof queueRunner;
288 throwOnExpectationFailure: boolean;
289 initError: Error;
290 result: SpecResult;
291 disabled?: boolean;
292 currentRun?: ReturnType<typeof queueRunner>;
293 markedTodo?: boolean;
294 markedPending?: boolean;
295 expand?: boolean;
296 static pendingSpecExceptionMessage: string;
297 static isPendingSpecException(e: Error): boolean;
298 constructor(attrs: Attributes);
299 addExpectationResult(passed: boolean, data: Options, isError?: boolean): void;
300 execute(onComplete?: () => void, enabled?: boolean): void;
301 cancel(): void;
302 onException(error: ExpectationFailed | AssertionErrorWithStack): void;
303 disable(): void;
304 pend(message?: string): void;
305 todo(): void;
306 getResult(): SpecResult;
307 status(
308 enabled?: boolean,
309 ): 'todo' | 'passed' | 'failed' | 'pending' | 'disabled';
310 isExecutable(): boolean;
311 getFullName(): string;
312 isAssertionError(error: Error): boolean;
313}
314
315declare type SpecDefinitionsFn = () => void;
316
317declare type SpecResult = {
318 id: string;
319 description: string;
320 fullName: string;
321 duration?: number;
322 failedExpectations: Array<FailedAssertion>;
323 testPath: string;
324 passedExpectations: Array<ReturnType<typeof expectationResultFactory>>;
325 pendingReason: string;
326 status: Status;
327 __callsite?: {
328 getColumnNumber: () => number;
329 getLineNumber: () => number;
330 };
331};
332
333declare interface Spy extends Record<string, any> {
334 (this: Record<string, unknown>, ...args: Array<any>): unknown;
335 and: SpyStrategy;
336 calls: CallTracker;
337 restoreObjectToOriginalState?: () => void;
338}
339
340declare class SpyRegistry {
341 allowRespy: (allow: unknown) => void;
342 spyOn: (
343 obj: Record<string, Spy>,
344 methodName: string,
345 accessType?: keyof PropertyDescriptor,
346 ) => Spy;
347 clearSpies: () => void;
348 respy: unknown;
349 private readonly _spyOnProperty;
350 constructor({currentSpies}?: {currentSpies?: () => Array<Spy>});
351}
352
353/**
354 * Copyright (c) Meta Platforms, Inc. and affiliates.
355 *
356 * This source code is licensed under the MIT license found in the
357 * LICENSE file in the root directory of this source tree.
358 *
359 */
360declare class SpyStrategy {
361 identity: () => string;
362 exec: (...args: Array<any>) => unknown;
363 callThrough: () => unknown;
364 returnValue: (value: unknown) => unknown;
365 returnValues: () => unknown;
366 throwError: (something: string | Error) => unknown;
367 callFake: (fn: Function) => unknown;
368 stub: (fn: Function) => unknown;
369 constructor({
370 name,
371 fn,
372 getSpy,
373 }?: {
374 name?: string;
375 fn?: Function;
376 getSpy?: () => unknown;
377 });
378}
379
380declare class Suite {
381 id: string;
382 parentSuite?: Suite;
383 description: Circus.TestNameLike;
384 throwOnExpectationFailure: boolean;
385 beforeFns: Array<QueueableFn>;
386 afterFns: Array<QueueableFn>;
387 beforeAllFns: Array<QueueableFn>;
388 afterAllFns: Array<QueueableFn>;
389 disabled: boolean;
390 children: Array<Suite | Spec>;
391 result: SuiteResult;
392 sharedContext?: object;
393 markedPending: boolean;
394 markedTodo: boolean;
395 isFocused: boolean;
396 constructor(attrs: Attributes_2);
397 getFullName(): string;
398 disable(): void;
399 pend(_message?: string): void;
400 beforeEach(fn: QueueableFn): void;
401 beforeAll(fn: QueueableFn): void;
402 afterEach(fn: QueueableFn): void;
403 afterAll(fn: QueueableFn): void;
404 addChild(child: Suite | Spec): void;
405 status(): 'failed' | 'pending' | 'disabled' | 'finished';
406 isExecutable(): boolean;
407 canBeReentered(): boolean;
408 getResult(): SuiteResult;
409 sharedUserContext(): object;
410 clonedSharedUserContext(): object;
411 onException(...args: Parameters<Spec['onException']>): void;
412 addExpectationResult(...args: Parameters<Spec['addExpectationResult']>): void;
413 execute(..._args: Array<any>): void;
414}
415
416declare type SuiteResult = {
417 id: string;
418 description: string;
419 fullName: string;
420 failedExpectations: Array<ReturnType<typeof expectationResultFactory>>;
421 testPath: string;
422 status?: string;
423};
424
425/**
426 * Copyright (c) Meta Platforms, Inc. and affiliates.
427 *
428 * This source code is licensed under the MIT license found in the
429 * LICENSE file in the root directory of this source tree.
430 *
431 */
432declare class Timer {
433 start: () => void;
434 elapsed: () => number;
435 constructor(options?: {now?: () => number});
436}
437
438export {};