Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testClient does not open websockets for testing #3239

Open
cybercoder-naj opened this issue Aug 6, 2024 · 4 comments
Open

testClient does not open websockets for testing #3239

cybercoder-naj opened this issue Aug 6, 2024 · 4 comments
Labels

Comments

@cybercoder-naj
Copy link

cybercoder-naj commented Aug 6, 2024

What version of Hono are you using?

4.5.1

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

Step 1: Create a route with an upgrade to web socket handler.

const app = factory
  .createApp()
  .get(
    '/ws',
    upgradeWebSocket(async c => {
      return {
        onOpen(evt, ws) {
          console.log('WebSocket opened');
          ws.send('Hello from the server');
        },
      };
    })
  );

Bun.serve({
  fetch: app.fetch,
  port: Bun.env.PORT || 3000,
  websocket
})

Step 2a: Now have two variations of test files. Let's call the first one withHc.test.ts:

const client = hc<typeof app>("http://localhost:3000");
const socket = client.ws.$ws(0);

test("testing websocket", async () => {
  const res = await new Promise<Event>((resolve, reject) => {
    socket.addEventListener(
      'open',
      ev => {
        console.log('WebSocket connection opened');
        resolve(ev);
      },
      { once: true }
    );
  });

  console.log(res);
  expect(res).toBeTruthy();
})

Start the server on port 3000. Run the tests with bun test and you will see the console logs and the test passes 🥳


Step 2b: Let's call the second test file withClient.test.ts:

const client = testClient(app); // <- This is the only thing that has changed!
const socket = client.ws.$ws(0);

test("testing websocket", async () => {
  const res = await new Promise<Event>((resolve, reject) => {
    socket.addEventListener(
      'open',
      ev => {
        console.log('WebSocket connection opened');
        resolve(ev);
      },
      { once: true }
    );
  });

  console.log(res);
  expect(res).toBeTruthy();
})

Don't start the server. Simply run bun test and the code will timeout 😢 . This is because the socket object from testClient never opens a connection.

What is the expected behavior?

Any sockets created using testClient should also open connections on request. In a testing environment, it is important to have this helper work to maintain consistency around the project.

What do you see instead?

testClient does not open a connection so I cannot test websocket server on the route.

Additional information

No response

@yusukebe
Copy link
Member

yusukebe commented Aug 8, 2024

Hi @cybercoder-naj

In short, you cannot use testClient to test WebSockets in Bun.

To test WebSockets in Bun, you have to run the actual server on like localhost:3000.

With Step 2a, you run the server at 3000 port, but with Step 2b, you don't run a server. The testClient can't execute a server, and it's not created for that purpose.

@cybercoder-naj
Copy link
Author

Is it possible to have a websocket test helper planned for Hono?

@yusukebe
Copy link
Member

yusukebe commented Aug 9, 2024

This may be related to #3185

@nakasyou Isn't it?

@nakasyou
Copy link
Contributor

nakasyou commented Aug 9, 2024

@yusukebe Yes.
But I don't know about better API for that. It needs to discuss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants