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

Method decideSync

packages/jest-runtime/src/internals/MockState.ts:156–215  ·  view source on GitHub ↗
(
    from: string,
    moduleName: string,
    moduleID: string,
    mode: 'cjs' | 'esm',
  )

Source from the content-addressed store, hash-verified

154 }
155
156 private decideSync(
157 from: string,
158 moduleName: string,
159 moduleID: string,
160 mode: 'cjs' | 'esm',
161 ): boolean {
162 const explicitMap =
163 mode === 'cjs' ? this.explicitCjsMock : this.explicitEsmMock;
164 const explicit = explicitMap.get(moduleID);
165 if (explicit !== undefined) return explicit;
166
167 const key = transitiveCacheKey(from, moduleID);
168 if (
169 !this.shouldAutoMock ||
170 this.resolution.isCoreModule(moduleName) ||
171 this.shouldUnmockTransitiveDepsCache.get(key)
172 ) {
173 return false;
174 }
175
176 const cached = this.shouldMockCache.get(moduleID);
177 if (cached !== undefined) return cached;
178
179 let modulePath: string;
180 try {
181 modulePath =
182 mode === 'cjs'
183 ? this.resolution.resolveCjs(from, moduleName)
184 : this.resolution.resolveEsm(from, moduleName);
185 } catch (error) {
186 const manualMock =
187 mode === 'cjs'
188 ? this.resolution.getCjsMockModule(from, moduleName)
189 : this.resolution.getEsmMockModule(from, moduleName);
190 if (manualMock) {
191 this.shouldMockCache.set(moduleID, true);
192 return true;
193 }
194 throw error;
195 }
196
197 if (this.unmockList?.test(modulePath)) {
198 this.shouldMockCache.set(moduleID, false);
199 return false;
200 }
201
202 const currentModuleID =
203 mode === 'cjs'
204 ? this.resolution.getCjsModuleId(this.virtualCjsMocks, from)
205 : this.resolution.getEsmModuleId(this.virtualEsmMocks, from);
206
207 return this.applyTransitive(
208 moduleID,
209 currentModuleID,
210 modulePath,
211 from,
212 key,
213 explicitMap,

Callers 2

shouldMockCjsMethod · 0.95
shouldMockEsmSyncMethod · 0.95

Calls 12

applyTransitiveMethod · 0.95
transitiveCacheKeyFunction · 0.85
resolveCjsMethod · 0.80
resolveEsmMethod · 0.80
getCjsMockModuleMethod · 0.80
getEsmMockModuleMethod · 0.80
getMethod · 0.45
isCoreModuleMethod · 0.45
setMethod · 0.45
testMethod · 0.45
getCjsModuleIdMethod · 0.45
getEsmModuleIdMethod · 0.45

Tested by

no test coverage detected