MCPcopy
hub / github.com/CodebuffAI/codebuff / mockModule

Function mockModule

common/src/testing/mock-modules.ts:16–48  ·  view source on GitHub ↗
(
  modulePath: string,
  renderMocks: () => Record<string, any>,
)

Source from the content-addressed store, hash-verified

14 * @returns an object
15 */
16export async function mockModule(
17 modulePath: string,
18 renderMocks: () => Record<string, any>,
19): Promise<MockResult> {
20 let original = originalModuleCache[modulePath]
21 if (!original) {
22 const moduleExports = await import(modulePath)
23 original = {
24 ...moduleExports,
25 }
26 originalModuleCache[modulePath] = original
27 }
28 let mocks = renderMocks()
29 let result = {
30 ...original,
31 ...mocks,
32 }
33 mock.module(modulePath, () => result)
34 let num = 0
35 let key = modulePath
36 while (key in mockModuleCache) {
37 num++
38 key = `${modulePath}\n${num}`
39 }
40 const mocked: MockResult = {
41 clear: () => {
42 mock.module(modulePath, () => original)
43 delete mockModuleCache[key]
44 },
45 }
46 mockModuleCache[key] = mocked
47 return mocked
48}
49
50export const clearMockedModules = () => {
51 Object.values(mockModuleCache).forEach((mockResult) => mockResult.clear())

Calls

no outgoing calls

Tested by 3

importModuleFunction · 0.72
setupMocksFunction · 0.72
setupMocksFunction · 0.72