docs: fix testing docs (#3419)
This fixes the testing page in the docs for real this time. Every test is hand checked now. Thanks to a certain button, which appeared recently. Optionally I can also split this page up in separate pages: - Middlewars - AppWrappers and Layouts - Routing and Handlers - SSR and Islands
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
description: |
|
description: |
|
||||||
Add a global app wrapper to provide common meta tags or context for application routes.
|
Learn how to test Fresh applications using Deno's built-in test runner.
|
||||||
---
|
---
|
||||||
|
|
||||||
To ensure that your application works as expected we can write tests. Any aspect
|
To ensure that your application works as expected we can write tests. Any aspect
|
||||||
@@ -11,11 +11,13 @@ write tests.
|
|||||||
## Testing middlewares
|
## Testing middlewares
|
||||||
|
|
||||||
To test [middlewares](/docs/concepts/middleware) we're going to create a dummy
|
To test [middlewares](/docs/concepts/middleware) we're going to create a dummy
|
||||||
app and return the relevant info we want to check in a custom `/` handler.
|
app and return the relevant info we want to check in a custom `/` handler. This
|
||||||
|
test assumes the `State` object in `utils.ts` has `text` property.
|
||||||
|
|
||||||
```ts middleware.test.ts
|
```ts tests/middleware.test.ts
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import { App } from "fresh";
|
import { App } from "fresh";
|
||||||
|
import { define, type State } from "../utils.ts";
|
||||||
|
|
||||||
const middleware = define.middleware((ctx) => {
|
const middleware = define.middleware((ctx) => {
|
||||||
ctx.state.text = "middleware text";
|
ctx.state.text = "middleware text";
|
||||||
@@ -23,9 +25,11 @@ const middleware = define.middleware((ctx) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("My middleware - sets ctx.state.text", async () => {
|
Deno.test("My middleware - sets ctx.state.text", async () => {
|
||||||
const handler = new App()
|
const handler = new App<State>()
|
||||||
.use(middleware)
|
.use(middleware)
|
||||||
.get("/", (ctx) => new Response(ctx.state.text))
|
.get("/", (ctx) => {
|
||||||
|
return new Response(ctx.state.text || "");
|
||||||
|
})
|
||||||
.handler();
|
.handler();
|
||||||
|
|
||||||
const res = await handler(new Request("http://localhost"));
|
const res = await handler(new Request("http://localhost"));
|
||||||
@@ -43,11 +47,12 @@ that adds a header to the returned response, you can assert against that too.
|
|||||||
Both the [app wrapper](/docs/advanced/app-wrapper) component and
|
Both the [app wrapper](/docs/advanced/app-wrapper) component and
|
||||||
[layouts](/docs/advanced/layouts) can be tested in the same way.
|
[layouts](/docs/advanced/layouts) can be tested in the same way.
|
||||||
|
|
||||||
```tsx routes/_app.test.tsx
|
```tsx tests/appWrapper.test.tsx
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import { App } from "fresh";
|
import { App } from "fresh";
|
||||||
|
import { define, type State } from "../utils.ts";
|
||||||
|
|
||||||
function AppWrapper({ Component }) {
|
const AppWrapper = define.layout(function AppWrapper({ Component }) {
|
||||||
return (
|
return (
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
@@ -59,10 +64,10 @@ function AppWrapper({ Component }) {
|
|||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
}
|
});
|
||||||
|
|
||||||
Deno.test("App Wrapper - renders title and content", async () => {
|
Deno.test("App Wrapper - renders title and content", async () => {
|
||||||
const handler = new App()
|
const handler = new App<State>()
|
||||||
.appWrapper(AppWrapper)
|
.appWrapper(AppWrapper)
|
||||||
.get("/", (ctx) => ctx.render(<h1>hello</h1>))
|
.get("/", (ctx) => ctx.render(<h1>hello</h1>))
|
||||||
.handler();
|
.handler();
|
||||||
@@ -71,28 +76,29 @@ Deno.test("App Wrapper - renders title and content", async () => {
|
|||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
|
|
||||||
expect(text).toContain("My App");
|
expect(text).toContain("My App");
|
||||||
expect(text).toContain("Hello");
|
expect(text).toContain("hello");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
Same can be done for layouts.
|
Same can be done for layouts.
|
||||||
|
|
||||||
```tsx routes/_layout.test.tsx
|
```tsx tests/layout.test.tsx
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import { App } from "fresh";
|
import { App } from "fresh";
|
||||||
|
import { define, type State } from "../utils.ts";
|
||||||
|
|
||||||
function MyLayout({ Component }) {
|
const MyLayout = define.layout(function MyLayout({ Component }) {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>My Layout</h1>
|
<h1>My Layout</h1>
|
||||||
<Component />
|
<Component />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
});
|
||||||
|
|
||||||
Deno.test("MyLayout - renders heading and content", async () => {
|
Deno.test("MyLayout - renders heading and content", async () => {
|
||||||
const handler = new App()
|
const handler = new App<State>()
|
||||||
.layout("*", MyLayout)
|
.appWrapper(MyLayout)
|
||||||
.get("/", (ctx) => ctx.render(<h1>hello</h1>))
|
.get("/", (ctx) => ctx.render(<h1>hello</h1>))
|
||||||
.handler();
|
.handler();
|
||||||
|
|
||||||
@@ -100,42 +106,34 @@ Deno.test("MyLayout - renders heading and content", async () => {
|
|||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
|
|
||||||
expect(text).toContain("My Layout");
|
expect(text).toContain("My Layout");
|
||||||
expect(text).toContain("Hello");
|
expect(text).toContain("hello");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
## Testing routes and handlers
|
## Testing routes and handlers
|
||||||
|
|
||||||
For testing your route handlers and business logic, you can use the same
|
For testing your route handlers and business logic, you can use the same
|
||||||
[`App`](/docs/concepts/app) pattern shown above. Fresh 2.0 makes it easy to test
|
[`App`](/docs/concepts/app) pattern shown above. Fresh makes it easy to test
|
||||||
individual routes without needing a full build process:
|
individual routes without needing a full build process, as long as they export a
|
||||||
|
handler:
|
||||||
|
|
||||||
```ts my-routes.test.ts
|
```ts tests/routes.test.ts
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import { App } from "fresh";
|
import { App } from "fresh";
|
||||||
|
import { type State } from "../utils.ts";
|
||||||
|
|
||||||
// Import your route handlers
|
// Import actual route handlers
|
||||||
import { handler as indexHandler } from "./routes/index.ts";
|
import { handler as apiHandler } from "../routes/api/[name].tsx";
|
||||||
import { handler as apiHandler } from "./routes/api/users.ts";
|
|
||||||
|
|
||||||
Deno.test("Index route returns homepage", async () => {
|
Deno.test("API route returns name", async () => {
|
||||||
const app = new App().get("/", indexHandler);
|
const app = new App<State>()
|
||||||
const handler = app.handler();
|
.get("/api/:name", apiHandler.GET)
|
||||||
|
.handler();
|
||||||
|
|
||||||
const response = await handler(new Request("http://localhost/"));
|
const response = await app(new Request("http://localhost/api/joe"));
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
|
|
||||||
expect(text).toContain("Welcome");
|
expect(text).toEqual("Hello, Joe!");
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test("API route returns JSON", async () => {
|
|
||||||
const app = new App().get("/api/users", apiHandler);
|
|
||||||
const handler = app.handler();
|
|
||||||
|
|
||||||
const response = await handler(new Request("http://localhost/api/users"));
|
|
||||||
const json = await response.json();
|
|
||||||
|
|
||||||
expect(json).toEqual({ users: [] });
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -150,28 +148,37 @@ You can test that your islands render correctly on the server using the same
|
|||||||
[`App`](/docs/concepts/app) pattern. Note: this requires a `.tsx` file extension
|
[`App`](/docs/concepts/app) pattern. Note: this requires a `.tsx` file extension
|
||||||
to use JSX:
|
to use JSX:
|
||||||
|
|
||||||
```tsx island-ssr.test.tsx
|
```tsx tests/island-ssr.test.tsx
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import { App } from "fresh";
|
import { App } from "fresh";
|
||||||
import Counter from "./islands/Counter.tsx";
|
import { useSignal } from "@preact/signals";
|
||||||
|
import { type State } from "../utils.ts";
|
||||||
|
import Counter from "../islands/Counter.tsx";
|
||||||
|
|
||||||
|
function CounterPage() {
|
||||||
|
const count = useSignal(3);
|
||||||
|
return (
|
||||||
|
<div class="p-8">
|
||||||
|
<h1>Counter Test Page</h1>
|
||||||
|
<Counter count={count} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Deno.test("Counter page renders island", async () => {
|
Deno.test("Counter page renders island", async () => {
|
||||||
const app = new App().get("/counter", (ctx) => {
|
const app = new App<State>()
|
||||||
return ctx.render(
|
.get("/counter", (ctx) => {
|
||||||
<div className="p-8">
|
return ctx.render(<CounterPage />);
|
||||||
<h1>Counter Test Page</h1>
|
})
|
||||||
<Counter />
|
.handler();
|
||||||
</div>,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
const handler = app.handler();
|
|
||||||
|
|
||||||
const response = await handler(new Request("http://localhost/counter"));
|
const response = await app(new Request("http://localhost/counter"));
|
||||||
const html = await response.text();
|
const html = await response.text();
|
||||||
|
|
||||||
// Verify the island's initial HTML is present
|
// Verify the island's initial HTML is present
|
||||||
expect(html).toContain('class="counter"');
|
expect(html).toContain('class="flex gap-8 py-6"');
|
||||||
expect(html).toContain("count: 0");
|
expect(html).toContain("Counter Test Page");
|
||||||
|
expect(html).toContain("3");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -181,13 +188,34 @@ For testing client-side island behavior (clicks, state changes, etc.), you need
|
|||||||
a full build and browser environment. You can use the approach similar to
|
a full build and browser environment. You can use the approach similar to
|
||||||
Fresh's own tests:
|
Fresh's own tests:
|
||||||
|
|
||||||
```tsx island-client.test.tsx
|
```tsx tests/island-client.test.tsx
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import { createBuilder } from "vite";
|
import { buildFreshApp, startTestServer } from "./test-utils.ts";
|
||||||
|
|
||||||
|
const app = await buildFreshApp();
|
||||||
|
|
||||||
|
Deno.test("Counter island renders correctly", async () => {
|
||||||
|
const { server, address } = startTestServer(app);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Basic smoke test: verify the island HTML is served
|
||||||
|
const response = await fetch(`${address}/`);
|
||||||
|
const html = await response.text();
|
||||||
|
|
||||||
|
expect(html).toContain('class="flex gap-8 py-6"');
|
||||||
|
expect(html).toContain("3");
|
||||||
|
} finally {
|
||||||
|
await server.shutdown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
```tsx tests/test-utils.ts
|
||||||
|
import { createBuilder, type InlineConfig } from "vite";
|
||||||
import * as path from "@std/path";
|
import * as path from "@std/path";
|
||||||
|
|
||||||
// Create a production build
|
// Default Fresh build configuration
|
||||||
const builder = await createBuilder({
|
export const FRESH_BUILD_CONFIG: InlineConfig = {
|
||||||
logLevel: "error",
|
logLevel: "error",
|
||||||
root: "./",
|
root: "./",
|
||||||
build: { emptyOutDir: true },
|
build: { emptyOutDir: true },
|
||||||
@@ -195,13 +223,21 @@ const builder = await createBuilder({
|
|||||||
ssr: { build: { outDir: path.join("_fresh", "server") } },
|
ssr: { build: { outDir: path.join("_fresh", "server") } },
|
||||||
client: { build: { outDir: path.join("_fresh", "client") } },
|
client: { build: { outDir: path.join("_fresh", "client") } },
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Helper function to create and build the Fresh app
|
||||||
|
export async function buildFreshApp(config: InlineConfig = FRESH_BUILD_CONFIG) {
|
||||||
|
const builder = await createBuilder(config);
|
||||||
await builder.buildApp();
|
await builder.buildApp();
|
||||||
|
return await import("../_fresh/server.js");
|
||||||
|
}
|
||||||
|
|
||||||
const app = await import("./_fresh/server.js");
|
// Helper function to start a test server
|
||||||
|
export function startTestServer(app: {
|
||||||
Deno.test("Counter island renders correctly", async () => {
|
default: {
|
||||||
// Start production server
|
fetch: (req: Request) => Promise<Response>;
|
||||||
|
};
|
||||||
|
}) {
|
||||||
const server = Deno.serve({
|
const server = Deno.serve({
|
||||||
port: 0,
|
port: 0,
|
||||||
handler: app.default.fetch,
|
handler: app.default.fetch,
|
||||||
@@ -210,21 +246,8 @@ Deno.test("Counter island renders correctly", async () => {
|
|||||||
const { port } = server.addr as Deno.NetAddr;
|
const { port } = server.addr as Deno.NetAddr;
|
||||||
const address = `http://localhost:${port}`;
|
const address = `http://localhost:${port}`;
|
||||||
|
|
||||||
try {
|
return { server, address };
|
||||||
// Basic smoke test: verify the island HTML is served
|
|
||||||
const response = await fetch(`${address}/counter`);
|
|
||||||
const html = await response.text();
|
|
||||||
|
|
||||||
expect(html).toContain('class="counter"');
|
|
||||||
expect(html).toContain("count: 0");
|
|
||||||
|
|
||||||
// For full browser interactivity testing, you would need:
|
|
||||||
// - Browser automation tools (Puppeteer, Playwright)
|
|
||||||
// - withBrowser utility from Fresh's test suite
|
|
||||||
} finally {
|
|
||||||
await server.shutdown();
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Note:** For most applications, testing the server-side rendering is
|
**Note:** For most applications, testing the server-side rendering is
|
||||||
|
|||||||
Reference in New Issue
Block a user