**Issue with Injecting HttpClient.Fetch for Unit Tests in API Client Adapter**
Hey folks, I am trying to inject HttpClient.Fetch for unit tests in my api client adaptor. But it does not get executed. Is it due to scopes or I may be doing something entirely wrong?
import {
HttpClient,
HttpClientRequest,
HttpClientResponse,
} from "@effect/platform";
import { Schema } from "@effect/schema";
import { Context, Effect, Layer, pipe } from "effect";
type Note = {
id: string;
note: string;
};
type Author = {
id: string;
name: string;
};
interface ApiClient {
createNote(author: Author, note: string): Effect.Effect<string, any, never>;
// other methods
}
const ApiClient = Context.GenericTag<ApiClient>("ApiClient");
const createClientAdapter = () =>
Layer.effect(
ApiClient,
pipe(
HttpClient.fetchOk,
HttpClient.mapRequest(
HttpClientRequest.prependUrl("https://api.example.com")
),
HttpClient.mapRequest(HttpClientRequest.accept("application/json")),
HttpClient.mapRequest(
HttpClientRequest.setHeaders({
"Content-Type": "application/json",
})
),
(client) =>
ApiClient.of({
createNote: (author, note) =>
pipe(
HttpClientRequest.post("/notes"),
HttpClientRequest.schemaBody(Schema.Any)({ author, note }),
Effect.flatMap(client),
Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.String)),
Effect.scoped
),
}),
Effect.succeed
)
);
// Mock fetch implementation
const FetchTest = Layer.succeed(HttpClient.Fetch, () =>
Promise.resolve(new Response("not found", { status: 404 }))
);
// Test
const apiClient = Effect.runSync(
pipe(
ApiClient,
Effect.provide(createClientAdapter()),
Effect.provide(FetchTest)
)
);
Effect.runPromise(
apiClient.createNote({ id: "1", name: "John" }, "Hello world!")
).then((result) => {
console.log(result);
});import {
HttpClient,
HttpClientRequest,
HttpClientResponse,
} from "@effect/platform";
import { Schema } from "@effect/schema";
import { Context, Effect, Layer, pipe } from "effect";
type Note = {
id: string;
note: string;
};
type Author = {
id: string;
name: string;
};
interface ApiClient {
createNote(author: Author, note: string): Effect.Effect<string, any, never>;
// other methods
}
const ApiClient = Context.GenericTag<ApiClient>("ApiClient");
const createClientAdapter = () =>
Layer.effect(
ApiClient,
pipe(
HttpClient.fetchOk,
HttpClient.mapRequest(
HttpClientRequest.prependUrl("https://api.example.com")
),
HttpClient.mapRequest(HttpClientRequest.accept("application/json")),
HttpClient.mapRequest(
HttpClientRequest.setHeaders({
"Content-Type": "application/json",
})
),
(client) =>
ApiClient.of({
createNote: (author, note) =>
pipe(
HttpClientRequest.post("/notes"),
HttpClientRequest.schemaBody(Schema.Any)({ author, note }),
Effect.flatMap(client),
Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.String)),
Effect.scoped
),
}),
Effect.succeed
)
);
// Mock fetch implementation
const FetchTest = Layer.succeed(HttpClient.Fetch, () =>
Promise.resolve(new Response("not found", { status: 404 }))
);
// Test
const apiClient = Effect.runSync(
pipe(
ApiClient,
Effect.provide(createClientAdapter()),
Effect.provide(FetchTest)
)
);
Effect.runPromise(
apiClient.createNote({ id: "1", name: "John" }, "Hello world!")
).then((result) => {
console.log(result);
});