UNPKG

5.78 kBTypeScriptView Raw
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 */
7import type { JestEnvironment } from '@jest/environment';
8import type * as JestGlobals from '@jest/globals';
9import type { SourceMapRegistry } from '@jest/source-map';
10import type { V8CoverageResult } from '@jest/test-result';
11import { CallerTransformOptions, ScriptTransformer, ShouldInstrumentOptions, shouldInstrument } from '@jest/transform';
12import type { Config, Global } from '@jest/types';
13import type { IModuleMap } from 'jest-haste-map';
14import HasteMap from 'jest-haste-map';
15import Resolver from 'jest-resolve';
16import type { Context } from './types';
17export type { Context } from './types';
18interface JestGlobals extends Global.TestFrameworkGlobals {
19 expect: typeof JestGlobals.expect;
20}
21declare type HasteMapOptions = {
22 console?: Console;
23 maxWorkers: number;
24 resetCache: boolean;
25 watch?: boolean;
26 watchman: boolean;
27};
28interface InternalModuleOptions extends Required<CallerTransformOptions> {
29 isInternalModule: boolean;
30}
31export default class Runtime {
32 private readonly _cacheFS;
33 private readonly _config;
34 private readonly _coverageOptions;
35 private _currentlyExecutingModulePath;
36 private readonly _environment;
37 private readonly _explicitShouldMock;
38 private readonly _explicitShouldMockModule;
39 private _fakeTimersImplementation;
40 private readonly _internalModuleRegistry;
41 private _isCurrentlyExecutingManualMock;
42 private _mainModule;
43 private readonly _mockFactories;
44 private readonly _mockMetaDataCache;
45 private _mockRegistry;
46 private _isolatedMockRegistry;
47 private _moduleMockRegistry;
48 private readonly _moduleMockFactories;
49 private readonly _moduleMocker;
50 private _isolatedModuleRegistry;
51 private _moduleRegistry;
52 private readonly _esmoduleRegistry;
53 private readonly _cjsNamedExports;
54 private readonly _esmModuleLinkingMap;
55 private readonly _testPath;
56 private readonly _resolver;
57 private _shouldAutoMock;
58 private readonly _shouldMockModuleCache;
59 private readonly _shouldUnmockTransitiveDependenciesCache;
60 private readonly _sourceMapRegistry;
61 private readonly _scriptTransformer;
62 private readonly _fileTransforms;
63 private readonly _fileTransformsMutex;
64 private _v8CoverageInstrumenter;
65 private _v8CoverageResult;
66 private readonly _transitiveShouldMock;
67 private _unmockList;
68 private readonly _virtualMocks;
69 private readonly _virtualModuleMocks;
70 private _moduleImplementation?;
71 private readonly jestObjectCaches;
72 private jestGlobals?;
73 private readonly esmConditions;
74 private readonly cjsConditions;
75 private isTornDown;
76 constructor(config: Config.ProjectConfig, environment: JestEnvironment, resolver: Resolver, transformer: ScriptTransformer, cacheFS: Map<string, string>, coverageOptions: ShouldInstrumentOptions, testPath: Config.Path);
77 static shouldInstrument: typeof shouldInstrument;
78 static createContext(config: Config.ProjectConfig, options: {
79 console?: Console;
80 maxWorkers: number;
81 watch?: boolean;
82 watchman: boolean;
83 }): Promise<Context>;
84 static createHasteMap(config: Config.ProjectConfig, options?: HasteMapOptions): HasteMap;
85 static createResolver(config: Config.ProjectConfig, moduleMap: IModuleMap): Resolver;
86 static runCLI(): Promise<never>;
87 static getCLIOptions(): never;
88 unstable_shouldLoadAsEsm(path: Config.Path): boolean;
89 private loadEsmModule;
90 private resolveModule;
91 private linkAndEvaluateModule;
92 unstable_importModule(from: Config.Path, moduleName?: string): Promise<void>;
93 private loadCjsAsEsm;
94 private importMock;
95 private getExportsOfCjs;
96 requireModule<T = unknown>(from: Config.Path, moduleName?: string, options?: InternalModuleOptions, isRequireActual?: boolean): T;
97 requireInternalModule<T = unknown>(from: Config.Path, to?: string): T;
98 requireActual<T = unknown>(from: Config.Path, moduleName: string): T;
99 requireMock<T = unknown>(from: Config.Path, moduleName: string): T;
100 private _loadModule;
101 private _getFullTransformationOptions;
102 requireModuleOrMock<T = unknown>(from: Config.Path, moduleName: string): T;
103 isolateModules(fn: () => void): void;
104 resetModules(): void;
105 collectV8Coverage(): Promise<void>;
106 stopCollectingV8Coverage(): Promise<void>;
107 getAllCoverageInfoCopy(): JestEnvironment['global']['__coverage__'];
108 getAllV8CoverageInfoCopy(): V8CoverageResult;
109 getSourceMaps(): SourceMapRegistry;
110 setMock(from: string, moduleName: string, mockFactory: () => unknown, options?: {
111 virtual?: boolean;
112 }): void;
113 private setModuleMock;
114 restoreAllMocks(): void;
115 resetAllMocks(): void;
116 clearAllMocks(): void;
117 teardown(): void;
118 private _resolveModule;
119 private _requireResolve;
120 private _requireResolvePaths;
121 private _execModule;
122 private transformFile;
123 private transformFileAsync;
124 private createScriptFromCode;
125 private _requireCoreModule;
126 private _importCoreModule;
127 private _getMockedNativeModule;
128 private _generateMock;
129 private _shouldMock;
130 private _createRequireImplementation;
131 private _createJestObjectFor;
132 private _logFormattedReferenceError;
133 private wrapCodeInModuleWrapper;
134 private constructModuleWrapperStart;
135 private constructInjectedModuleParameters;
136 private handleExecutionError;
137 private getGlobalsForCjs;
138 private getGlobalsForEsm;
139 private getGlobalsFromEnvironment;
140 private readFile;
141 setGlobalsForRuntime(globals: JestGlobals): void;
142}