Question - Ideal unit testing of GraphQL

I am asking this question in terms of the speed/execution time as well as test coverage.

Considering these 2 ways, which one should be ideal to test GraphQL.
Second way does not ask for conn, doesn’t POST. So, can I assume its faster or much faster.
If I have 50 Schema or query and second method saves time, Should i add an additional test to ensure the POST to graphQL is working and getting me back the result.

Last project I worked… it took few minutes for all tests to run. That was bothering me.

I am open to suggestions.

defmodule DakgharWeb.GraphQL.Schema.TopicSchemaTest do
  use DakgharWeb.ConnCase

  @query """
  query{listTopics {
    code
    name
  }}
  """
  describe "list all topics" do
    test "success - returns blank for no topics with conn", %{conn: conn} do
      assert %{
               "data" => %{"listTopics" => data}
             } =
               conn
               |> post("/graphql", %{query: @query, variables: %{}})
               |> json_response(200)

      assert data == []
    end

    test "success - returns blank for no topics without conn" do
      result = Absinthe.run(@query, DakgharWeb.GraphQL.Schema, variables: %{})

      assert result = {:ok, data: %{"data" => []}}
    end
  end
end

This isn’t really true, phoenix conn tests aren’t doing an actual HTTP connection, they’re just making a fake conn and running it through the plug pipeline.

The overhead of doing it with phoenix conn tests is going to be probably < 1ms as compared to calling Absinthe.run manually. I tend to favor the conn tests because it makes sure that you actually set up your context correctly on HTTP calls.

3 Likes