fresh icon indicating copy to clipboard operation
fresh copied to clipboard

[Fresh 2] Documentation for migrating `createHandler`

Open honmanyau opened this issue 7 months ago • 2 comments

createHandler (https://fresh.deno.dev/docs/examples/writing-tests) appears to have been removed from Fresh 2. It's unclear what the new pattern for writing of integration tests should be.

It would be fantastic if we could have that included in the migration documentation, too!

Apologies in advance if I have missed something obvious, or missed details in the blog post or migration instructions!

honmanyau avatar May 21 '25 02:05 honmanyau

Fresh 1.x

import { createHandler, ServeHandlerInfo } from "$fresh/server.ts";
import manifest from "../fresh.gen.ts";
import config from "../fresh.config.ts";

const handler = await createHandler(manifest, config);

Fresh 2.x

import { app } from "../main.ts";

const handler = app.handler()

marvinhagemeister avatar May 21 '25 14:05 marvinhagemeister

Thank you very much for your help, @marvinhagemeister! ☀ That simple change + rebuilding was all that was needed.

For context because I don't think it's specific to my project, rebuilding was necessary because I was seeing errors like this:

./services/user.test.ts (uncaught error)
error: (in promise) TypeError: Cannot convert undefined or null to object
      const files = Object.keys(snapshot.staticFiles);
                           ^
    at Object.keys (<anonymous>)
    at ProdBuildCache.fromSnapshot (https://jsr.io/@fresh/core/2.0.0-alpha.34/src/build_cache.ts:45:28)
    at App.handler (https://jsr.io/@fresh/core/2.0.0-alpha.34/src/app.ts:251:41)
    # === Where `app.handler` is called. ===
    at file:///Users/redacted-username/Development/redacted-project-name/utils/test.ts:38:21

This error was not caught from a test and caused the test runner to fail on the referenced module.
It most likely originated from a dangling promise, event/timeout handler or top-level code.

Edit: am more than happy to open a PR to update migration guide if that' s welcomed. :)

honmanyau avatar May 22 '25 03:05 honmanyau

@marvinhagemeister Thank you very much for adding this to the docs! Have a lovely Friday and a fantastic weekend in advance! ☀️

honmanyau avatar Jul 25 '25 04:07 honmanyau