fix TSamFailedOnly and the output formatting for the mocha-runner

This commit is contained in:
2025-12-25 17:57:05 +01:00
parent 2c2cb35953
commit 20b8cf8009
4 changed files with 203 additions and 21 deletions

View File

@@ -395,12 +395,6 @@ local function run_command(command, opts)
if not results then
return
end
local lines = format_results(results)
if #lines == 0 then
return
end
ensure_output_started()
append_lines(buf, lines)
had_parsed_output = true
if options.track_scope then
if results.failures_all ~= nil then
@@ -409,6 +403,12 @@ local function run_command(command, opts)
state.last_scope_failures = results.failures
end
end
local lines = format_results(results)
if #lines == 0 then
return
end
ensure_output_started()
append_lines(buf, lines)
end
run_cmd(cmd, cwd, {

View File

@@ -445,7 +445,31 @@ function M.new(opts)
local function parse_mocha(output)
local ok, data = pcall(vim.json.decode, output)
if not ok or type(data) ~= "table" then
return nil
local passes = {}
local failures = {}
local skips = {}
for line in (output or ""):gmatch("[^\n]+") do
local ok_line, entry = pcall(vim.json.decode, line)
if ok_line and type(entry) == "table" then
local event = entry.event or entry[1] or entry["1"]
local payload = entry
if not entry.event then
payload = entry[2] or entry["2"] or {}
end
local title = payload.fullTitle or payload.title
if event == "pass" and title then
table.insert(passes, title)
elseif event == "fail" and title then
table.insert(failures, title)
elseif event == "pending" and title then
table.insert(skips, title)
end
end
end
if #passes == 0 and #failures == 0 and #skips == 0 then
return nil
end
return { passes = passes, failures = failures, skips = skips }
end
local passes = {}
local failures = {}
@@ -496,7 +520,7 @@ function M.new(opts)
end
function runner.output_parser()
local state = { raw = {}, done = false }
local state = { raw = {}, done = false, saw_stream = false }
local failures = {}
local skips = {}
return {
@@ -514,28 +538,45 @@ function M.new(opts)
end
if uses_stream then
local ok, data = pcall(vim.json.decode, line)
if ok and type(data) == "table" and data.event then
if data.event == "pass" and data.fullTitle then
if ok and type(data) == "table" then
local event = data.event
local payload = data
if not event then
event = data[1] or data["1"]
payload = data[2] or data["2"] or {}
end
if event == "pass" and payload.fullTitle then
state.saw_stream = true
return {
passes = { data.fullTitle },
passes = { payload.fullTitle },
failures = {},
skips = {},
failures_all = vim.deepcopy(failures),
}
elseif data.event == "fail" and data.fullTitle then
table.insert(failures, data.fullTitle)
elseif event == "fail" and payload.fullTitle then
state.saw_stream = true
table.insert(failures, payload.fullTitle)
return {
passes = {},
failures = { data.fullTitle },
failures = { payload.fullTitle },
skips = {},
failures_all = vim.deepcopy(failures),
}
elseif data.event == "pending" and data.fullTitle then
table.insert(skips, data.fullTitle)
elseif event == "pending" and payload.fullTitle then
state.saw_stream = true
table.insert(skips, payload.fullTitle)
return {
passes = {},
failures = {},
skips = { data.fullTitle },
skips = { payload.fullTitle },
failures_all = vim.deepcopy(failures),
}
elseif event == "start" or event == "end" then
state.saw_stream = true
return {
passes = {},
failures = {},
skips = {},
failures_all = vim.deepcopy(failures),
}
end
@@ -596,7 +637,7 @@ function M.new(opts)
return results
end,
on_complete = function(output, _state)
if state.done then
if state.done or state.saw_stream then
return nil
end
local results = parse_output(output)
@@ -618,8 +659,13 @@ function M.new(opts)
append_args(cmd, runner.json_args)
if runner.framework == "mocha" then
table.insert(cmd, "--grep")
table.insert(cmd, pattern)
if #failures == 1 then
table.insert(cmd, "--fgrep")
table.insert(cmd, failures[1])
else
table.insert(cmd, "--grep")
table.insert(cmd, pattern)
end
else
table.insert(cmd, "-t")
table.insert(cmd, pattern)