fix TSamFailedOnly and the output formatting for the jest-runner

This commit is contained in:
2025-12-25 18:53:31 +01:00
parent 20b8cf8009
commit a6e51f280f
10 changed files with 617 additions and 106 deletions

View File

@@ -151,11 +151,11 @@ describe("test-samurai output formatting", function()
local has_skip = false
local has_fail = false
for _, line in ipairs(lines) do
if line == "[ PASS ] - outer inner 1" then
if line == "[ PASS ] - inner 1" then
has_pass = true
elseif line == "[ SKIP ] - outer inner skip" then
elseif line == "[ SKIP ] - inner skip" then
has_skip = true
elseif line == "[ FAIL ] - outer inner 2" then
elseif line == "[ FAIL ] - inner 2" then
has_fail = true
end
end
@@ -218,6 +218,186 @@ describe("test-samurai output formatting", function()
assert.is_false(has_raw_json)
end)
it("formats multi-line jest JSON output and ignores non-JSON lines", function()
local lines_out = {
"Some log line",
"{",
' "testResults": [',
" {",
' "assertionResults": [',
' {"status":"passed","title":"inner 1","fullName":"outer inner 1"}',
" ]",
" }",
" ]",
"}",
}
local orig_jobstart = vim.fn.jobstart
vim.fn.jobstart = function(_cmd, opts)
if opts and opts.on_stdout then
opts.on_stdout(1, lines_out, nil)
end
if opts and opts.on_exit then
opts.on_exit(1, 0, nil)
end
return 1
end
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, "/tmp/output_multiline_json.test.ts")
vim.bo[bufnr].filetype = "typescript"
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
'describe("outer", function() {',
' it("inner 1", function() {',
" -- inside 1",
" })",
"})",
})
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_win_set_cursor(0, { 3, 0 })
core.run_nearest()
local out_buf = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(out_buf, 0, -1, false)
vim.fn.jobstart = orig_jobstart
local has_pass = false
local has_raw_json = false
for _, line in ipairs(lines) do
if line == "[ PASS ] - inner 1" then
has_pass = true
elseif line == ' "testResults": [' then
has_raw_json = true
end
end
assert.is_true(has_pass)
assert.is_false(has_raw_json)
end)
it("formats jest verbose output as PASS/FAIL/SKIP lines", function()
local check = string.char(0xE2, 0x9C, 0x93)
local cross = string.char(0xE2, 0x9C, 0x95)
local circle = string.char(0xE2, 0x97, 0x8B)
local orig_jobstart = vim.fn.jobstart
vim.fn.jobstart = function(_cmd, opts)
if opts and opts.on_stdout then
opts.on_stdout(1, {
" PASS /tmp/output_verbose.test.ts",
" " .. check .. " inner 1 (5 ms)",
" " .. circle .. " inner skip (skipped)",
" " .. cross .. " inner 2 (1 ms)",
}, nil)
end
if opts and opts.on_exit then
opts.on_exit(1, 1, nil)
end
return 1
end
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, "/tmp/output_verbose.test.ts")
vim.bo[bufnr].filetype = "typescript"
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
'describe("outer", function() {',
' it("inner 1", function() {',
" -- inside 1",
" })",
"",
' it("inner 2", function() {',
" -- inside 2",
" })",
"})",
})
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_win_set_cursor(0, { 7, 0 })
core.run_all()
local out_buf = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(out_buf, 0, -1, false)
vim.fn.jobstart = orig_jobstart
local has_pass = false
local has_skip = false
local has_fail = false
local has_raw_verbose = false
for _, line in ipairs(lines) do
if line == "[ PASS ] - inner 1" then
has_pass = true
elseif line == "[ SKIP ] - inner skip" then
has_skip = true
elseif line == "[ FAIL ] - inner 2" then
has_fail = true
elseif line:match("^%s*PASS%s+") then
has_raw_verbose = true
end
end
assert.is_true(has_pass)
assert.is_true(has_skip)
assert.is_true(has_fail)
assert.is_false(has_raw_verbose)
end)
it("formats go subtests as short names", function()
local json_line = vim.json.encode({
Action = "pass",
Test = "TestHandleGet/returns_200",
})
local orig_jobstart = vim.fn.jobstart
vim.fn.jobstart = function(_cmd, opts)
if opts and opts.on_stdout then
opts.on_stdout(1, { json_line }, nil)
end
if opts and opts.on_exit then
opts.on_exit(1, 0, nil)
end
return 1
end
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, "/tmp/output_go_short_test.go")
vim.bo[bufnr].filetype = "go"
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
"package main",
"import \"testing\"",
"",
"func TestHandleGet(t *testing.T) {",
" t.Run(\"returns_200\", func(t *testing.T) {",
" -- inside test",
" })",
"}",
})
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_win_set_cursor(0, { 6, 0 })
core.run_nearest()
local out_buf = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(out_buf, 0, -1, false)
vim.fn.jobstart = orig_jobstart
local has_pass = false
local has_raw_json = false
for _, line in ipairs(lines) do
if line == "[ PASS ] - returns_200" then
has_pass = true
elseif line == json_line then
has_raw_json = true
end
end
assert.is_true(has_pass)
assert.is_false(has_raw_json)
end)
it("does not print raw JSON output for mocha json-stream", function()
test_samurai.setup({
runner_modules = {
@@ -323,7 +503,7 @@ describe("test-samurai output formatting", function()
local has_pass = false
local has_raw_json = false
for _, line in ipairs(lines) do
if line == "[ PASS ] - API :: /brands... GET: /" then
if line == "[ PASS ] - GET: /" then
has_pass = true
elseif line == pass_line then
has_raw_json = true
@@ -419,10 +599,12 @@ describe("test-samurai output formatting", function()
local pass_line = vim.json.encode({
event = "pass",
title = "inner 1",
fullTitle = "outer inner 1",
})
local fail_line = vim.json.encode({
event = "fail",
title = "inner 2",
fullTitle = "outer inner 2",
})
@@ -475,9 +657,9 @@ describe("test-samurai output formatting", function()
local has_pass = false
local has_fail = false
for _, line in ipairs(lines) do
if line == "[ PASS ] - outer inner 1" then
if line == "[ PASS ] - inner 1" then
has_pass = true
elseif line == "[ FAIL ] - outer inner 2" then
elseif line == "[ FAIL ] - inner 2" then
has_fail = true
end
end
@@ -516,9 +698,9 @@ describe("test-samurai output formatting", function()
opts.on_stdout(1, {
"TAP version 13",
"1..2",
"ok 1 - outer inner 1 # time=1.00ms",
"ok 2 - outer inner skip # SKIP not now",
"not ok 2 - outer inner 2 # time=2.00ms",
"ok 1 - outer > inner 1 # time=1.00ms",
"ok 2 - outer > inner skip # SKIP not now",
"not ok 2 - outer > inner 2 # time=2.00ms",
}, nil)
end
if opts and opts.on_exit then
@@ -556,11 +738,11 @@ describe("test-samurai output formatting", function()
local has_skip = false
local has_fail = false
for _, line in ipairs(lines) do
if line == "[ PASS ] - outer inner 1" then
if line == "[ PASS ] - inner 1" then
has_pass = true
elseif line == "[ SKIP ] - outer inner skip" then
elseif line == "[ SKIP ] - inner skip" then
has_skip = true
elseif line == "[ FAIL ] - outer inner 2" then
elseif line == "[ FAIL ] - inner 2" then
has_fail = true
end
end