fix listing for errors that occurs within beforeEach
All checks were successful
tests / test (push) Successful in 8s

This commit is contained in:
2026-01-08 08:56:13 +01:00
parent 2d5889dce3
commit 2a1bac55b2
4 changed files with 270 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ local runner = {
local default_config = {
all_test_glob = "test/**/*.test.js",
debug_log_path = nil,
}
local config = vim.deepcopy(default_config)
@@ -18,6 +19,18 @@ function runner.setup(opts)
return config
end
local function debug_log_line(label, line)
if not config or not config.debug_log_path then
return
end
local ok, handle = pcall(io.open, config.debug_log_path, "a")
if not ok or not handle then
return
end
handle:write(string.format("%s %s\n", label, line or ""))
handle:close()
end
local function runner_root()
local source = debug.getinfo(1, "S").source
if source:sub(1, 1) == "@" then
@@ -357,8 +370,24 @@ local function build_full_name_from_payload(payload)
return title, payload.fullTitle or title
end
local function build_hook_name_from_payload(payload)
local hook_type = payload.hookType or payload.hook_type or payload.title or "hook"
local hook_label = tostring(hook_type)
local path = normalize_title_path(payload)
if path then
return hook_label .. ": " .. table.concat(path, "/"), hook_label .. " " .. table.concat(path, " ")
end
if payload.fullTitle and payload.fullTitle ~= "" then
return hook_label .. ": " .. payload.fullTitle, hook_label .. " " .. payload.fullTitle
end
if payload.title and payload.title ~= "" then
return hook_label .. ": " .. payload.title, hook_label .. " " .. payload.title
end
return hook_label, hook_label
end
local function normalize_location(location)
if not location then
if not location or type(location) ~= "table" then
return nil
end
local filename = location.file or location.filename
@@ -439,9 +468,80 @@ local function record_ndjson_result(state, status, payload)
return nil
end
local function record_hook_failure(state, payload)
local full_name, mocha_title = build_hook_name_from_payload(payload or {})
if not full_name then
return nil
end
state.mocha_titles[full_name] = mocha_title
if not state.seen.failures[full_name] then
state.seen.failures[full_name] = true
table.insert(state.results.failures, full_name)
end
local location = normalize_location(payload.location)
local output_lines = output_lines_from_payload(payload)
local lines = {}
append_lines(lines, output_lines)
append_lines(lines, build_error_lines(payload.error))
if #lines > 0 then
state.outputs[full_name] = lines
end
if not location and payload.error and payload.error.stack then
location = extract_location(payload.error.stack)
end
if location then
location.text = (payload.error and payload.error.message) or "hook failure"
state.locations[full_name] = location
end
if not state.outputs[full_name] or #state.outputs[full_name] == 0 then
state.outputs[full_name] = { "failed" }
end
return full_name
end
local function decode_ndjson_payload(line)
if not line or line == "" then
return nil
end
local trimmed = vim.trim(line)
if trimmed == "" then
return nil
end
local ok, payload = pcall(vim.json.decode, trimmed)
if ok and type(payload) == "table" then
return payload
end
local start = trimmed:find("{", 1, true)
if not start then
return nil
end
local last = nil
for i = #trimmed, 1, -1 do
if trimmed:sub(i, i) == "}" then
last = i
break
end
end
if not last or last <= start then
return nil
end
local candidate = trimmed:sub(start, last)
ok, payload = pcall(vim.json.decode, candidate)
if ok and type(payload) == "table" then
return payload
end
return nil
end
local function parse_ndjson_line(line, state)
local ok, payload = pcall(vim.json.decode, line)
if not ok or type(payload) ~= "table" then
debug_log_line("on_line_raw", line)
local payload = decode_ndjson_payload(line)
if not payload then
return nil
end
if payload.event == "test" then
@@ -449,6 +549,11 @@ local function parse_ndjson_line(line, state)
if name then
return { event = payload.status, name = name }
end
elseif payload.event == "hook" and payload.status == "failed" then
local name = record_hook_failure(state, payload)
if name then
return { event = "failed", name = name }
end
elseif payload.event == "suite" then
if payload.status == "skipped" or payload.status == "pending" then
local name = record_ndjson_result(state, payload.status, payload)
@@ -678,11 +783,15 @@ function runner.output_parser()
return results
end,
on_complete = function(output, state)
debug_log_line("on_complete_raw", output)
state = ensure_state(state or {})
local parsed = parse_ndjson_output(output)
runner._last_locations = parsed.locations
runner._last_mocha_titles = parsed.mocha_titles
return nil
if #state.results.passes > 0 or #state.results.failures > 0 or #state.results.skips > 0 then
return nil
end
return parsed.results
end,
}
end