fix: allow setting server config from fresh config (#1889)

This commit is contained in:
Marvin Hagemeister
2023-10-09 13:59:30 +02:00
committed by GitHub
parent 4fac2a22a6
commit 32eb61c95d
11 changed files with 266 additions and 6 deletions
+15
View File
@@ -0,0 +1,15 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
const TEST_CONFIG_SERVER = Deno.env.get("TEST_CONFIG_SERVER") === "true";
const onListen = (params: { hostname: string; port: number }) => {
console.log("it works");
console.log(`http://localhost:${params.port}`);
};
const onListen2 = (params: { hostname: string; port: number }) => {
console.log("it works #2");
console.log(`http://localhost:${params.port}`);
};
await dev(import.meta.url, "./main.ts", {
server: {
onListen: TEST_CONFIG_SERVER ? onListen2 : undefined,
},
onListen: TEST_CONFIG_SERVER ? undefined : onListen,
});
+15
View File
@@ -0,0 +1,15 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {},
baseUrl: import.meta.url,
};
export default manifest;
+25
View File
@@ -0,0 +1,25 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
const TEST_CONFIG_SERVER = Deno.env.get("TEST_CONFIG_SERVER") === "true";
const onListen = (params: { hostname: string; port: number }) => {
console.log("it works");
console.log(`http://localhost:${params.port}`);
};
const onListen2 = (params: { hostname: string; port: number }) => {
console.log("it works #2");
console.log(`http://localhost:${params.port}`);
};
await start(routes, {
server: {
onListen: TEST_CONFIG_SERVER ? onListen2 : undefined,
},
onListen: TEST_CONFIG_SERVER ? undefined : onListen,
});
+3
View File
@@ -0,0 +1,3 @@
export default function Home() {
return <h1>hello</h1>;
}
+95
View File
@@ -5,6 +5,7 @@ import {
assertMatch,
assertStringIncludes,
delay,
join,
puppeteer,
retry,
} from "./deps.ts";
@@ -971,3 +972,97 @@ Deno.test("De-duplicates <Head /> nodes by key", async () => {
assert(/<meta property="og:title" content="Other title"\/>/.test(html));
});
});
Deno.test("pass options in config", async (t) => {
const fixture = join(Deno.cwd(), "tests", "fixture_config");
await t.step("config.onListen", async () => {
const { lines, serverProcess, output } = await startFreshServer({
args: [
"run",
"-A",
join(fixture, "main.ts"),
],
});
try {
assert(output.find((line) => line === "it works"));
} finally {
serverProcess.kill("SIGTERM");
await serverProcess.status;
// Drain the lines stream
for await (const _ of lines) { /* noop */ }
}
});
await t.step("config.server.onListen", async () => {
const { lines, serverProcess, output } = await startFreshServer({
args: [
"run",
"-A",
join(fixture, "main.ts"),
],
env: {
TEST_CONFIG_SERVER: "true",
},
});
try {
assert(output.find((line) => line === "it works #2"));
} finally {
serverProcess.kill("SIGTERM");
await serverProcess.status;
// Drain the lines stream
for await (const _ of lines) { /* noop */ }
}
});
});
Deno.test("pass options in config dev.ts", async (t) => {
const fixture = join(Deno.cwd(), "tests", "fixture_config");
await t.step("config.onListen", async () => {
const { lines, serverProcess, output } = await startFreshServer({
args: [
"run",
"-A",
join(fixture, "dev.ts"),
],
});
try {
assert(output.find((line) => line === "it works"));
} finally {
serverProcess.kill("SIGTERM");
await serverProcess.status;
// Drain the lines stream
for await (const _ of lines) { /* noop */ }
}
});
await t.step("config.server.onListen", async () => {
const { lines, serverProcess, output } = await startFreshServer({
args: [
"run",
"-A",
join(fixture, "dev.ts"),
],
env: {
TEST_CONFIG_SERVER: "true",
},
});
try {
assert(output.find((line) => line === "it works #2"));
} finally {
serverProcess.kill("SIGTERM");
await serverProcess.status;
// Drain the lines stream
for await (const _ of lines) { /* noop */ }
}
});
});