'use strict'; const vite = require('./shared/vite-test-utils-edge.21a779c8.cjs'); const node_child_process = require('node:child_process'); const node_url = require('node:url'); const createDebug = require('debug'); const getPortPlease = require('get-port-please'); const ofetch = require('ofetch'); require('node:fs'); require('node:path'); require('picocolors'); require('vite'); require('defu'); require('node:module'); require('node:os'); require('@intlify/shared'); require('esbuild'); const DEBUG = createDebug("vite-test-utils:server"); function getServerEntryPoint() { const devPath = node_url.fileURLToPath(new URL(`./process.${"mjs"}`, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)))); return devPath; } async function startServer() { const ctx = vite.useTestContext(); await stopServer(); const port = ctx.port = await getPortPlease.getRandomPort(); ctx.url = `http://localhost:${port}`; DEBUG(`ctx.url: ${ctx.url}`); const devPath = getServerEntryPoint(); DEBUG("devPath: ", devPath); ctx.server = await node_child_process.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 getPortPlease.waitForPort(port, { retries: 32 }); for (let i = 0; i < 50; i++) { await new Promise((resolve) => setTimeout(resolve, 100)); try { const res = await ofetch.fetch(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 = vite.useTestContext(); if (ctx.server) { await ctx.server.kill(); ctx.server = void 0; ctx.port = void 0; } } function url(path) { const ctx = vite.useTestContext(); if (!ctx.url) { throw new Error("url is not available (is `server` option enabled?)"); } return ctx.url + path; } function fetch(path, options) { return ofetch.fetch(url(path), options); } function $fetch(path, options) { return ofetch.$fetch(url(path), options); } async function createBrowser() { const ctx = vite.useTestContext(); if (!ctx.options.browser) { throw new Error("browser feature is not available (is `browser` option enabled?)"); } const playwright = await vite.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 = vite.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 = vite.createTestContext(options); const beforeEach = async () => { vite.setTestContext(ctx); }; const afterEach = async () => { vite.setTestContext(void 0); }; const afterAll = async () => { if (ctx.server) { vite.setTestContext(ctx); await stopServer(); vite.setTestContext(void 0); } if (ctx.browser) { await ctx.browser.close(); ctx.browser = void 0; } }; const beforeAll = async () => { await vite.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 vite.dynamicImport("vitest"); const hooks = createTest(options); vitest.beforeAll(hooks.beforeAll, 120 * 1e3); vitest.afterAll(hooks.afterAll); vitest.beforeEach(hooks.beforeEach); vitest.afterEach(hooks.afterEach); } exports.$fetch = $fetch; exports.createPage = createPage; exports.fetch = fetch; exports.setup = setup; exports.startServer = startServer; exports.stopServer = stopServer; exports.url = url;