From ef410fa30bb24962df61e825c970d411298f3750 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 5 Jun 2023 15:05:16 +0800 Subject: [PATCH] Simplify Deno test (#7276) --- .../integrations/deno/test/basics.test.ts | 106 +++++------------- .../deno/test/dynamic-import.test.ts | 16 +-- packages/integrations/deno/test/helpers.ts | 20 +--- 3 files changed, 37 insertions(+), 105 deletions(-) diff --git a/packages/integrations/deno/test/basics.test.ts b/packages/integrations/deno/test/basics.test.ts index 48a35d785..381343d06 100644 --- a/packages/integrations/deno/test/basics.test.ts +++ b/packages/integrations/deno/test/basics.test.ts @@ -1,10 +1,6 @@ -import { StartServerCallback, runBuildAndStartApp, defaultTestPermissions } from './helpers.ts'; import { DOMParser } from 'https://deno.land/x/deno_dom@v0.1.35-alpha/deno-dom-wasm.ts'; import { assert, assertEquals } from 'https://deno.land/std@0.158.0/testing/asserts.ts'; - -async function startApp(cb: StartServerCallback) { - await runBuildAndStartApp('./fixtures/basics/', cb); -} +import { runBuildAndStartApp, defaultTestPermissions } from './helpers.ts'; // this needs to be here and not in the specific test case, because // the variables are loaded in the global scope of the built server @@ -15,9 +11,13 @@ Deno.env.set('SOME_VARIABLE', varContent); Deno.test({ name: 'Basics', permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(baseUrl); + sanitizeResources: false, + sanitizeOps: false, + async fn(t) { + const app = await runBuildAndStartApp('./fixtures/basics/'); + + await t.step('Works', async () => { + const resp = await fetch(app.url); assertEquals(resp.status, 200); const html = await resp.text(); @@ -28,17 +28,9 @@ Deno.test({ assert(div, 'div exists'); }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'Custom 404', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(new URL('this-does-not-exist', baseUrl)); + await t.step('Custom 404', async () => { + const resp = await fetch(new URL('this-does-not-exist', app.url)); assertEquals(resp.status, 404); const html = await resp.text(); @@ -48,108 +40,60 @@ Deno.test({ const header = doc!.querySelector('#custom-404'); assert(header, 'displays custom 404'); }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'Loads style assets', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - let resp = await fetch(baseUrl); + await t.step('Loads style assets', async () => { + let resp = await fetch(app.url); const html = await resp.text(); const doc = new DOMParser().parseFromString(html, `text/html`); const link = doc!.querySelector('link'); const href = link!.getAttribute('href'); - resp = await fetch(new URL(href!, baseUrl)); + resp = await fetch(new URL(href!, app.url)); assertEquals(resp.status, 200); const ct = resp.headers.get('content-type'); assertEquals(ct, 'text/css; charset=UTF-8'); await resp.body!.cancel(); }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'Correctly loads run-time env variables', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(baseUrl); + await t.step('Correctly loads run-time env variables', async () => { + const resp = await fetch(app.url); const html = await resp.text(); const doc = new DOMParser().parseFromString(html, `text/html`); const p = doc!.querySelector('p#env-value'); assertEquals(p!.innerText, varContent); }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'Works with Markdown', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(new URL('markdown', baseUrl)); + await t.step('Works with Markdown', async () => { + const resp = await fetch(new URL('markdown', app.url)); const html = await resp.text(); const doc = new DOMParser().parseFromString(html, `text/html`); const h1 = doc!.querySelector('h1'); assertEquals(h1!.innerText, 'Heading from Markdown'); }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'Works with MDX', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(new URL('mdx', baseUrl)); + await t.step('Works with MDX', async () => { + const resp = await fetch(new URL('mdx', app.url)); const html = await resp.text(); const doc = new DOMParser().parseFromString(html, `text/html`); const h1 = doc!.querySelector('h1'); assertEquals(h1!.innerText, 'Heading from MDX'); }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'Astro.cookies', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const url = new URL('/admin', baseUrl); + await t.step('Astro.cookies', async () => { + const url = new URL('/admin', app.url); const resp = await fetch(url, { redirect: 'manual' }); assertEquals(resp.status, 302); const headers = resp.headers; assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/'); }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'perendering', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(new URL('/prerender', baseUrl)); + await t.step('perendering', async () => { + const resp = await fetch(new URL('/prerender', app.url)); assertEquals(resp.status, 200); const html = await resp.text(); @@ -159,7 +103,7 @@ Deno.test({ const h1 = doc!.querySelector('h1'); assertEquals(h1!.innerText, 'test'); }); + + app.stop(); }, - sanitizeResources: false, - sanitizeOps: false, }); diff --git a/packages/integrations/deno/test/dynamic-import.test.ts b/packages/integrations/deno/test/dynamic-import.test.ts index 1562a5771..987aac9ff 100644 --- a/packages/integrations/deno/test/dynamic-import.test.ts +++ b/packages/integrations/deno/test/dynamic-import.test.ts @@ -1,16 +1,14 @@ import { DOMParser } from 'https://deno.land/x/deno_dom@v0.1.35-alpha/deno-dom-wasm.ts'; import { assert, assertEquals } from 'https://deno.land/std@0.158.0/testing/asserts.ts'; -import { StartServerCallback, runBuildAndStartAppFromSubprocess } from './helpers.ts'; - -async function startApp(cb: StartServerCallback) { - await runBuildAndStartAppFromSubprocess('./fixtures/dynimport/', cb); -} +import { runBuildAndStartAppFromSubprocess } from './helpers.ts'; Deno.test({ name: 'Dynamic import', - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(baseUrl); + async fn(t) { + const app = await runBuildAndStartAppFromSubprocess('./fixtures/dynimport/'); + + await t.step('Works', async () => { + const resp = await fetch(app.url); assertEquals(resp.status, 200); const html = await resp.text(); assert(html); @@ -18,5 +16,7 @@ Deno.test({ const div = doc!.querySelector('#thing'); assert(div, 'div exists'); }); + + app.stop(); }, }); diff --git a/packages/integrations/deno/test/helpers.ts b/packages/integrations/deno/test/helpers.ts index e69abbebb..ac451d963 100644 --- a/packages/integrations/deno/test/helpers.ts +++ b/packages/integrations/deno/test/helpers.ts @@ -12,7 +12,6 @@ export const defaultTestPermissions: Deno.PermissionOptions = { env: true, }; -export declare type StartServerCallback = (url: URL) => Promise; declare type ExitCallback = () => void; export async function runBuild(fixturePath: string) { @@ -59,31 +58,20 @@ export async function startModFromSubprocess(baseUrl: URL): Promise proc.close(); } -export async function runBuildAndStartApp(fixturePath: string, cb: StartServerCallback) { +export async function runBuildAndStartApp(fixturePath: string) { const url = new URL(fixturePath, dir); await runBuild(fixturePath); const stop = await startModFromImport(url); - try { - await cb(defaultURL); - } finally { - stop(); - } + return { url: defaultURL, stop }; } -export async function runBuildAndStartAppFromSubprocess( - fixturePath: string, - cb: StartServerCallback -) { +export async function runBuildAndStartAppFromSubprocess(fixturePath: string) { const url = new URL(fixturePath, dir); await runBuild(fixturePath); const stop = await startModFromSubprocess(url); - try { - await cb(defaultURL); - } finally { - stop(); - } + return { url: defaultURL, stop }; }