Effect CommunityEC
Effect Community14mo ago
7 replies
Angan

Issues with @effect/vitest and HttpClient Error Handling in Tests

I try to write reusable request function with HttpClient from @effect/platform, and add some test with vitest to make sure the function works fine. And last week I just notice effect has @effect/vitest to test effect directly. So, I change the test suite to @effect/vitest. And then I got problem, some of my test return different result with the regular vitest. The function is still same without any changes, just change from regular vitest to @effect/vitest.

Here the test suite:

describe("Test", () => {
  it.effect("Request GET with effect test", () =>
    Effect.gen(function* () {
      const getResponse = yield* getRequest.pipe(Effect.exit);

      expect(getResponse).toStrictEqual(
        Exit.fail("Transport error (GET https://this-not-exist-web.com)")
      );
    }).pipe(Effect.provide(FetchHttpClient.layer))
  );

  it("Request GET with normal test", async () => {
    const getResponse = () =>
      getRequest.pipe(Effect.provide(FetchHttpClient.layer), Effect.runPromise);

    await expect(getResponse()).rejects.toThrowError(
      "Transport error (GET https://this-not-exist-web.com)"
    );
  });
});


Here the function to request:

export const getRequest = Effect.gen(function* () {
  const client = yield* HttpClient.HttpClient.pipe(
    Effect.andThen(
      HttpClient.retryTransient({
        times: 2,
        schedule: Schedule.exponential(100),
      })
    )
  );

  return yield* client.get("https://this-not-exist-web.com").pipe(
    Effect.andThen((response) => response.text),
    Effect.scoped,
    Effect.mapError((error) => {
      return error;
    })
  );
});


In this part I just want to make sure in the test suite if I request to not exist endpoint it will error.

Here sandbox to try run the test:
https://codesandbox.io/p/devbox/2dtktm

Maybe I'm not correct with @effect/vitest implementation, or maybe the function with HttpClient is not correct in my code. Maybe somebody can help me.

Thank you.
Was this page helpful?