elysiajs / eden

Fully type-safe Elysia client
MIT License
154 stars 37 forks source link

[BUG] Client not receiving response body object with edenFetch #56

Open gabriel-abn opened 6 months ago

gabriel-abn commented 6 months ago

Making some native Bun tests using Eden but I'm facing a null data object in response object. I'm gonna let my fetching config and what I'm receiving in my test.

// --- src/main/adapter/rest-adapter.ts

import type { Controller, HttpRequest } from "@/presentation/common";

export default (controller: Controller<any>) =>
  async ({ body, query, params, headers, set }: any): Promise<any> => {
    set.headers["Content-Type"] = "application/json";

    try {
      const request: HttpRequest<any> = { body, query, params, headers };

      const result = await controller.handle(request);

      set.status = result.status;

      return JSON.stringify({ response: result.body });
    } catch (error: any) {
      set.status = 500;

      return JSON.stringify({ error: error.message });
    }
  };

// --- src/main/app.ts

import authRoutes from "@/main/routes/auth-routes";
import { Elysia } from "elysia";

const app = new Elysia()
  .get("/", () => {
    return "Hello World!";
  })
  .group("/auth", (app) => app.use(authRoutes));

export default app;

export type App = typeof app;

// --- tests/config/api.ts

import app, { type App } from "@/main/app";
import env from "@/main/env";
import { edenFetch } from "@elysiajs/eden";
import { afterAll, beforeAll } from "bun:test";

beforeAll(() => {
  app.listen(env.PORT, () => {
    console.log("Started testing server...");
  });
});

afterAll(async () => {
  await app.stop();
});

export default edenFetch<App>(`http://localhost:${env.PORT}`);

// --- test file
import api from "@/config/api";
import { faker } from "@faker-js/faker";
import { beforeAll, describe, expect, it } from "bun:test";

describe("Create an account/Sign up", () => {
  let response: any;

  beforeAll(async () => {
    const user = {
      username: faker.internet.userName(),
      email: faker.internet.email(),
      password: faker.internet.password(),
    };

    response = await api("/auth/signup", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: {
        username: user.username,
        email: user.email,
        password: user.password,
      },
    });
  });

  it("should receive email and password", () => {
    console.log(response.data);
    expect(response.status).toBe(200);
  });
});

When fetching from an external client, like Postman or Insomnia, I get my response just like it should be. But when running tests with bun run test data field in response object is null. This image is showing what is happening: I'm logging what I'm returning to route and logging the response object I'm getting of this route.

image

"@elysiajs/eden": "^0.8.1",
"elysia": "^0.8.17",

Any thoughts?

EvHaus commented 6 months ago

I'm seeing the same issue. Using edenTreaty gives me a proper response but when using edenFetch on the same endpoint, the response is null.

Using @elysiajs/eden@1.0.0-rc.3