Skip to content

Commit

Permalink
refactor: cleaning up how URLs are resolved in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Sullivan committed May 12, 2022
1 parent 346565a commit 1cccce8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/astro/e2e/tailwindcss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
20 changes: 0 additions & 20 deletions packages/astro/e2e/test-utils.js
Original file line number Diff line number Diff line change
@@ -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<Fixture>} 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/...' }");
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ polyfill(globalThis, {
*
* @typedef {Object} Fixture
* @property {typeof build} build
* @property {(url: string) => string} resolveUrl
* @property {(url: string, opts: any) => Promise<Response>} fetch
* @property {(path: string) => Promise<string>} readFile
* @property {(path: string) => Promise<string[]>} readdir
Expand Down Expand Up @@ -93,6 +94,8 @@ export async function loadFixture(inlineConfig) {
},
};

const resolveUrl = (url) => `https://${'127.0.0.1'}:${config.server.port}${url.replace(/^\/?/, '/')}`;

return {
build: (opts = {}) => build(config, { mode: 'development', logging, telemetry, ...opts }),
startDevServer: async (opts = {}) => {
Expand All @@ -101,8 +104,9 @@ export async function loadFixture(inlineConfig) {
return devResult;
},
config,
resolveUrl,
fetch: (url, init) =>
fetch(`https://${'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;
Expand Down

0 comments on commit 1cccce8

Please sign in to comment.