Effect CommunityEC
Effect Community•8mo ago•
14 replies
JimZer

Querying Effect HttpApi for Unit Tests without running server

Hi Effect team, first off, thanks for the amazing work on Effect! 🙌 I’ve built my first HttpApi server using @effect/platform-bun, and the dev experience has been fantastic. Here’s a simplified version of my setup:

// Server setup
const ServerLive = HttpApiBuilder.serve().pipe(
  Layer.provide(HttpApiBuilder.middlewareCors()),
  Layer.provide(HttpApiSwagger.layer()),
  Layer.provide(ApiLive),
  Layer.provide(BunHttpServer.layer({ port: 3000 })),
);

// Launch server
Layer.launch(ServerLive).pipe(BunRuntime.runMain);

// Testing the server
const program = Effect.gen(function* () {
  const ref = yield* Ref.make(Cookies.empty);
  const client = yield* HttpApiClient.make(Api, {
    baseUrl: "http://localhost:3000",
    transformClient: (client) => client.pipe(HttpClient.withCookiesRef(ref)),
  });

  const res2 = yield* client.auth.usernameSignup({
    payload: { email: "test", password: "password" },
  });
  console.log(res2);

  const res3 = yield* client.auth.usernameLogin({
    payload: { email: "test", password: "password" },
    withResponse: true,
  });
  console.log(res3[1].headers);

  const res1 = yield* client.auth.getMe();
  console.log(res1);
});

Effect.runPromise(program.pipe(Effect.provide(FetchHttpClient.layer)));


My question is about unit testing. To query the API, I currently need to launch the server with Layer.launch(ServerLive).pipe(BunRuntime.runMain). For end-to-end tests, I want to test the actual business logic against a real test database (cleaned between tests), not mock the server. Is there a way to query the API directly without spinning up the server, similar to how Hono’s test client works (e.g., const res = await app.request('/posts'))?

This would be super helpful for streamlining my test setup. Thanks!
Was this page helpful?