|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>Test Framework Plugins — aimock</title> |
| 7 | + <link rel="icon" type="image/svg+xml" href="../favicon.svg" /> |
| 8 | + <link rel="preconnect" href="https://fonts.googleapis.com" /> |
| 9 | + <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> |
| 10 | + <link |
| 11 | + href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,300;0,400;0,500;0,600;0,700;1,400&family=Instrument+Sans:wght@400;500;600;700&display=swap" |
| 12 | + rel="stylesheet" |
| 13 | + /> |
| 14 | + <link rel="stylesheet" href="../style.css" /> |
| 15 | + </head> |
| 16 | + <body> |
| 17 | + <nav class="top-nav"> |
| 18 | + <div class="nav-inner"> |
| 19 | + <div style="display: flex; align-items: center; gap: 1rem"> |
| 20 | + <button |
| 21 | + class="sidebar-toggle" |
| 22 | + onclick="document.querySelector('.sidebar').classList.toggle('open')" |
| 23 | + aria-label="Toggle sidebar" |
| 24 | + > |
| 25 | + ☰ |
| 26 | + </button> |
| 27 | + <a href="/" class="nav-brand"> <span class="prompt">$</span> aimock </a> |
| 28 | + </div> |
| 29 | + <ul class="nav-links"> |
| 30 | + <li><a href="/">Home</a></li> |
| 31 | + <li><a href="/docs" style="color: var(--accent)">Docs</a></li> |
| 32 | + <li> |
| 33 | + <a href="https://github.com/CopilotKit/aimock" class="gh-link" target="_blank" |
| 34 | + ><svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"> |
| 35 | + <path |
| 36 | + d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z" |
| 37 | + /> |
| 38 | + </svg> |
| 39 | + GitHub</a |
| 40 | + > |
| 41 | + </li> |
| 42 | + </ul> |
| 43 | + </div> |
| 44 | + </nav> |
| 45 | + |
| 46 | + <div class="docs-layout"> |
| 47 | + <aside class="sidebar" id="sidebar"></aside> |
| 48 | + |
| 49 | + <main class="docs-content"> |
| 50 | + <h1>Test Framework Plugins</h1> |
| 51 | + <p class="lead"> |
| 52 | + Zero-config integration for vitest and jest. Import <code>useAimock</code>, write tests |
| 53 | + — the server lifecycle, env vars, and cleanup are handled automatically. |
| 54 | + </p> |
| 55 | + |
| 56 | + <h2>Vitest</h2> |
| 57 | + <div class="code-block"> |
| 58 | + <div class="code-block-header">test/app.test.ts <span class="lang-tag">ts</span></div> |
| 59 | + <pre><code><span class="kw">import</span> { useAimock } <span class="kw">from</span> <span class="str">"@copilotkit/aimock/vitest"</span>; |
| 60 | + |
| 61 | +<span class="kw">const</span> mock = <span class="fn">useAimock</span>({ <span class="prop">fixtures</span>: <span class="str">"./fixtures"</span> }); |
| 62 | + |
| 63 | +<span class="fn">it</span>(<span class="str">"responds to hello"</span>, <span class="kw">async</span> () => { |
| 64 | + <span class="cmt">// OPENAI_BASE_URL is already set</span> |
| 65 | + <span class="kw">const</span> res = <span class="kw">await</span> myApp.<span class="fn">chat</span>(<span class="str">"hello"</span>); |
| 66 | + <span class="fn">expect</span>(res).<span class="fn">toBe</span>(<span class="str">"Hi there!"</span>); |
| 67 | +});</code></pre> |
| 68 | + </div> |
| 69 | + |
| 70 | + <h2>Jest</h2> |
| 71 | + <div class="code-block"> |
| 72 | + <div class="code-block-header">test/app.test.ts <span class="lang-tag">ts</span></div> |
| 73 | + <pre><code><span class="kw">import</span> { useAimock } <span class="kw">from</span> <span class="str">"@copilotkit/aimock/jest"</span>; |
| 74 | + |
| 75 | +<span class="kw">const</span> mock = <span class="fn">useAimock</span>({ <span class="prop">fixtures</span>: <span class="str">"./fixtures"</span> }); |
| 76 | + |
| 77 | +<span class="fn">it</span>(<span class="str">"responds to hello"</span>, <span class="kw">async</span> () => { |
| 78 | + <span class="kw">const</span> res = <span class="kw">await</span> myApp.<span class="fn">chat</span>(<span class="str">"hello"</span>); |
| 79 | + <span class="fn">expect</span>(res).<span class="fn">toBe</span>(<span class="str">"Hi there!"</span>); |
| 80 | +});</code></pre> |
| 81 | + </div> |
| 82 | + |
| 83 | + <h2>Options</h2> |
| 84 | + <p>Both plugins accept the same <code>UseAimockOptions</code> object:</p> |
| 85 | + <table class="endpoint-table"> |
| 86 | + <thead> |
| 87 | + <tr> |
| 88 | + <th>Option</th> |
| 89 | + <th>Type</th> |
| 90 | + <th>Default</th> |
| 91 | + <th>Description</th> |
| 92 | + </tr> |
| 93 | + </thead> |
| 94 | + <tbody> |
| 95 | + <tr> |
| 96 | + <td><code>fixtures</code></td> |
| 97 | + <td><code>string</code></td> |
| 98 | + <td>—</td> |
| 99 | + <td>Path to fixture file or directory</td> |
| 100 | + </tr> |
| 101 | + <tr> |
| 102 | + <td><code>patchEnv</code></td> |
| 103 | + <td><code>boolean</code></td> |
| 104 | + <td><code>true</code></td> |
| 105 | + <td>Auto-set <code>OPENAI_BASE_URL</code> and <code>ANTHROPIC_BASE_URL</code></td> |
| 106 | + </tr> |
| 107 | + <tr> |
| 108 | + <td><code>port</code></td> |
| 109 | + <td><code>number</code></td> |
| 110 | + <td><code>0</code> (random)</td> |
| 111 | + <td>Port to listen on</td> |
| 112 | + </tr> |
| 113 | + <tr> |
| 114 | + <td><code>host</code></td> |
| 115 | + <td><code>string</code></td> |
| 116 | + <td><code>127.0.0.1</code></td> |
| 117 | + <td>Host to bind to</td> |
| 118 | + </tr> |
| 119 | + <tr> |
| 120 | + <td><code>logLevel</code></td> |
| 121 | + <td><code>string</code></td> |
| 122 | + <td><code>silent</code></td> |
| 123 | + <td>Log verbosity</td> |
| 124 | + </tr> |
| 125 | + </tbody> |
| 126 | + </table> |
| 127 | + |
| 128 | + <h2>The Handle</h2> |
| 129 | + <p> |
| 130 | + The getter function returned by <code>useAimock()</code> returns an |
| 131 | + <code>AimockHandle</code>: |
| 132 | + </p> |
| 133 | + <table class="endpoint-table"> |
| 134 | + <thead> |
| 135 | + <tr> |
| 136 | + <th>Property</th> |
| 137 | + <th>Type</th> |
| 138 | + <th>Description</th> |
| 139 | + </tr> |
| 140 | + </thead> |
| 141 | + <tbody> |
| 142 | + <tr> |
| 143 | + <td><code>llm</code></td> |
| 144 | + <td><code>LLMock</code></td> |
| 145 | + <td>The LLMock instance — add fixtures programmatically</td> |
| 146 | + </tr> |
| 147 | + <tr> |
| 148 | + <td><code>url</code></td> |
| 149 | + <td><code>string</code></td> |
| 150 | + <td>The server URL (e.g., <code>http://127.0.0.1:4010</code>)</td> |
| 151 | + </tr> |
| 152 | + </tbody> |
| 153 | + </table> |
| 154 | + |
| 155 | + <h3>Programmatic fixture registration</h3> |
| 156 | + <div class="code-block"> |
| 157 | + <div class="code-block-header">test/custom.test.ts <span class="lang-tag">ts</span></div> |
| 158 | + <pre><code><span class="kw">const</span> mock = <span class="fn">useAimock</span>(); |
| 159 | + |
| 160 | +<span class="fn">it</span>(<span class="str">"custom fixture"</span>, <span class="kw">async</span> () => { |
| 161 | + mock().<span class="prop">llm</span>.<span class="fn">onMessage</span>(<span class="str">"custom"</span>, { <span class="prop">content</span>: <span class="str">"Custom response"</span> }); |
| 162 | + <span class="cmt">// ...</span> |
| 163 | +});</code></pre> |
| 164 | + </div> |
| 165 | + |
| 166 | + <h2>Lifecycle</h2> |
| 167 | + <p>Both plugins register framework hooks to manage the server automatically:</p> |
| 168 | + <ol> |
| 169 | + <li> |
| 170 | + <strong><code>beforeAll</code></strong> — starts the server, loads fixtures from |
| 171 | + the <code>fixtures</code> path, and patches <code>OPENAI_BASE_URL</code> / |
| 172 | + <code>ANTHROPIC_BASE_URL</code> environment variables |
| 173 | + </li> |
| 174 | + <li> |
| 175 | + <strong><code>beforeEach</code></strong> — resets match counts (sequential fixture |
| 176 | + counters return to zero, but fixtures themselves are preserved) |
| 177 | + </li> |
| 178 | + <li> |
| 179 | + <strong><code>afterAll</code></strong> — stops the server and restores the |
| 180 | + original environment variables |
| 181 | + </li> |
| 182 | + </ol> |
| 183 | + |
| 184 | + <h2>Without Plugins (Manual)</h2> |
| 185 | + <p> |
| 186 | + For comparison, here is the equivalent manual setup. The plugins above handle all of this |
| 187 | + for you: |
| 188 | + </p> |
| 189 | + <div class="code-block"> |
| 190 | + <div class="code-block-header">test/manual.test.ts <span class="lang-tag">ts</span></div> |
| 191 | + <pre><code><span class="kw">import</span> { LLMock } <span class="kw">from</span> <span class="str">"@copilotkit/aimock"</span>; |
| 192 | + |
| 193 | +<span class="kw">let</span> mock: LLMock; |
| 194 | + |
| 195 | +<span class="fn">beforeAll</span>(<span class="kw">async</span> () => { |
| 196 | + mock = <span class="kw">new</span> <span class="fn">LLMock</span>(); |
| 197 | + mock.<span class="fn">onMessage</span>(<span class="str">"hello"</span>, { <span class="prop">content</span>: <span class="str">"Hi!"</span> }); |
| 198 | + <span class="kw">await</span> mock.<span class="fn">start</span>(); |
| 199 | + process.env.OPENAI_BASE_URL = <span class="str">`${mock.url}/v1`</span>; |
| 200 | +}); |
| 201 | + |
| 202 | +<span class="fn">afterAll</span>(<span class="kw">async</span> () => { |
| 203 | + <span class="kw">await</span> mock.<span class="fn">stop</span>(); |
| 204 | + <span class="kw">delete</span> process.env.OPENAI_BASE_URL; |
| 205 | +});</code></pre> |
| 206 | + </div> |
| 207 | + </main> |
| 208 | + <aside class="page-toc" id="page-toc"></aside> |
| 209 | + </div> |
| 210 | + <footer class="docs-footer"> |
| 211 | + <div class="footer-inner"> |
| 212 | + <div class="footer-left"><span>$</span> aimock · MIT License</div> |
| 213 | + <ul class="footer-links"> |
| 214 | + <li><a href="https://github.com/CopilotKit/aimock" target="_blank">GitHub</a></li> |
| 215 | + <li> |
| 216 | + <a href="https://www.npmjs.com/package/@copilotkit/aimock" target="_blank">npm</a> |
| 217 | + </li> |
| 218 | + </ul> |
| 219 | + </div> |
| 220 | + </footer> |
| 221 | + <script src="../sidebar.js"></script> |
| 222 | + <script src="../cli-tabs.js"></script> |
| 223 | + </body> |
| 224 | +</html> |
0 commit comments