UNPKG

vite-test-utils

Version:
155 lines (150 loc) 4.32 kB
import { u as useTestContext, d as dynamicImport, c as createTestContext, s as setTestContext, p as prepareFixture } from './shared/vite-test-utils.60d8c08d.mjs'; import { spawn } from 'node:child_process'; import { fileURLToPath } from 'node:url'; import createDebug from 'debug'; import { getRandomPort, waitForPort } from 'get-port-please'; import { fetch as fetch$1, $fetch as $fetch$1 } from 'ofetch'; import 'node:fs'; import 'node:path'; import 'picocolors'; import 'vite'; import 'defu'; import 'node:module'; import 'node:os'; import '@intlify/shared'; import 'esbuild'; const DEBUG = createDebug("vite-test-utils:server"); function getServerEntryPoint() { const devPath = fileURLToPath(new URL(`./process.${"mjs"}`, import.meta.url)); return devPath; } async function startServer() { const ctx = useTestContext(); await stopServer(); const port = ctx.port = await getRandomPort(); ctx.url = `http://localhost:${port}`; DEBUG(`ctx.url: ${ctx.url}`); const devPath = getServerEntryPoint(); DEBUG("devPath: ", devPath); ctx.server = await spawn("jiti", [devPath], { cwd: ctx.options.rootDir, stdio: "inherit", env: { ...process.env, __VTU_PORT: String(port), __VTU_MODE: ctx.options.mode, __VTU_FIXTURE_BUILD_DIR: ctx.buildDir, __VTU_FIXTURE_ROOT: ctx.options.rootDir, __VTU_FIXTURE_CONFIG_FILE: ctx.options.configFile, __VTU_FIXTURE_VITE_CONFIG: ctx.viteConfigInline, __VTU_FIXTURE_VITE_CONFIG_FILE: ctx.options.viteConfigFile, NODE_ENV: ctx.options.mode === "dev" ? "development" : "production" } }); await waitForPort(port, { retries: 32 }); for (let i = 0; i < 50; i++) { await new Promise((resolve) => setTimeout(resolve, 100)); try { const res = await fetch$1(url("/")); DEBUG(`retry ... ${i}`, res.status); if (res.status === 200) { return; } } catch { } } throw new Error("Timeout waiting for dev server!"); } async function stopServer() { const ctx = useTestContext(); if (ctx.server) { await ctx.server.kill(); ctx.server = void 0; ctx.port = void 0; } } function url(path) { const ctx = useTestContext(); if (!ctx.url) { throw new Error("url is not available (is `server` option enabled?)"); } return ctx.url + path; } function fetch(path, options) { return fetch$1(url(path), options); } function $fetch(path, options) { return $fetch$1(url(path), options); } async function createBrowser() { const ctx = useTestContext(); if (!ctx.options.browser) { throw new Error("browser feature is not available (is `browser` option enabled?)"); } const playwright = await dynamicImport("playwright"); const { type, launch } = ctx.options.browserOptions; if (!playwright[type]) { throw new Error(`Invalid browser '${type}'`); } ctx.browser = await playwright[type].launch(launch); } async function getBrowser() { const ctx = useTestContext(); if (!ctx.browser) { await createBrowser(); } return ctx.browser; } async function createPage(path, options) { const browser = await getBrowser(); const page = await browser.newPage(options); if (path) { await page.goto(url(path)); } return page; } function createTest(options = {}) { const ctx = createTestContext(options); const beforeEach = async () => { setTestContext(ctx); }; const afterEach = async () => { setTestContext(void 0); }; const afterAll = async () => { if (ctx.server) { setTestContext(ctx); await stopServer(); setTestContext(void 0); } if (ctx.browser) { await ctx.browser.close(); ctx.browser = void 0; } }; const beforeAll = async () => { await prepareFixture(); if (ctx.options.server) { await startServer(); } if (ctx.options.browser) { await createBrowser(); } }; return { beforeEach, afterEach, afterAll, beforeAll, ctx }; } async function setup(options = {}) { const vitest = await dynamicImport("vitest"); const hooks = createTest(options); vitest.beforeAll(hooks.beforeAll, 120 * 1e3); vitest.afterAll(hooks.afterAll); vitest.beforeEach(hooks.beforeEach); vitest.afterEach(hooks.afterEach); } export { $fetch, createPage, fetch, setup, startServer, stopServer, url };