local test_samurai = require("test-samurai") local core = require("test-samurai.core") describe("test-samurai public API", function() it("delegates show_output to core", function() local called = false local orig = core.show_output core.show_output = function() called = true end test_samurai.show_output() core.show_output = orig assert.is_true(called) end) it("delegates test_nearest to core.run_nearest", function() local called = false local orig = core.run_nearest core.run_nearest = function() called = true end test_samurai.test_nearest() core.run_nearest = orig assert.is_true(called) end) it("delegates test_file to core.run_file", function() local called = false local orig = core.run_file core.run_file = function() called = true end test_samurai.test_file() core.run_file = orig assert.is_true(called) end) it("delegates test_all to core.run_all", function() local called = false local orig = core.run_all core.run_all = function() called = true end test_samurai.test_all() core.run_all = orig assert.is_true(called) end) it("delegates test_last to core.run_last", function() local called = false local orig = core.run_last core.run_last = function() called = true end test_samurai.test_last() core.run_last = orig assert.is_true(called) end) it("delegates test_failed_only to core.run_failed_only", function() local called = false local orig = core.run_failed_only core.run_failed_only = function() called = true end test_samurai.test_failed_only() core.run_failed_only = orig assert.is_true(called) end) end) describe("test-samurai output formatting", function() before_each(function() test_samurai.setup() end) it("formats JSON output as PASS/FAIL lines", function() local json = vim.json.encode({ testResults = { { assertionResults = { { status = "passed", title = "inner 1", fullName = "outer inner 1" }, { status = "skipped", title = "inner skip", fullName = "outer inner skip" }, { status = "failed", title = "inner 2", fullName = "outer inner 2" }, }, }, }, }) local orig_jobstart = vim.fn.jobstart vim.fn.jobstart = function(_cmd, opts) if opts and opts.on_stdout then opts.on_stdout(1, { json }, 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_format.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_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_skip = false local has_fail = false for _, line in ipairs(lines) do if line == "[ PASS ] - outer inner 1" then has_pass = true elseif line == "[ SKIP ] - outer inner skip" then has_skip = true elseif line == "[ FAIL ] - outer inner 2" then has_fail = true end end assert.is_true(has_pass) assert.is_true(has_skip) assert.is_true(has_fail) end) it("formats TAP output as PASS/FAIL lines", function() test_samurai.setup({ runner_modules = { "test-samurai.runners.js-vitest", }, }) local orig_jobstart = vim.fn.jobstart vim.fn.jobstart = function(_cmd, opts) if opts and opts.on_stdout then 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", }, 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_format.tap.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_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_skip = false local has_fail = false for _, line in ipairs(lines) do if line == "[ PASS ] - outer inner 1" then has_pass = true elseif line == "[ SKIP ] - outer inner skip" then has_skip = true elseif line == "[ FAIL ] - outer inner 2" then has_fail = true end end assert.is_true(has_pass) assert.is_true(has_skip) assert.is_true(has_fail) end) end)