From 48501ced5b9dae29ffd44979704389347f85cc2a Mon Sep 17 00:00:00 2001 From: Steffen Deusch Date: Sat, 10 Feb 2024 23:09:04 +0100 Subject: [PATCH] use mix test --- package.json | 2 +- test/e2e/teardown.js | 11 +++++-- test/e2e/test_helper.exs | 66 +++++++++++----------------------------- 3 files changed, 27 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 5f74c5c6e8..2221d657aa 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "monocart-reporter": "^2.3.1" }, "scripts": { - "e2e:server": "MIX_ENV=e2e mix run test/e2e/test_helper.exs", + "e2e:server": "MIX_ENV=e2e mix test --cover --export-coverage e2e test/e2e/test_helper.exs", "e2e:test": "mix assets.build && cd test/e2e && npx playwright test", "js:test": "cd assets && npm install && npm run test", "test": "npm run js:test && npm run e2e:test", diff --git a/test/e2e/teardown.js b/test/e2e/teardown.js index 057244bf2c..0d696216c7 100644 --- a/test/e2e/teardown.js +++ b/test/e2e/teardown.js @@ -1,6 +1,13 @@ const request = require("@playwright/test").request; module.exports = async () => { - const context = await request.newContext({ baseURL: "http://localhost:4000" }); - await context.post("/export-coverage") + try { + const context = await request.newContext({ baseURL: "http://localhost:4000" }); + // gracefully stops the e2e script to export coverage + await context.post("/halt") + } catch (e) { + // we expect the request to fail because the request + // actually stops the server + return + } }; diff --git a/test/e2e/test_helper.exs b/test/e2e/test_helper.exs index 4d7c0e964c..332d714207 100644 --- a/test/e2e/test_helper.exs +++ b/test/e2e/test_helper.exs @@ -17,41 +17,7 @@ Application.put_env(:phoenix_live_view, Phoenix.LiveViewTest.E2E.Endpoint, debug_errors: false ) -Mix.ensure_application!(:tools) -_ = :cover.stop() -{:ok, cover_pid} = :cover.start() - -beams = fn dir -> - consolidation_dir = Mix.Project.consolidation_path() - - consolidated = - case File.ls(consolidation_dir) do - {:ok, files} -> files - _ -> [] - end - - for file <- File.ls!(dir), Path.extname(file) == ".beam" do - with true <- file in consolidated, - [_ | _] = path <- :code.which(file |> Path.rootname() |> String.to_atom()) do - path - else - _ -> String.to_charlist(Path.join(dir, file)) - end - end -end - -for compile_path <- [Mix.Project.compile_path()] do - case :cover.compile_beam(beams.(compile_path)) do - results when is_list(results) -> - :ok - - {:error, reason} -> - Mix.raise( - "Failed to cover compile directory #{inspect(Path.relative_to_cwd(compile_path))} " <> - "with reason: #{inspect(reason)}" - ) - end -end +pid = self() defmodule Phoenix.LiveViewTest.E2E.ErrorHTML do def render(template, _), do: Phoenix.Controller.status_message_from_template(template) @@ -132,7 +98,7 @@ defmodule Phoenix.LiveViewTest.E2E.Endpoint do plug Plug.Static, from: System.tmp_dir!(), at: "/tmp" plug :health_check - plug :coverage + plug :halt plug Plug.Parsers, parsers: [:urlencoded, :multipart, :json], @@ -147,20 +113,13 @@ defmodule Phoenix.LiveViewTest.E2E.Endpoint do defp health_check(conn, _opts), do: conn - defp coverage(%{request_path: "/export-coverage"} = conn, _opts) do - output = "cover" - File.mkdir_p!(output) - - case :cover.export(~c"#{output}/e2e.coverdata") do - :ok -> - conn |> Plug.Conn.send_resp(200, "OK") |> Plug.Conn.halt() - - {:error, reason} -> - conn |> Plug.Conn.send_resp(500, inspect(reason)) |> Plug.Conn.halt() - end + defp halt(%{request_path: "/halt"}, _opts) do + send(unquote(pid), :halt) + # this ensure playwright waits until the server force stops + Process.sleep(:infinity) end - defp coverage(conn, _opts), do: conn + defp halt(conn, _opts), do: conn end {:ok, _} = @@ -173,5 +132,14 @@ end ) unless IEx.started?() do - Process.sleep(:infinity) + # when running the test server manually, we halt after + # reading from stdin + spawn(fn -> + IO.read(:stdio, :line) + send(pid, :halt) + end) + + receive do + :halt -> :ok + end end