Skip to content

Hono doesn't consider status codes from middlewares with testClient #3170

@cybercoder-naj

Description

@cybercoder-naj

What version of Hono are you using?

4.4.9

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

// index.ts
import { Hono } from "hono";
import { logger } from "hono/logger";
import { createFactory } from "hono/factory";

const factory = createFactory();

const myMiddleware = () => {
  return factory.createMiddleware(async (c, next) => {
    const random = Math.random();
    if (random > 0.5) {
      c.status(500);
      return c.text("Probabilistic error", 500);
    }
    return next();
  });
};

const app = new Hono().use(logger()).get("/", myMiddleware(), async (c) => {
  return c.json({ message: "Hello, World!" }, 200);
});

export default app;

// index.test.ts
import { test, expect } from "bun:test";
import app from ".";
import { testClient } from "hono/testing";

test("GET /", async () => {
  const res = await testClient(app).index.$get();

  expect(res.status).toBe(500); // <- "Argument of type '500' is not assignable to parameter of type '200'"
});

What is the expected behavior?

Consider other status codes that may be returned from middlewares or other factors.

What do you see instead?

Argument of type '500' is not assignable to parameter of type '200'

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions