MCPcopy
hub / github.com/jestjs/jest / resolve

Method resolve

packages/jest-runtime/src/internals/cjsRequire.ts:112–170  ·  view source on GitHub ↗
(
    from: string,
    moduleName: string | undefined,
    options: ResolveOptions = {},
  )

Source from the content-addressed store, hash-verified

110 }
111
112 private resolve(
113 from: string,
114 moduleName: string | undefined,
115 options: ResolveOptions = {},
116 ): string {
117 if (moduleName == null) {
118 throw new Error(
119 'The first argument to require.resolve must be a string. Received null or undefined.',
120 );
121 }
122
123 if (path.isAbsolute(moduleName)) {
124 const module = this.resolution.resolveCjsFromDirIfExists(
125 moduleName,
126 moduleName,
127 [],
128 );
129 if (module) {
130 return module;
131 }
132 } else if (options.paths) {
133 const module = this.resolution.resolveCjsStub(from, moduleName);
134
135 if (module) {
136 return module;
137 }
138
139 for (const searchPath of options.paths) {
140 const absolutePath = path.resolve(from, '..', searchPath);
141
142 // required to also resolve files without leading './' directly in the path
143 const module = this.resolution.resolveCjsFromDirIfExists(
144 absolutePath,
145 moduleName,
146 [absolutePath],
147 );
148
149 if (module) {
150 return module;
151 }
152 }
153
154 throw new Resolver.ModuleNotFoundError(
155 `Cannot resolve module '${moduleName}' from paths ['${options.paths.join(
156 "', '",
157 )}'] from ${from}`,
158 );
159 }
160
161 try {
162 return this.resolution.resolveCjs(from, moduleName);
163 } catch (error) {
164 const module = this.resolution.getCjsMockModule(from, moduleName);
165 if (module) {
166 return module;
167 }
168 throw error;
169 }

Callers 15

resolveImplMethod · 0.95
getPackagesFunction · 0.45
jest.config.mjsFile · 0.45
utils.tsFile · 0.45
doneFunction · 0.45
testUtils.tsFile · 0.45
jestAdapter.tsFile · 0.45
asyncError.test.tsFile · 0.45
requireResolveFunction · 0.45
setupPresetFunction · 0.45
setupBabelJestFunction · 0.45

Calls 4

resolveCjsStubMethod · 0.80
resolveCjsMethod · 0.80
getCjsMockModuleMethod · 0.80

Tested by 10

runMethod · 0.36
filterFunctionFunction · 0.36
resolveFromFixtureFunction · 0.36
defineTestsFunction · 0.36
testPathFunction · 0.36
createTransformerFunction · 0.36
defineTestsFunction · 0.36
resolveFixtureFunction · 0.36
proxyReadInitialOptionsFunction · 0.36