fix TSamNearest and fully support jump Keymap
All checks were successful
tests / test (push) Successful in 10s

This commit is contained in:
2026-01-05 08:58:48 +01:00
parent 7218ed0c4c
commit 29ddc34add
5 changed files with 736 additions and 169 deletions

View File

@@ -42,35 +42,82 @@ describe("test-samurai-mocha-runner", function()
end)
end)
it("finds nearest with test > suite > file priority", function()
it("finds nearest with precise scope selection", function()
with_project({
["package.json"] = [[{"devDependencies":{"mocha":"10.0.0"}}]],
}, function(root)
local content = table.concat({
"describe(\"Math\", function() {",
" it(\"adds\", function() {",
" expect(1).to.equal(1)",
" })",
local lines = {
"// #1",
"describe(\"outer\", () => {",
"",
" it(\"subs\", function() {",
" expect(1).to.equal(1)",
" })",
"})",
" it(\"on stage 1\"); // #2",
"",
"const value = 1",
}, "\n")
" it(\"i2\", () => { // #3",
" // #4",
" }); // #5",
"",
" // #6",
" describe(\"level 2\", () => { // #7",
" it(\"i3\", () => {",
" // #10",
" });",
"",
" // #8",
" it(\"i4\", () => {",
" // ...",
" });",
" }); // #9",
"});",
}
local content = table.concat(lines, "\n")
local bufnr = make_buffer(root .. "/test/math.spec.js", content)
local spec_test = assert(runner.find_nearest(bufnr, 3, 0))
assert.equals("test", spec_test.kind)
assert.equals("Math/adds", spec_test.full_name)
local markers = {}
for i, line in ipairs(lines) do
local mark = line:match("//%s*#(%d+)")
if mark then
markers[tonumber(mark)] = i
end
end
local spec_suite = assert(runner.find_nearest(bufnr, 5, 0))
assert.equals("suite", spec_suite.kind)
assert.equals("Math", spec_suite.full_name)
local spec_file = assert(runner.find_nearest(bufnr, 11, 0))
local spec_file = assert(runner.find_nearest(bufnr, markers[1], 0))
assert.equals("file", spec_file.kind)
local spec_pending = assert(runner.find_nearest(bufnr, markers[2], 0))
assert.equals("test", spec_pending.kind)
assert.equals("outer/on stage 1", spec_pending.full_name)
local spec_i2_line = assert(runner.find_nearest(bufnr, markers[3], 0))
assert.equals("test", spec_i2_line.kind)
assert.equals("outer/i2", spec_i2_line.full_name)
local spec_i2_body = assert(runner.find_nearest(bufnr, markers[4], 0))
assert.equals("test", spec_i2_body.kind)
assert.equals("outer/i2", spec_i2_body.full_name)
local spec_i2_close = assert(runner.find_nearest(bufnr, markers[5], 0))
assert.equals("test", spec_i2_close.kind)
assert.equals("outer/i2", spec_i2_close.full_name)
local spec_outer = assert(runner.find_nearest(bufnr, markers[6], 0))
assert.equals("suite", spec_outer.kind)
assert.equals("outer", spec_outer.full_name)
local spec_inner_line = assert(runner.find_nearest(bufnr, markers[7], 0))
assert.equals("suite", spec_inner_line.kind)
assert.equals("outer/level 2", spec_inner_line.full_name)
local spec_inner_body = assert(runner.find_nearest(bufnr, markers[8], 0))
assert.equals("suite", spec_inner_body.kind)
assert.equals("outer/level 2", spec_inner_body.full_name)
local spec_inner_close = assert(runner.find_nearest(bufnr, markers[9], 0))
assert.equals("suite", spec_inner_close.kind)
assert.equals("outer/level 2", spec_inner_close.full_name)
local spec_i3 = assert(runner.find_nearest(bufnr, markers[10], 0))
assert.equals("test", spec_i3.kind)
assert.equals("outer/level 2/i3", spec_i3.full_name)
end)
end)
@@ -92,7 +139,7 @@ describe("test-samurai-mocha-runner", function()
assert.are.same({ "echo", "no package.json found" }, command.cmd)
end)
it("builds command with mocha json-stream and grep", function()
it("builds command with custom ui and reporter and grep", function()
with_project({
["package.json"] = [[{"devDependencies":{"mocha":"10.0.0"}}]],
}, function(root)
@@ -106,8 +153,13 @@ describe("test-samurai-mocha-runner", function()
local command = runner.build_command(spec)
assert.equals("npx", command.cmd[1])
assert.equals("mocha", command.cmd[2])
assert.is_true(vim.tbl_contains(command.cmd, "--ui"))
assert.is_true(vim.tbl_contains(command.cmd, "bdd-with-location"))
assert.is_true(vim.tbl_contains(command.cmd, "--require"))
assert.is_true(vim.tbl_contains(command.cmd, "--reporter"))
assert.is_true(vim.tbl_contains(command.cmd, "json-stream"))
local joined = table.concat(command.cmd, " ")
assert.is_true(joined:match("scripts/bdd%-with%-location%.cjs") ~= nil)
assert.is_true(joined:match("scripts/mocha%-ndjson%-reporter%.cjs") ~= nil)
assert.is_true(vim.tbl_contains(command.cmd, "--grep"))
end)
end)
@@ -124,18 +176,49 @@ describe("test-samurai-mocha-runner", function()
it("streams results without duplicates", function()
local output_lines = {
[=["suite",{"title":"Math","fullTitle":"Math"}]=],
[=["pass",{"title":"adds","fullTitle":"Math adds","file":"/tmp/math.spec.js"}]=],
[=["pass",{"title":"adds","fullTitle":"Math adds","file":"/tmp/math.spec.js"}]=],
[=["fail",{"title":"subs","fullTitle":"Math subs","file":"/tmp/math.spec.js","err":"oops","stack":"Error: oops\n at Context.<anonymous> (/tmp/math.spec.js:10:5)"}]=],
[=["pending",{"title":"skips","fullTitle":"Math skips","file":"/tmp/math.spec.js"}]=],
[=["suite end",{"title":"Math","fullTitle":"Math"}]=],
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 = 5, column = 3 },
}),
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 = 5, column = 3 },
}),
vim.json.encode({
event = "test",
status = "failed",
title = "subs",
fullTitle = "Math subs",
titlePath = { "Math", "subs" },
file = "/tmp/math.spec.js",
location = { file = "/tmp/math.spec.js", line = 10, column = 5 },
error = { message = "oops", stack = "Error: oops\n at Context.<anonymous> (/tmp/math.spec.js:10:5)" },
}),
vim.json.encode({
event = "test",
status = "pending",
title = "skips",
fullTitle = "Math skips",
titlePath = { "Math", "skips" },
file = "/tmp/math.spec.js",
location = { file = "/tmp/math.spec.js", line = 15, column = 2 },
}),
}
local parser = runner.output_parser()
local state = {}
local aggregated = { passes = {}, failures = {}, skips = {} }
for _, line in ipairs(output_lines) do
local results = parser.on_line("[" .. line .. "]", state)
local results = parser.on_line(line, state)
if results then
for _, name in ipairs(results.passes or {}) do
table.insert(aggregated.passes, name)
@@ -157,6 +240,16 @@ describe("test-samurai-mocha-runner", function()
assert.equals("/tmp/math.spec.js", items[1].filename)
assert.equals(10, items[1].lnum)
assert.equals(5, items[1].col)
local pass_items = runner.collect_failed_locations({ "Math/adds" }, {}, "nearest")
assert.equals("/tmp/math.spec.js", pass_items[1].filename)
assert.equals(5, pass_items[1].lnum)
assert.equals(3, pass_items[1].col)
local skip_items = runner.collect_failed_locations({ "Math/skips" }, {}, "nearest")
assert.equals("/tmp/math.spec.js", skip_items[1].filename)
assert.equals(15, skip_items[1].lnum)
assert.equals(2, skip_items[1].col)
end)
it("builds failed-only command with grep pattern", function()
@@ -179,21 +272,59 @@ describe("test-samurai-mocha-runner", function()
it("parses results and per-test output", function()
local output = table.concat({
[=["suite",{"title":"Math","fullTitle":"Math"}]=],
[=["fail",{"title":"subs","fullTitle":"Math subs","file":"/tmp/math.spec.js","err":"oops","stack":"Error: oops\n at Context.<anonymous> (/tmp/math.spec.js:10:5)\n at processImmediate"}]=],
[=["suite end",{"title":"Math","fullTitle":"Math"}]=],
vim.json.encode({
event = "test",
status = "failed",
title = "subs",
fullTitle = "Math subs",
titlePath = { "Math", "subs" },
file = "/tmp/math.spec.js",
location = { file = "/tmp/math.spec.js", line = 10, column = 5 },
error = {
message = "oops",
stack = "Error: oops\n at Context.<anonymous> (/tmp/math.spec.js:10:5)\n at processImmediate",
},
}),
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 = 4, column = 2 },
}),
vim.json.encode({
event = "test",
status = "skipped",
title = "skips",
fullTitle = "Math skips",
titlePath = { "Math", "skips" },
file = "/tmp/math.spec.js",
location = { file = "/tmp/math.spec.js", line = 6, column = 2 },
}),
}, "\n")
local results = runner.parse_results("[" .. output:gsub("\n", "]\n[") .. "]")
local results = runner.parse_results(output)
assert.equals("Math/subs", results.failures[1])
local outputs = runner.parse_test_output("[" .. output:gsub("\n", "]\n[") .. "]")
local outputs = runner.parse_test_output(output)
assert.is_true(outputs["Math/subs"] ~= nil)
assert.is_true(outputs["Math/adds"] ~= nil)
assert.is_true(outputs["Math/skips"] ~= nil)
end)
it("does not return results on complete", function()
local parser = runner.output_parser()
local state = {}
local output = "[" .. [=["pass",{"title":"adds","fullTitle":"Math adds","file":"/tmp/math.spec.js"}]=] .. "]"
local output = 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 },
})
local results = parser.on_complete(output, state)
assert.is_nil(results)
end)