diff --git a/src/commands/snapshot/snapshot-fallback.test.ts b/src/commands/snapshot/snapshot-fallback.test.ts new file mode 100644 index 0000000..01f061a --- /dev/null +++ b/src/commands/snapshot/snapshot-fallback.test.ts @@ -0,0 +1,24 @@ +import { Hono } from 'hono' +import { describe, it, expect, vi } from 'vitest' +import { snapshotLines } from './snapshot.js' + +// Hono v5 lists notFound and onError handlers like this +vi.mock('hono/dev', () => ({ + inspectRoutes: () => [ + { path: '/health', method: 'GET', name: '[handler]', isMiddleware: false }, + { path: '/*', method: '@NOT_FOUND', name: '[handler]', isMiddleware: false }, + { path: '/*', method: '@ERROR', name: '[handler]', isMiddleware: false }, + ], +})) + +describe('snapshotLines on Hono v5', () => { + it('skips the notFound and onError handlers', async () => { + const app = new Hono() + app.get('/health', (c) => c.text('ok')) + const lines = (await snapshotLines(app)).map((l) => JSON.parse(l)) + expect(lines).toEqual([ + { path: '/health', expect: { status: 200, body: 'ok' } }, + { path: '/__no_such_path__', expect: { status: 404, body: '404 Not Found' } }, + ]) + }) +}) diff --git a/src/commands/snapshot/snapshot.ts b/src/commands/snapshot/snapshot.ts index 4943eae..328ad8c 100644 --- a/src/commands/snapshot/snapshot.ts +++ b/src/commands/snapshot/snapshot.ts @@ -18,7 +18,11 @@ export const snapshotLines = async ( target: RequestTarget = app ): Promise => { const lines: string[] = [] - const routes = inspectRoutes(app).filter((route) => !route.isMiddleware) + // Hono v5 lists notFound and onError handlers with a method like + // "@NOT_FOUND". They are not routes to request. + const routes = inspectRoutes(app).filter( + (route) => !route.isMiddleware && !route.method.startsWith('@') + ) for (const route of routes) { const isParamless = !route.path.includes(':') && !route.path.includes('*')