Testing Failures in a Fetch Service with FetchTest Layer

How to test failures in a service? I have a basic fetch service with a FetchTest defined like so:

const FetchTest = Layer.succeed(
  Fetch,
  Fetch.of({
    get: (url: string) =>
      Effect.tryPromise<Todos, FetchError>({
        try: () => {
          if (url === "fail") {
            return Promise.reject("rejection");
          }
          return Promise.resolve({});
        },
        catch: (err) => new FetchError(err),
      }),
    post: (url: string) =>
      Effect.tryPromise<Todos, FetchPostError>({
        try: () => {
          if (url === "fail") {
            return Promise.reject("rejection");
          }
          return Promise.resolve({});
        },
        catch: () => new FetchPostError(),
      }),
  }),
);


I don't know if this is "correct", but I'm not sure how else to trigger a failure in this case other than explicitly do so based on some variable passed in.

So open to a better way to do this part, but here is my test:

it.effect("should fail a post", () =>
  Effect.gen(function* () {
    const { post } = yield* Fetch;
    const todos = yield* post("fail", {} as BodyInit);
    expect(todos).toBe({ _tag: "FetchPostError" });
  }).pipe(Effect.provide(FetchTest)),
);


My thought is that I'd realistically pass in the error class to the toBe or whatever the matcher should be here. But I always end up getting

should fail a post: failed
{"_tag":"FetchPostError"}


Am I asserting the wrong thing?
Was this page helpful?