Simplify Deno test (#7276)

This commit is contained in:
Bjorn Lu 2023-06-05 15:05:16 +08:00 committed by GitHub
parent c895981386
commit ef410fa30b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 105 deletions

View file

@ -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 { 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 { assert, assertEquals } from 'https://deno.land/std@0.158.0/testing/asserts.ts';
import { runBuildAndStartApp, defaultTestPermissions } from './helpers.ts';
async function startApp(cb: StartServerCallback) {
await runBuildAndStartApp('./fixtures/basics/', cb);
}
// this needs to be here and not in the specific test case, because // 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 // the variables are loaded in the global scope of the built server
@ -15,9 +11,13 @@ Deno.env.set('SOME_VARIABLE', varContent);
Deno.test({ Deno.test({
name: 'Basics', name: 'Basics',
permissions: defaultTestPermissions, permissions: defaultTestPermissions,
async fn() { sanitizeResources: false,
await startApp(async (baseUrl: URL) => { sanitizeOps: false,
const resp = await fetch(baseUrl); async fn(t) {
const app = await runBuildAndStartApp('./fixtures/basics/');
await t.step('Works', async () => {
const resp = await fetch(app.url);
assertEquals(resp.status, 200); assertEquals(resp.status, 200);
const html = await resp.text(); const html = await resp.text();
@ -28,17 +28,9 @@ Deno.test({
assert(div, 'div exists'); assert(div, 'div exists');
}); });
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({ await t.step('Custom 404', async () => {
name: 'Custom 404', const resp = await fetch(new URL('this-does-not-exist', app.url));
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const resp = await fetch(new URL('this-does-not-exist', baseUrl));
assertEquals(resp.status, 404); assertEquals(resp.status, 404);
const html = await resp.text(); const html = await resp.text();
@ -48,108 +40,60 @@ Deno.test({
const header = doc!.querySelector('#custom-404'); const header = doc!.querySelector('#custom-404');
assert(header, 'displays custom 404'); assert(header, 'displays custom 404');
}); });
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({ await t.step('Loads style assets', async () => {
name: 'Loads style assets', let resp = await fetch(app.url);
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
let resp = await fetch(baseUrl);
const html = await resp.text(); const html = await resp.text();
const doc = new DOMParser().parseFromString(html, `text/html`); const doc = new DOMParser().parseFromString(html, `text/html`);
const link = doc!.querySelector('link'); const link = doc!.querySelector('link');
const href = link!.getAttribute('href'); const href = link!.getAttribute('href');
resp = await fetch(new URL(href!, baseUrl)); resp = await fetch(new URL(href!, app.url));
assertEquals(resp.status, 200); assertEquals(resp.status, 200);
const ct = resp.headers.get('content-type'); const ct = resp.headers.get('content-type');
assertEquals(ct, 'text/css; charset=UTF-8'); assertEquals(ct, 'text/css; charset=UTF-8');
await resp.body!.cancel(); await resp.body!.cancel();
}); });
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({ await t.step('Correctly loads run-time env variables', async () => {
name: 'Correctly loads run-time env variables', const resp = await fetch(app.url);
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const resp = await fetch(baseUrl);
const html = await resp.text(); const html = await resp.text();
const doc = new DOMParser().parseFromString(html, `text/html`); const doc = new DOMParser().parseFromString(html, `text/html`);
const p = doc!.querySelector('p#env-value'); const p = doc!.querySelector('p#env-value');
assertEquals(p!.innerText, varContent); assertEquals(p!.innerText, varContent);
}); });
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({ await t.step('Works with Markdown', async () => {
name: 'Works with Markdown', const resp = await fetch(new URL('markdown', app.url));
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const resp = await fetch(new URL('markdown', baseUrl));
const html = await resp.text(); const html = await resp.text();
const doc = new DOMParser().parseFromString(html, `text/html`); const doc = new DOMParser().parseFromString(html, `text/html`);
const h1 = doc!.querySelector('h1'); const h1 = doc!.querySelector('h1');
assertEquals(h1!.innerText, 'Heading from Markdown'); assertEquals(h1!.innerText, 'Heading from Markdown');
}); });
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({ await t.step('Works with MDX', async () => {
name: 'Works with MDX', const resp = await fetch(new URL('mdx', app.url));
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const resp = await fetch(new URL('mdx', baseUrl));
const html = await resp.text(); const html = await resp.text();
const doc = new DOMParser().parseFromString(html, `text/html`); const doc = new DOMParser().parseFromString(html, `text/html`);
const h1 = doc!.querySelector('h1'); const h1 = doc!.querySelector('h1');
assertEquals(h1!.innerText, 'Heading from MDX'); assertEquals(h1!.innerText, 'Heading from MDX');
}); });
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({ await t.step('Astro.cookies', async () => {
name: 'Astro.cookies', const url = new URL('/admin', app.url);
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const url = new URL('/admin', baseUrl);
const resp = await fetch(url, { redirect: 'manual' }); const resp = await fetch(url, { redirect: 'manual' });
assertEquals(resp.status, 302); assertEquals(resp.status, 302);
const headers = resp.headers; const headers = resp.headers;
assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/'); assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/');
}); });
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({ await t.step('perendering', async () => {
name: 'perendering', const resp = await fetch(new URL('/prerender', app.url));
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const resp = await fetch(new URL('/prerender', baseUrl));
assertEquals(resp.status, 200); assertEquals(resp.status, 200);
const html = await resp.text(); const html = await resp.text();
@ -159,7 +103,7 @@ Deno.test({
const h1 = doc!.querySelector('h1'); const h1 = doc!.querySelector('h1');
assertEquals(h1!.innerText, 'test'); assertEquals(h1!.innerText, 'test');
}); });
app.stop();
}, },
sanitizeResources: false,
sanitizeOps: false,
}); });

View file

@ -1,16 +1,14 @@
import { DOMParser } from 'https://deno.land/x/deno_dom@v0.1.35-alpha/deno-dom-wasm.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'; import { assert, assertEquals } from 'https://deno.land/std@0.158.0/testing/asserts.ts';
import { StartServerCallback, runBuildAndStartAppFromSubprocess } from './helpers.ts'; import { runBuildAndStartAppFromSubprocess } from './helpers.ts';
async function startApp(cb: StartServerCallback) {
await runBuildAndStartAppFromSubprocess('./fixtures/dynimport/', cb);
}
Deno.test({ Deno.test({
name: 'Dynamic import', name: 'Dynamic import',
async fn() { async fn(t) {
await startApp(async (baseUrl: URL) => { const app = await runBuildAndStartAppFromSubprocess('./fixtures/dynimport/');
const resp = await fetch(baseUrl);
await t.step('Works', async () => {
const resp = await fetch(app.url);
assertEquals(resp.status, 200); assertEquals(resp.status, 200);
const html = await resp.text(); const html = await resp.text();
assert(html); assert(html);
@ -18,5 +16,7 @@ Deno.test({
const div = doc!.querySelector('#thing'); const div = doc!.querySelector('#thing');
assert(div, 'div exists'); assert(div, 'div exists');
}); });
app.stop();
}, },
}); });

View file

@ -12,7 +12,6 @@ export const defaultTestPermissions: Deno.PermissionOptions = {
env: true, env: true,
}; };
export declare type StartServerCallback = (url: URL) => Promise<void>;
declare type ExitCallback = () => void; declare type ExitCallback = () => void;
export async function runBuild(fixturePath: string) { export async function runBuild(fixturePath: string) {
@ -59,31 +58,20 @@ export async function startModFromSubprocess(baseUrl: URL): Promise<ExitCallback
return () => proc.close(); return () => proc.close();
} }
export async function runBuildAndStartApp(fixturePath: string, cb: StartServerCallback) { export async function runBuildAndStartApp(fixturePath: string) {
const url = new URL(fixturePath, dir); const url = new URL(fixturePath, dir);
await runBuild(fixturePath); await runBuild(fixturePath);
const stop = await startModFromImport(url); const stop = await startModFromImport(url);
try { return { url: defaultURL, stop };
await cb(defaultURL);
} finally {
stop();
}
} }
export async function runBuildAndStartAppFromSubprocess( export async function runBuildAndStartAppFromSubprocess(fixturePath: string) {
fixturePath: string,
cb: StartServerCallback
) {
const url = new URL(fixturePath, dir); const url = new URL(fixturePath, dir);
await runBuild(fixturePath); await runBuild(fixturePath);
const stop = await startModFromSubprocess(url); const stop = await startModFromSubprocess(url);
try { return { url: defaultURL, stop };
await cb(defaultURL);
} finally {
stop();
}
} }