Use assert/2 with a stacktrace

I’m working on some test helpers for my GraphQL tests:

This is what the default output looks like with assert/1:

  1) test me with unauthorized user cannot retrieve the current user (WebInterface.GraphQL.SchemaTest)
     test/web_interface/graphql/schema_test.exs:26
     Expected truthy, got false
     code: assert Enum.any?(errors, fn error -> error["message"] == Atom.to_string(error_message) && Enum.join(error["path"], "/") == path_specification end)
     stacktrace:
       (web_interface) test/support/graphql_utils.ex:24: WebInterface.Test.GraphQLUtils.assert_has_graphql_error/3
       test/web_interface/graphql/schema_test.exs:32: (test)

But since that isn’t very helpful (doesn’t show the actual or expected values of the errors) I’m creating my own custom output. Which is create but I’d really like to still include the full stacktrace. This is what I currently get (note that the graphql_utils) entry is now missing from the stacktrace:

  1) test me with unauthorized user cannot retrieve the current user (WebInterface.GraphQL.SchemaTest)
     test/web_interface/graphql/schema_test.exs:26
     Unable to find error "no_user_logged_in" at path: "me"
     
     Errors: [%{"locations" => [%{"column" => 0, "line" => 1}], "message" => "unauthorized", "path" => ["me"]}]
     
     code: assert_has_graphql_error(conn, "me", :no_user_logged_in)
     stacktrace:
       test/web_interface/graphql/schema_test.exs:32: (test)

For full reference here is the contents of my assertion function (slightly tweaked from the output above):

  def assert_has_graphql_error(conn, path_specification, error_message) do
    body = json_response(conn, 200)
    errors = body["errors"]

    assertion_error_message = """
    Unable to find error \"#{error_message}\" at path: \"#{path_specification}\" in list of errors.

    Received Errors: #{inspect errors, pretty: true}
    """

    assert Enum.any?(errors, fn error ->
      error["message"] == Atom.to_string(error_message) &&
        Enum.join(error["path"], "/") == path_specification
    end), assertion_error_message
  end

Is there any way to include the full stacktrace here?

1 Like