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

@@ -267,6 +267,92 @@ describe("test-samurai-mocha-runner", function()
assert.equals(2, skip_items[1].col)
end)
it("captures hook failures in results and output", function()
local output = vim.json.encode({
event = "hook",
status = "failed",
hookType = "beforeEach",
titlePath = { "Math", "adds" },
fullTitle = "Math adds",
file = "/tmp/math.spec.js",
error = {
message = "boom",
stack = "Error: boom\n at Context.<anonymous> (/tmp/math.spec.js:5:3)",
},
})
local results = runner.parse_results(output)
assert.are.same({ "beforeEach: Math/adds" }, results.failures)
local outputs = runner.parse_test_output(output)
assert.is_table(outputs["beforeEach: Math/adds"])
assert.is_true(table.concat(outputs["beforeEach: Math/adds"], "\n"):match("boom") ~= nil)
local items = runner.collect_failed_locations(results.failures)
assert.are.same({
{ filename = "/tmp/math.spec.js", lnum = 5, col = 3, text = "boom" },
}, items)
end)
it("parses mocha hook failures emitted as test events", function()
local output = vim.json.encode({
event = "test",
status = "failed",
title = "\"before each\" hook for \"findAllInquiries()\"",
fullTitle = "BlacklistRepository... \"before each\" hook for \"findAllInquiries()\"",
titlePath = { "BlacklistRepository...", "\"before each\" hook for \"findAllInquiries()\"" },
file = "/tmp/BlacklistRepository.test.js",
location = nil,
error = {
message = "missing dbName",
stack = "Error: missing dbName\n at Context.<anonymous> (/tmp/BlacklistRepository.test.js:10:25)",
},
})
local results = runner.parse_results(output)
assert.are.same({
"BlacklistRepository.../\"before each\" hook for \"findAllInquiries()\"",
}, results.failures)
local outputs = runner.parse_test_output(output)
assert.is_true(table.concat(outputs[results.failures[1]], "\n"):match("missing dbName") ~= nil)
local items = runner.collect_failed_locations(results.failures)
assert.are.same({
{ filename = "/tmp/BlacklistRepository.test.js", lnum = 10, col = 25, text = "missing dbName" },
}, items)
end)
it("handles null location fields in ndjson output", function()
local output = [[{"event":"test","status":"failed","title":"\"before each\" hook for \"findAllInquiries()\"","fullTitle":"BlacklistRepository... \"before each\" hook for \"findAllInquiries()\"","titlePath":["BlacklistRepository...","\"before each\" hook for \"findAllInquiries()\""],"file":"/tmp/BlacklistRepository.test.js","location":null,"error":{"name":"Error","message":"missing dbName","stack":"Error: missing dbName\n at Context.<anonymous> (/tmp/BlacklistRepository.test.js:10:25)"}}]]
local results = runner.parse_results(output)
assert.are.same({
"BlacklistRepository.../\"before each\" hook for \"findAllInquiries()\"",
}, results.failures)
end)
it("writes debug logs when configured", function()
local log_path = vim.fn.tempname()
runner.setup({ debug_log_path = log_path })
local parser = runner.output_parser()
local state = {}
local line = vim.json.encode({
event = "test",
status = "passed",
title = "adds",
fullTitle = "Math adds",
titlePath = { "Math", "adds" },
file = "/tmp/math.spec.js",
location = { file = "/tmp/math.spec.js", line = 3, column = 2 },
})
parser.on_line(line, state)
parser.on_complete(line, state)
local lines = vim.fn.readfile(log_path)
assert.is_true(#lines >= 2)
assert.is_true(lines[1]:match("on_line_raw") ~= nil)
assert.is_true(lines[2]:match("on_complete_raw") ~= nil)
end)
it("builds failed-only command with grep pattern", function()
runner._last_mocha_titles = {
["Math/adds"] = "Math adds",
@@ -334,7 +420,7 @@ describe("test-samurai-mocha-runner", function()
assert.equals("skipped", outputs["Math/skips"][1])
end)
it("does not return results on complete", function()
it("returns results on complete when no streaming state exists", function()
local parser = runner.output_parser()
local state = {}
local output = vim.json.encode({
@@ -347,6 +433,23 @@ describe("test-samurai-mocha-runner", function()
location = { file = "/tmp/math.spec.js", line = 3, column = 2 },
})
local results = parser.on_complete(output, state)
assert.are.same({ "Math/adds" }, results.passes)
end)
it("does not return results on complete when streaming already handled", function()
local parser = runner.output_parser()
local state = {}
local line = vim.json.encode({
event = "test",
status = "passed",
title = "adds",
fullTitle = "Math adds",
titlePath = { "Math", "adds" },
file = "/tmp/math.spec.js",
location = { file = "/tmp/math.spec.js", line = 3, column = 2 },
})
parser.on_line(line, state)
local results = parser.on_complete(line, state)
assert.is_nil(results)
end)
end)