Simplify Deno test (#7276)
This commit is contained in:
parent
c895981386
commit
ef410fa30b
3 changed files with 37 additions and 105 deletions
|
@ -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,
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -12,7 +12,6 @@ export const defaultTestPermissions: Deno.PermissionOptions = {
|
|||
env: true,
|
||||
};
|
||||
|
||||
export declare type StartServerCallback = (url: URL) => Promise<void>;
|
||||
declare type ExitCallback = () => void;
|
||||
|
||||
export async function runBuild(fixturePath: string) {
|
||||
|
@ -59,31 +58,20 @@ export async function startModFromSubprocess(baseUrl: URL): Promise<ExitCallback
|
|||
return () => 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 };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue