|
1 | 1 | import { describe, it, expect, vi, afterEach } from 'vitest' |
2 | | -import { execFile } from 'node:child_process' |
| 2 | +import { execFile, execFileSync } from 'node:child_process' |
3 | 3 | import { CliError } from '../../utils/output' |
4 | | -import { buildRunnerBody, parseRunnerOutput } from './runtime' |
| 4 | +import { buildRunnerBody, parseRunnerOutput, runInRuntime } from './runtime' |
5 | 5 |
|
6 | 6 | const MARKER = '__TEST_MARKER__' |
7 | 7 |
|
@@ -66,6 +66,38 @@ describe('buildRunnerBody', () => { |
66 | 66 | }) |
67 | 67 | }) |
68 | 68 |
|
| 69 | +// Run only where the runtime is installed. CI does not have them. |
| 70 | +const hasRuntime = (bin: string): boolean => { |
| 71 | + try { |
| 72 | + execFileSync('which', [bin], { stdio: 'ignore' }) |
| 73 | + return true |
| 74 | + } catch { |
| 75 | + return false |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +for (const runtime of ['bun', 'deno'] as const) { |
| 80 | + describe.runIf(hasRuntime(runtime))(`runInRuntime on ${runtime}`, () => { |
| 81 | + it('should run the app and return the response', async () => { |
| 82 | + const result = await runInRuntime( |
| 83 | + runtime, |
| 84 | + { |
| 85 | + code: [ |
| 86 | + 'const app = { fetch: () => new Response(JSON.stringify({ ua: navigator.userAgent })) }', |
| 87 | + 'export default app', |
| 88 | + ].join('\n'), |
| 89 | + }, |
| 90 | + [], |
| 91 | + { path: '/', method: 'GET', headers: {} } |
| 92 | + ) |
| 93 | + |
| 94 | + expect(result.status).toBe(200) |
| 95 | + const body = JSON.parse(Buffer.from(result.bodyBase64, 'base64').toString('utf-8')) |
| 96 | + expect(body.ua).toContain(runtime === 'bun' ? 'Bun/' : 'Deno/') |
| 97 | + }) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
69 | 101 | describe('parseRunnerOutput', () => { |
70 | 102 | afterEach(() => { |
71 | 103 | vi.restoreAllMocks() |
|
0 commit comments