scripts/.lune/tests/reporter.luau
Erica Marigold 54a3f6869b
chore(lune): fix test skipping
* Previously, skipping tests using arguments of selective files
  to include did not work correctly due to our directory structure.
  This has been fixed.
* On test skip, the reporter would tyr to get the style for the report
  with no timestamp argument, causing a string formatting with nil
  error. For skips, we force the timestamp to be zero now.
2024-12-04 06:27:20 +00:00

60 lines
1.9 KiB
Text

local stdio = require("@lune/stdio")
local frktest = require("../../lune_packages/frktest")
local Reporter = frktest._reporters.lune_console_reporter
local watch = require("../lib/channel")
local STYLE = table.freeze({
suite = function(name: string)
return `{stdio.style("bold")}{stdio.color("purple")}SUITE{stdio.style("reset")} {name}`
end,
report = function(name: string, state: "success" | "error" | "skip", elapsed: number)
local state_color: stdio.Color = if state == "success" then "green"
elseif state == "error" then "red"
elseif state == "skip" then "yellow"
else error("Invalid test state")
return ` {stdio.style("bold")}{stdio.color(state_color)}{if state == "skip" then "SKIP" else "TEST"}{stdio.style(
"reset"
)} {name} [{stdio.style("dim")}{string.format("%.2fms", elapsed)}{stdio.style("reset")}]`
end,
})
local ReporterExt = {}
function ReporterExt.init()
frktest.test.on_suite_enter(function(suite)
print(STYLE.suite(suite.name))
end)
frktest.test.on_suite_leave(function()
stdio.write("\n")
end)
local send_ts, recv_ts = watch((nil :: any) :: number)
frktest.test.on_test_enter(function()
-- Send over some high precision timestamp when the test starts
return send_ts(os.clock())
end)
frktest.test.on_test_leave(function(test)
print(
STYLE.report(
test.name,
if test.failed then "error" else "success",
-- Await receival of the timestamp and convert the difference to ms
(os.clock() - recv_ts()) * 1000
)
)
end)
frktest.test.on_test_skipped(function(test)
print(STYLE.report(test.name, "skip", 0))
end)
Reporter.init()
end
return setmetatable(ReporterExt, { __index = Reporter })