MCPcopy Create free account
hub / github.com/CopilotKit/aimock / useAimock

Function useAimock

src/jest.ts:53–107  ·  view source on GitHub ↗
(options: UseAimockOptions = {})

Source from the content-addressed store, hash-verified

51 * in the test environment. This works with the default jest configuration.
52 */
53export function useAimock(options: UseAimockOptions = {}): () => AimockHandle {
54 let handle: AimockHandle | null = null;
55 let origOpenaiUrl: string | undefined;
56 let origAnthropicUrl: string | undefined;
57
58 beforeAll(async () => {
59 const { fixtures: fixturePath, patchEnv, ...serverOpts } = options;
60 const llm = new LLMock(serverOpts);
61
62 if (fixturePath) {
63 const resolved = resolve(fixturePath);
64 const loadedFixtures = loadFixtures(resolved);
65 for (const f of loadedFixtures) {
66 llm.addFixture(f);
67 }
68 }
69
70 const url = await llm.start();
71
72 if (patchEnv !== false) {
73 origOpenaiUrl = process.env.OPENAI_BASE_URL;
74 origAnthropicUrl = process.env.ANTHROPIC_BASE_URL;
75 process.env.OPENAI_BASE_URL = `${url}/v1`;
76 process.env.ANTHROPIC_BASE_URL = `${url}/v1`;
77 }
78
79 handle = { llm, url };
80 });
81
82 beforeEach(() => {
83 if (handle) {
84 handle.llm.resetMatchCounts();
85 }
86 });
87
88 afterAll(async () => {
89 if (handle) {
90 if (options.patchEnv !== false) {
91 if (origOpenaiUrl !== undefined) process.env.OPENAI_BASE_URL = origOpenaiUrl;
92 else delete process.env.OPENAI_BASE_URL;
93 if (origAnthropicUrl !== undefined) process.env.ANTHROPIC_BASE_URL = origAnthropicUrl;
94 else delete process.env.ANTHROPIC_BASE_URL;
95 }
96 await handle.llm.stop();
97 handle = null;
98 }
99 });
100
101 return () => {
102 if (!handle) {
103 throw new Error("useAimock(): server not started — are you calling this inside a test?");
104 }
105 return handle;
106 };
107}
108
109function loadFixtures(fixturePath: string): Fixture[] {
110 try {

Callers

nothing calls this directly

Calls 6

addFixtureMethod · 0.95
startMethod · 0.95
resolveFunction · 0.85
resetMatchCountsMethod · 0.80
loadFixturesFunction · 0.70
stopMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…