Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
test: re-implement tests for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsukina-7mochi committed Mar 8, 2024
1 parent 4e4114f commit 3790425
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 124 deletions.
32 changes: 0 additions & 32 deletions example/examples.json

This file was deleted.

15 changes: 14 additions & 1 deletion example/lit-esmsh/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 0 additions & 45 deletions example/serve-continuous.ts

This file was deleted.

4 changes: 1 addition & 3 deletions import_map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"imports": {
"assert": "https://deno.land/[email protected]/assert/mod.ts",
"hono": "https://deno.land/x/[email protected]/mod.ts",
"hono/": "https://deno.land/x/[email protected]/"
"assert": "https://deno.land/[email protected]/assert/mod.ts"
}
}
122 changes: 79 additions & 43 deletions test/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

import { assertEquals } from 'assert';
import { path } from '../deps.ts';
import examples from '../example/examples.json' assert { type: 'json' };

const decoder = new TextDecoder();

const executeDenoTask = function(cwd: string, taskName: string, ...args: string[]): Promise<Deno.CommandOutput> {
const process = new Deno.Command(Deno.execPath(), {
args: ['task', taskName, ...args],
cwd,
stdin: "null",
stdout: "piped",
stderr: "piped",
stdin: 'null',
stdout: 'piped',
stderr: 'piped',
}).spawn();

return process.output().then((output) => new Promise<typeof output>((resolve, reject) => {
Expand All @@ -25,47 +26,82 @@ const executeDenoTask = function(cwd: string, taskName: string, ...args: string[
}));
}

Deno.test("examples", async (testContext) => {
const decoder = new TextDecoder();
const buildExample = async function(exampleName: string) {
const examplePath = path.resolve('example', exampleName);

await executeDenoTask(examplePath, 'clean')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Clean process failed with:\n Error ${stdout}\n Output ${stderr}`);
});

await executeDenoTask(examplePath, 'cache')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Clean process failed with:\n Error ${stdout}\n Output ${stderr}`);
});

await executeDenoTask(examplePath, 'build')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Build process failed with:\n Error ${stdout}\n Output ${stderr}`);
});
}

const compareExampleOutput = async function(exampleName: string) {
const examplePath = path.resolve('example', exampleName);

for(const [exampleName, exampleSettings] of Object.entries(examples)) {
const examplePath = path.resolve('example', exampleName);
const runRawOutput = await executeDenoTask(examplePath, 'run:raw')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Execution (raw) process failed with:\n Error ${stdout}\n Output ${stderr}`);
});
const runRawStdout = decoder.decode(runRawOutput.stdout);

const runBundleOutput = await executeDenoTask(examplePath, 'run:bundle')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Execution (bundle) process failed with:\n Error ${stdout}\n Output ${stderr}`);
});
const runBundleStdout = decoder.decode(runBundleOutput.stdout);

assertEquals(runRawStdout, runBundleStdout);
}

const buildTestOf = function(testContext: Deno.TestContext) {
return async function(exampleName: string) {
await testContext.step(exampleName, async () => {
await executeDenoTask(examplePath, 'clean')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Clean process failed with:\n Error ${stdout}\n Output ${stderr}`);
});

await executeDenoTask(examplePath, 'build')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Build process failed with:\n Error ${stdout}\n Output ${stderr}`);
});

if(exampleSettings.type !== 'simple') {
return;
}

const runRawOutput = await executeDenoTask(examplePath, 'run:raw')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Execution (raw) process failed with:\n Error ${stdout}\n Output ${stderr}`);
});
const runRawStdout = decoder.decode(runRawOutput.stdout);

const runBundleOutput = await executeDenoTask(examplePath, 'run:bundle')
.catch((output) => {
const stdout = decoder.decode(output.stdout);
const stderr = decoder.decode(output.stderr);
throw Error(`Execution (bundle) process failed with:\n Error ${stdout}\n Output ${stderr}`);
});
const runBundleStdout = decoder.decode(runBundleOutput.stdout);

assertEquals(runRawStdout, runBundleStdout);
await buildExample(exampleName);
});
}
}

const buildAndCompareTestOf = function(testContext: Deno.TestContext) {
return async function(exampleName: string) {
await testContext.step(exampleName, async () => {
await buildExample(exampleName);
await compareExampleOutput(exampleName);
});
}
}

Deno.test('examples', async (testContext) => {
const buildTest = buildTestOf(testContext);
const buildAndCompareTest = buildAndCompareTestOf(testContext);

await buildAndCompareTest('ignore-modules');
await buildAndCompareTest('import-map');
await buildTest('lit-esmsh');
await buildTest('lit-npm');
await buildAndCompareTest('lodash-jsdelivr');
await buildAndCompareTest('lodash-npm');
await buildTest('preact-esmsh');
await buildTest('preact-npm');
await buildTest('react-esmsh');
await buildTest('react-npm');
});

0 comments on commit 3790425

Please sign in to comment.