From 9ee980a578bfddeb18c2b67d11d74904030d97b5 Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Thu, 12 May 2022 14:57:45 -0600 Subject: [PATCH] refactor: cleaning up how URLs are resolved in e2e tests --- packages/astro/e2e/tailwindcss.test.js | 4 ++-- packages/astro/e2e/test-utils.js | 20 -------------------- packages/astro/test/test-utils.js | 6 +++++- 3 files changed, 7 insertions(+), 23 deletions(-) diff --git a/packages/astro/e2e/tailwindcss.test.js b/packages/astro/e2e/tailwindcss.test.js index 8d643e7749ce3..e156a7be7f11a 100644 --- a/packages/astro/e2e/tailwindcss.test.js +++ b/packages/astro/e2e/tailwindcss.test.js @@ -18,8 +18,8 @@ test.afterAll(async ({ astro }) => { await devServer.stop(); }); -test('Tailwind CSS', async ({ page }) => { - await page.goto(`localhost:${devServer.address.port}/`); +test('Tailwind CSS', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); await test.step('body', async () => { const body = page.locator('body'); diff --git a/packages/astro/e2e/test-utils.js b/packages/astro/e2e/test-utils.js index 19a83b8444626..e2552042c9eee 100644 --- a/packages/astro/e2e/test-utils.js +++ b/packages/astro/e2e/test-utils.js @@ -1,25 +1,5 @@ import { loadFixture as baseLoadFixture } from '../test/test-utils.js'; -/** - * Load Astro fixture - * @param {AstroConfig} inlineConfig Astro config partial (note: must specify `root`) - * @returns {Promise} The fixture. Has the following properties: - * .config - Returns the final config. Will be automatically passed to the methods below: - * - * Build - * .build() - Async. Builds into current folder (will erase previous build) - * .readFile(path) - Async. Read a file from the build. - * - * Dev - * .startDevServer() - Async. Starts a dev server at an available port. Be sure to call devServer.stop() before test exit. - * .fetch(url) - Async. Returns a URL from the prevew server (must have called .preview() before) - * - * Preview - * .preview() - Async. Starts a preview server. Note this can’t be running in same fixture as .dev() as they share ports. Also, you must call `server.close()` before test exit - * - * Clean-up - * .clean() - Async. Removes the project’s dist folder. - */ export function loadFixture(inlineConfig) { if (!inlineConfig || !inlineConfig.root) throw new Error("Must provide { root: './fixtures/...' }"); diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index 267e4039f62c3..bc7ff3f0b2ac6 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -25,6 +25,7 @@ polyfill(globalThis, { * * @typedef {Object} Fixture * @property {typeof build} build + * @property {(url: string) => string} resolveUrl * @property {(url: string, opts: any) => Promise} fetch * @property {(path: string) => Promise} readFile * @property {(path: string) => Promise} readdir @@ -93,6 +94,8 @@ export async function loadFixture(inlineConfig) { }, }; + const resolveUrl = (url) => `http://${'127.0.0.1'}:${config.server.port}${url.replace(/^\/?/, '/')}`; + return { build: (opts = {}) => build(config, { mode: 'development', logging, telemetry, ...opts }), startDevServer: async (opts = {}) => { @@ -101,8 +104,9 @@ export async function loadFixture(inlineConfig) { return devResult; }, config, + resolveUrl, fetch: (url, init) => - fetch(`http://${'127.0.0.1'}:${config.server.port}${url.replace(/^\/?/, '/')}`, init), + fetch(resolveUrl(url), init), preview: async (opts = {}) => { const previewServer = await preview(config, { logging, telemetry, ...opts }); return previewServer;