Testing Logger messages in Absinthe mutations with ExUnit capture_log

I’ve been struggling trying to figure out how to use capture_log to test if Logger messages are emitted inside code resolving Absinthe mutations.


log =
  capture_log(fn ->
    conn =
      post(conn, "/gql", %{
        query: @password_redeem_mutation,
        variables: %{"password" => new_pwd, "resetToken" => reset_token}

assert log =~ "expired token"
assert json_response(conn, 200) == %{"data" => %{ ...expected data here... }}

The issue is capture_log takes an anonymous function which only returns the captured log so I can’t get the new conn back to test if I received the proper data return. I can’t just send multiple identical GQL mutations as they may not be idempotent.

I thought about also adding the log message to conn.resp_headers (only when env == :test) in the function that has the actual Logger call but that requires some gymnastics with adding a middleware to all of my mutations to add the log message to the Absinthe context then using before_send in the router to append the resp_header. I think that may be the only way to get it to work but not sure it’s worth it and hoping there is some other way to test logging?