Skip to content

Commit

Permalink
use mix test
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Feb 10, 2024
1 parent 4619e33 commit e23d023
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions test/e2e/teardown.js
Original file line number Diff line number Diff line change
@@ -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
}
};
66 changes: 17 additions & 49 deletions test/e2e/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -130,7 +96,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],
Expand All @@ -145,20 +111,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"} = conn, _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, _} =
Expand All @@ -171,5 +130,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

0 comments on commit e23d023

Please sign in to comment.