UNPKG

8.59 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 */
7import type {IModuleMap} from 'jest-haste-map';
8
9export declare type AsyncResolver = (
10 path: string,
11 options: ResolverOptions,
12) => Promise<string>;
13
14declare function cachedShouldLoadAsEsm(
15 path: string,
16 extensionsToTreatAsEsm: Array<string>,
17): boolean;
18
19declare const defaultResolver: SyncResolver;
20
21export declare type FindNodeModuleConfig = {
22 basedir: string;
23 conditions?: Array<string>;
24 extensions?: Array<string>;
25 moduleDirectory?: Array<string>;
26 paths?: Array<string>;
27 resolver?: string | null;
28 rootDir?: string;
29 throwIfNotFound?: boolean;
30};
31
32export declare type JestResolver = ResolverSyncObject | ResolverAsyncObject;
33
34declare interface JSONObject {
35 [key: string]: JSONValue;
36}
37
38declare type JSONValue =
39 | string
40 | number
41 | boolean
42 | JSONObject
43 | Array<JSONValue>;
44
45declare type ModuleNameMapperConfig = {
46 regex: RegExp;
47 moduleName: string | Array<string>;
48};
49
50declare class ModuleNotFoundError extends Error {
51 code: string;
52 hint?: string;
53 requireStack?: Array<string>;
54 siblingWithSimilarExtensionFound?: boolean;
55 moduleName?: string;
56 private _originalMessage?;
57 constructor(message: string, moduleName?: string);
58 buildMessage(rootDir: string): void;
59 static duckType(error: ModuleNotFoundError): ModuleNotFoundError;
60}
61
62/**
63 * Allows transforming parsed `package.json` contents.
64 *
65 * @param pkg - Parsed `package.json` contents.
66 * @param file - Path to `package.json` file.
67 * @param dir - Directory that contains the `package.json`.
68 *
69 * @returns Transformed `package.json` contents.
70 */
71export declare type PackageFilter = (
72 pkg: PackageJSON,
73 file: string,
74 dir: string,
75) => PackageJSON;
76
77export declare type PackageJSON = JSONObject;
78
79/**
80 * Allows transforming a path within a package.
81 *
82 * @param pkg - Parsed `package.json` contents.
83 * @param path - Path being resolved.
84 * @param relativePath - Path relative from the `package.json` location.
85 *
86 * @returns Relative path that will be joined from the `package.json` location.
87 */
88export declare type PathFilter = (
89 pkg: PackageJSON,
90 path: string,
91 relativePath: string,
92) => string;
93
94export declare type ResolveModuleConfig = {
95 conditions?: Array<string>;
96 skipNodeResolution?: boolean;
97 paths?: Array<string>;
98};
99
100declare class Resolver {
101 private readonly _options;
102 private readonly _moduleMap;
103 private readonly _moduleIDCache;
104 private readonly _moduleNameCache;
105 private readonly _modulePathCache;
106 private readonly _supportsNativePlatform;
107 constructor(moduleMap: IModuleMap, options: ResolverConfig);
108 static ModuleNotFoundError: typeof ModuleNotFoundError;
109 static tryCastModuleNotFoundError(error: unknown): ModuleNotFoundError | null;
110 static clearDefaultResolverCache(): void;
111 static findNodeModule(
112 path: string,
113 options: FindNodeModuleConfig,
114 ): string | null;
115 static findNodeModuleAsync(
116 path: string,
117 options: FindNodeModuleConfig,
118 ): Promise<string | null>;
119 static unstable_shouldLoadAsEsm: typeof cachedShouldLoadAsEsm;
120 resolveModuleFromDirIfExists(
121 dirname: string,
122 moduleName: string,
123 options?: ResolveModuleConfig,
124 ): string | null;
125 resolveModuleFromDirIfExistsAsync(
126 dirname: string,
127 moduleName: string,
128 options?: ResolveModuleConfig,
129 ): Promise<string | null>;
130 resolveModule(
131 from: string,
132 moduleName: string,
133 options?: ResolveModuleConfig,
134 ): string;
135 resolveModuleAsync(
136 from: string,
137 moduleName: string,
138 options?: ResolveModuleConfig,
139 ): Promise<string>;
140 /**
141 * _prepareForResolution is shared between the sync and async module resolution
142 * methods, to try to keep them as DRY as possible.
143 */
144 private _prepareForResolution;
145 /**
146 * _getHasteModulePath attempts to return the path to a haste module.
147 */
148 private _getHasteModulePath;
149 private _throwModNotFoundError;
150 private _getMapModuleName;
151 private _isAliasModule;
152 isCoreModule(moduleName: string): boolean;
153 getModule(name: string): string | null;
154 getModulePath(from: string, moduleName: string): string;
155 getPackage(name: string): string | null;
156 getMockModule(from: string, name: string): string | null;
157 getMockModuleAsync(from: string, name: string): Promise<string | null>;
158 getModulePaths(from: string): Array<string>;
159 getGlobalPaths(moduleName?: string): Array<string>;
160 getModuleID(
161 virtualMocks: Map<string, boolean>,
162 from: string,
163 moduleName?: string,
164 options?: ResolveModuleConfig,
165 ): string;
166 getModuleIDAsync(
167 virtualMocks: Map<string, boolean>,
168 from: string,
169 moduleName?: string,
170 options?: ResolveModuleConfig,
171 ): Promise<string>;
172 private _getModuleType;
173 private _getAbsolutePath;
174 private _getAbsolutePathAsync;
175 private _getMockPath;
176 private _getMockPathAsync;
177 private _getVirtualMockPath;
178 private _getVirtualMockPathAsync;
179 private _isModuleResolved;
180 private _isModuleResolvedAsync;
181 resolveStubModuleName(from: string, moduleName: string): string | null;
182 resolveStubModuleNameAsync(
183 from: string,
184 moduleName: string,
185 ): Promise<string | null>;
186}
187export default Resolver;
188
189declare type ResolverAsyncObject = {
190 sync?: SyncResolver;
191 async: AsyncResolver;
192};
193
194declare type ResolverConfig = {
195 defaultPlatform?: string | null;
196 extensions: Array<string>;
197 hasCoreModules: boolean;
198 moduleDirectories: Array<string>;
199 moduleNameMapper?: Array<ModuleNameMapperConfig> | null;
200 modulePaths?: Array<string>;
201 platforms?: Array<string>;
202 resolver?: string | null;
203 rootDir: string;
204};
205
206export declare type ResolverOptions = {
207 /** Directory to begin resolving from. */
208 basedir: string;
209 /** List of export conditions. */
210 conditions?: Array<string>;
211 /** Instance of default resolver. */
212 defaultResolver: typeof defaultResolver;
213 /** List of file extensions to search in order. */
214 extensions?: Array<string>;
215 /**
216 * List of directory names to be looked up for modules recursively.
217 *
218 * @defaultValue
219 * The default is `['node_modules']`.
220 */
221 moduleDirectory?: Array<string>;
222 /**
223 * List of `require.paths` to use if nothing is found in `node_modules`.
224 *
225 * @defaultValue
226 * The default is `undefined`.
227 */
228 paths?: Array<string>;
229 /** Allows transforming parsed `package.json` contents. */
230 packageFilter?: PackageFilter;
231 /** Allows transforms a path within a package. */
232 pathFilter?: PathFilter;
233 /** Current root directory. */
234 rootDir?: string;
235};
236
237declare type ResolverSyncObject = {
238 sync: SyncResolver;
239 async?: AsyncResolver;
240};
241
242/**
243 * Finds the runner to use:
244 *
245 * 1. looks for jest-runner-<name> relative to project.
246 * 1. looks for jest-runner-<name> relative to Jest.
247 * 1. looks for <name> relative to project.
248 * 1. looks for <name> relative to Jest.
249 */
250export declare const resolveRunner: (
251 resolver: string | undefined | null,
252 {
253 filePath,
254 rootDir,
255 requireResolveFunction,
256 }: {
257 filePath: string;
258 rootDir: string;
259 requireResolveFunction: (moduleName: string) => string;
260 },
261) => string;
262
263export declare const resolveSequencer: (
264 resolver: string | undefined | null,
265 {
266 filePath,
267 rootDir,
268 requireResolveFunction,
269 }: {
270 filePath: string;
271 rootDir: string;
272 requireResolveFunction: (moduleName: string) => string;
273 },
274) => string;
275
276/**
277 * Finds the test environment to use:
278 *
279 * 1. looks for jest-environment-<name> relative to project.
280 * 1. looks for jest-environment-<name> relative to Jest.
281 * 1. looks for <name> relative to project.
282 * 1. looks for <name> relative to Jest.
283 */
284export declare const resolveTestEnvironment: ({
285 rootDir,
286 testEnvironment: filePath,
287 requireResolveFunction,
288}: {
289 rootDir: string;
290 testEnvironment: string;
291 requireResolveFunction: (moduleName: string) => string;
292}) => string;
293
294/**
295 * Finds the watch plugins to use:
296 *
297 * 1. looks for jest-watch-<name> relative to project.
298 * 1. looks for jest-watch-<name> relative to Jest.
299 * 1. looks for <name> relative to project.
300 * 1. looks for <name> relative to Jest.
301 */
302export declare const resolveWatchPlugin: (
303 resolver: string | undefined | null,
304 {
305 filePath,
306 rootDir,
307 requireResolveFunction,
308 }: {
309 filePath: string;
310 rootDir: string;
311 requireResolveFunction: (moduleName: string) => string;
312 },
313) => string;
314
315export declare type SyncResolver = (
316 path: string,
317 options: ResolverOptions,
318) => string;
319
320export {};