createHandler() (#622)
I cut out the function that gets an hundler from fresh.
The purpose is to prepare you to create tests for your application.
The introduction of this feature does not affect fresh.
Example:
```typescript main_test.ts
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />
import { createHandler } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import { superdeno } from "https://deno.land/x/superdeno/mod.ts";
import { assertEquals } from "https://deno.land/std@0.148.0/testing/asserts.ts";
import {
type RequestHandlerLike,
} from "https://deno.land/x/superdeno/src/types.ts";
import {
Document,
DOMParser,
} from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
Deno.test("HTTP assert test.", async (t) => {
await t.step("#1 GET /", async () => {
const handler = await createHandler(manifest) as RequestHandlerLike;
await superdeno(handler)
.get("/")
.expect(200);
});
await t.step("#2 GET /Foo", async () => {
const server = await createHandler(manifest) as RequestHandlerLike;
const r = await superdeno(server)
.get("/Foo")
.expect(200);
const document: Document = new DOMParser().parseFromString(
r.text,
"text/html",
)!;
assertEquals(document.querySelector("div")?.innerText, "Hello Foo");
});
await t.step("#3 GET /Foo/Bar", async () => {
const server = await createHandler(manifest) as RequestHandlerLike;
await superdeno(server)
.get("/Foo/Bar")
.expect(404);
});
});
```
```sh
$ deno test -A main_test.ts
Check file:///usr/src/app/my-project/main_test.ts
running 1 test from ./main_test.ts
HTTP assert test. ...
#1 GET / ... ok (53ms)
#2 GET /Foo ... ok (50ms)
#3 GET /Foo/Bar ... ok (45ms)
HTTP assert test. ... ok (154ms)
ok | 1 passed (3 steps) | 0 failed (300ms)
```
This was discussed in a separate pull request, but prepared in a
separate branch based on @lucacasonato suggestion.
[Add a function to get a server class
instance.](https://github.com/denoland/fresh/pull/552)
Thenk you.
Documentation | Getting started | API Reference
fresh
Fresh is a next generation web framework, built for speed, reliability, and simplicity.
Some stand-out features:
- Just-in-time rendering on the edge.
- Island based client hydration for maximum interactivity.
- Zero runtime overhead: no JS is shipped to the client by default.
- No build step.
- No configuration necessary.
- TypeScript support out of the box.
- File-system routing à la Next.js.
📖 Documentation
The documentation is available on fresh.deno.dev.
🚀 Getting started
Install Deno CLI version 1.25.0 or higher.
You can scaffold a new project by running the Fresh init script. To scaffold a
project in the deno-fresh-demo folder, run the following:
deno run -A -r https://fresh.deno.dev deno-fresh-demo
Then navigate to the newly created project folder:
cd deno-fresh-demo
From within your project folder, start the development server using the
deno task command:
deno task start
Now open http://localhost:8000 in your browser to view the page. You make changes to the project source code and see them reflected in your browser.
To deploy the project to the live internet, you can use Deno Deploy:
- Push your project to GitHub.
- Create a Deno Deploy project.
- Link the Deno Deploy
project to the
main.tsfile in the root of the created repository. - The project will be deployed to a public $project.deno.dev subdomain.
For a more in-depth getting started guide, visit the Getting Started page in the Fresh docs.
Adding your project to the showcase
If you feel that your project would be helpful to other fresh users, please consider putting your project on the showcase. However, websites that are just for promotional purposes may not be listed.
To take a screenshot, run the following command.
deno task screenshot [url] [your-app-name]
Then add your site to showcase.json, preferably with source code on GitHub, but not required.
Badges
[](https://fresh.deno.dev)
<a href="https://fresh.deno.dev">
<img width="197" height="37" src="https://fresh.deno.dev/fresh-badge.svg" alt="Made with Fresh" />
</a>
[](https://fresh.deno.dev)
<a href="https://fresh.deno.dev">
<img width="197" height="37" src="https://fresh.deno.dev/fresh-badge-dark.svg" alt="Made with Fresh" />
</a>