Effect CommunityEC
Effect Community2y ago
4 replies
Gibbs

Testing HttpClient

How can I inject a "fake" HttpClient in tests ?
I'm trying this:
  const HttpClientTestLayer = Layer.succeed(
    Http.client.Client,
    Http.client.makeDefault(request =>
      Effect.succeed(
        Http.response.fromWeb(
          request,
          new Response('not found', { status: 404 }),
        ),
      ),
    ),
  );

  it.effect('google', () =>
    Effect.gen(function* () {
      const response = yield* Http.request
        .get('https://www.google.com/')
        .pipe(Http.client.fetch, Http.response.text);

      expect(response).toContain('google');
    }).pipe(Effect.provide(HttpClientTestLayer)),
  );

I would expect that the test fail, but it pass. Seems like the provided client is completely ignored and my test is doing a real http call..
Was this page helpful?