|
Is it possible to run tests on an app with fsRoutes? Every way I've tested ends with the app just not being able to access the routes If no, ought there not be some kind of warning when using fsRoutes in a way that it doesn't have the necessary information? |
Replies: 2 comments 3 replies
|
Yes. The key distinction is that For a unit test of one route, the simplest supported pattern is to import its handler and register it explicitly: import { App } from "fresh";
import { handler as apiHandler } from "../routes/api/[name].tsx";
const handler = new App()
.get("/api/:name", apiHandler.GET)
.handler();
const response = await handler(
new Request("http://localhost/api/joe"),
);That is also the pattern in the current testing guide. If the test specifically needs to verify filesystem discovery (layouts, middleware, route filenames, islands, and so on), it needs to go through a build so the route snapshot exists. With the current Vite setup, use I agree that silently producing 404s when no build cache is attached is surprising. The behavior is explicit in the implementation, but it is easy to miss from the |
|
Right—the explicit handler pattern is only practical for a single-route unit test. For a multi-route integration test, the in-memory A file such as const builder = new Builder({ root: projectRoot });
const applySnapshot = await builder.build({ snapshot: "memory" });
const app = new App().fsRoutes();
applySnapshot(app);
const response = await app.handler()(
new Request("http://localhost/users/123"),
);If a concrete dynamic path still returns 404 with that setup, that is not expected. Please share the Fresh version, the small route tree, the request URL, and how |
Right—the explicit handler pattern is only practical for a single-route unit test. For a multi-route integration test, the in-memory
Buildersnapshot is the better fit.A file such as
routes/users/[id].tsxis registered as a parameterized route, so the test request must use a concrete path such as/users/123, not/users/[id]. The other two easy gotchas are thatBuilderresolvesroutes/fromDeno.cwd()unlessrootis set, and the snapshot must be applied to the same app after.fsRoutes():