|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 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 | + |
| 8 | +import {Script} from 'vm'; |
| 9 | +import {Config} from '@jest/types'; |
| 10 | +import moduleMocker from 'jest-mock'; |
| 11 | +import {ScriptTransformer} from '@jest/transform'; |
| 12 | + |
| 13 | +export type EnvironmentContext = { |
| 14 | + console?: Console; |
| 15 | + testPath?: Config.Path; |
| 16 | +}; |
| 17 | + |
| 18 | +// TODO: Get rid of this at some point |
| 19 | +type JasmineType = {_DEFAULT_TIMEOUT_INTERVAL?: number; addMatchers: Function}; |
| 20 | + |
| 21 | +// TODO: type this better: https://nodejs.org/api/modules.html#modules_the_module_wrapper |
| 22 | +type ModuleWrapper = (...args: Array<unknown>) => unknown; |
| 23 | + |
| 24 | +export interface JestEnvironment { |
| 25 | + new ( |
| 26 | + config: Config.ProjectConfig, |
| 27 | + context?: EnvironmentContext, |
| 28 | + ): JestEnvironment; |
| 29 | + runScript( |
| 30 | + script: Script, |
| 31 | + ): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null; |
| 32 | + // TODO: Maybe add `| Window` in the future? |
| 33 | + global: NodeJS.Global & {__coverage__: any; jasmine: JasmineType}; |
| 34 | + // TODO: When `jest-util` is ESM, this can just be `fakeTimers: import('jest-util').FakeTimers` |
| 35 | + fakeTimers: { |
| 36 | + clearAllTimers(): void; |
| 37 | + runAllImmediates(): void; |
| 38 | + runAllTicks(): void; |
| 39 | + runAllTimers(): void; |
| 40 | + advanceTimersByTime(msToRun: number): void; |
| 41 | + runOnlyPendingTimers(): void; |
| 42 | + runWithRealTimers(callback: any): void; |
| 43 | + getTimerCount(): number; |
| 44 | + useFakeTimers(): void; |
| 45 | + useRealTimers(): void; |
| 46 | + }; |
| 47 | + testFilePath: Config.Path; |
| 48 | + moduleMocker: typeof moduleMocker; |
| 49 | + setup(): Promise<void>; |
| 50 | + teardown(): Promise<void>; |
| 51 | +} |
| 52 | + |
| 53 | +export type Module = typeof module; |
| 54 | + |
| 55 | +export type LocalModuleRequire = typeof require & { |
| 56 | + requireActual: (moduleName: string) => unknown; |
| 57 | + requireMock: (moduleName: string) => unknown; |
| 58 | +}; |
| 59 | + |
| 60 | +export type Jest = { |
| 61 | + addMatchers(matchers: Object): void; |
| 62 | + autoMockOff(): Jest; |
| 63 | + autoMockOn(): Jest; |
| 64 | + clearAllMocks(): Jest; |
| 65 | + clearAllTimers(): void; |
| 66 | + deepUnmock(moduleName: string): Jest; |
| 67 | + disableAutomock(): Jest; |
| 68 | + doMock(moduleName: string, moduleFactory?: () => unknown): Jest; |
| 69 | + dontMock(moduleName: string): Jest; |
| 70 | + enableAutomock(): Jest; |
| 71 | + fn: typeof moduleMocker.fn; |
| 72 | + genMockFromModule(moduleName: string): any; |
| 73 | + isMockFunction(fn: Function): boolean; |
| 74 | + mock( |
| 75 | + moduleName: string, |
| 76 | + moduleFactory?: () => unknown, |
| 77 | + options?: {virtual?: boolean}, |
| 78 | + ): Jest; |
| 79 | + requireActual: LocalModuleRequire['requireActual']; |
| 80 | + requireMock: LocalModuleRequire['requireMock']; |
| 81 | + resetAllMocks(): Jest; |
| 82 | + resetModuleRegistry(): Jest; |
| 83 | + resetModules(): Jest; |
| 84 | + restoreAllMocks(): Jest; |
| 85 | + retryTimes(numRetries: number): Jest; |
| 86 | + runAllImmediates(): void; |
| 87 | + runAllTicks(): void; |
| 88 | + runAllTimers(): void; |
| 89 | + runOnlyPendingTimers(): void; |
| 90 | + advanceTimersByTime(msToRun: number): void; |
| 91 | + runTimersToTime(msToRun: number): void; |
| 92 | + getTimerCount(): number; |
| 93 | + setMock(moduleName: string, moduleExports: any): Jest; |
| 94 | + setTimeout(timeout: number): Jest; |
| 95 | + spyOn: typeof moduleMocker.spyOn; |
| 96 | + unmock(moduleName: string): Jest; |
| 97 | + useFakeTimers(): Jest; |
| 98 | + useRealTimers(): Jest; |
| 99 | + isolateModules(fn: () => void): Jest; |
| 100 | +}; |
0 commit comments