local runner = require("test-samurai-jest-runner") local function get_reporter_path() local source = debug.getinfo(runner.build_command, "S").source if source:sub(1, 1) == "@" then source = source:sub(2) end local dir = vim.fs.dirname(source) return vim.fs.normalize(dir .. "/../../reporter/test_samurai_jest_reporter.js") end describe("test-samurai-jest-runner", function() it("detects Jest test files by suffix", function() local bufnr1 = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr1, "/tmp/example.test.js") assert.is_true(runner.is_test_file(bufnr1)) local bufnr2 = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr2, "/tmp/example.spec.tsx") assert.is_true(runner.is_test_file(bufnr2)) local bufnr3 = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr3, "/tmp/example.js") assert.is_false(runner.is_test_file(bufnr3)) end) it("finds nearest test with describe hierarchy", function() local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr, "/tmp/math.test.js") local lines = { "describe('Math', () => {", " test('adds', () => {", " expect(1 + 1).toBe(2)", " })", "})", } vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) local orig_fs_find = vim.fs.find vim.fs.find = function(_, _) return { "/tmp/package.json" } end local spec, err = runner.find_nearest(bufnr, 2, 0) vim.fs.find = orig_fs_find assert.is_nil(err) assert.equals("adds", spec.test_name) assert.equals("Math/adds", spec.full_name) assert.equals("Math adds", spec.jest_name) assert.are.same({ "Math", "adds" }, spec.jest_parts) assert.is_true(spec.file:match("math%.test%.js$") ~= nil) assert.equals("/tmp", spec.cwd) end) it("handles multiline describe and it declarations", function() local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr, "/tmp/multiline.test.js") local lines = { "describe(", " '
',", " () => {", " describe(", " 'renders properly...',", " () => {", " it(", " 'the teaser links',", " async () => {", " expect(true).toBe(true)", " }", " )", " }", " )", " }", ")", } vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) local orig_fs_find = vim.fs.find vim.fs.find = function(_, _) return { "/tmp/package.json" } end local spec, err = runner.find_nearest(bufnr, 9, 0) vim.fs.find = orig_fs_find assert.is_nil(err) assert.equals("the teaser links", spec.test_name) assert.equals("
/renders properly.../the teaser links", spec.full_name) assert.are.same({ "
", "renders properly...", "the teaser links" }, spec.jest_parts) end) it("uses describe block when cursor is between tests", function() local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr, "/tmp/between.test.js") local lines = { "describe('
', () => {", " describe('renders properly...', () => {", " it('the logo', async () => {", " expect(true).toBe(true)", " })", " ", " it('the teaser links', async () => {", " expect(true).toBe(true)", " })", " })", "})", } vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) local orig_fs_find = vim.fs.find vim.fs.find = function(_, _) return { "/tmp/package.json" } end local spec, err = runner.find_nearest(bufnr, 5, 0) vim.fs.find = orig_fs_find assert.is_nil(err) assert.equals("renders properly...", spec.test_name) assert.equals("
/renders properly...", spec.full_name) assert.are.same({ "
", "renders properly..." }, spec.jest_parts) assert.equals("describe", spec.kind) end) it("falls back to file command when outside any describe", function() local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr, "/tmp/outside.test.js") local lines = { "const value = 1", "function helper() { return value }", } vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) local orig_fs_find = vim.fs.find vim.fs.find = function(_, _) return { "/tmp/package.json" } end local spec, err = runner.find_nearest(bufnr, 1, 0) assert.is_nil(err) assert.equals("file", spec.kind) local cmd_spec = runner.build_command(spec) vim.fs.find = orig_fs_find assert.are.same( { "npx", "jest", "--testLocationInResults", "--reporters", get_reporter_path(), "--runTestsByPath", cmd_spec.cmd[#cmd_spec.cmd], }, cmd_spec.cmd ) assert.is_true(cmd_spec.cmd[#cmd_spec.cmd]:match("outside%.test%.js$") ~= nil) end) it("build_command uses npx jest with reporter and pattern", function() local spec = { file = "/tmp/math.test.js", cwd = "/tmp", full_name = "Math/adds", jest_parts = { "Math", "adds" }, } local cmd_spec = runner.build_command(spec) assert.are.same( { "npx", "jest", "--testLocationInResults", "--reporters", get_reporter_path(), "--runTestsByPath", "/tmp/math.test.js", "--testNamePattern", "^.*adds$", }, cmd_spec.cmd ) assert.equals("/tmp", cmd_spec.cwd) end) it("build_command uses prefix pattern for describe blocks", function() local spec = { file = "/tmp/math.test.js", cwd = "/tmp", kind = "describe", jest_parts = { "
", "renders properly..." }, } local cmd_spec = runner.build_command(spec) local pattern = cmd_spec.cmd[#cmd_spec.cmd] assert.are.same( { "npx", "jest", "--testLocationInResults", "--reporters", get_reporter_path(), "--runTestsByPath", "/tmp/math.test.js", "--testNamePattern", pattern, }, cmd_spec.cmd ) assert.is_true(pattern:find("Header", 1, true) ~= nil) assert.is_true(pattern:find("renders", 1, true) ~= nil) assert.is_true(pattern:find("properly", 1, true) ~= nil) assert.is_true(pattern:sub(-2) == ".*") end) it("build_file_command scopes to file", function() local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr, "/tmp/project/foo.test.ts") local orig_fs_find = vim.fs.find vim.fs.find = function(_, _) return { "/tmp/project/package.json" } end local cmd_spec = runner.build_file_command(bufnr) vim.fs.find = orig_fs_find assert.are.same( { "npx", "jest", "--testLocationInResults", "--reporters", get_reporter_path(), "--runTestsByPath", "/tmp/project/foo.test.ts", }, cmd_spec.cmd ) assert.equals("/tmp/project", cmd_spec.cwd) end) it("build_all_command runs project tests", function() local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr, "/tmp/project/bar.test.ts") local orig_fs_find = vim.fs.find vim.fs.find = function(_, _) return { "/tmp/project/package.json" } end local cmd_spec = runner.build_all_command(bufnr) vim.fs.find = orig_fs_find assert.are.same( { "npx", "jest", "--testLocationInResults", "--reporters", get_reporter_path(), }, cmd_spec.cmd ) assert.equals("/tmp/project", cmd_spec.cwd) end) it("build_failed_command narrows to failed tests", function() local last_command = { cmd = { "npx", "jest", "--testLocationInResults", "--reporters", get_reporter_path(), "--runTestsByPath", "/tmp/math.test.js", "--testNamePattern", "^Old$", }, cwd = "/tmp", } local failures = { "Math/adds", "edge (1+1)" } runner.parse_results(table.concat({ "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/adds", jestName = "Math adds", status = "failed", }), "TSAMURAI_RESULT " .. vim.json.encode({ name = "edge (1+1)", jestName = "edge (1+1)", status = "failed", }), }, "\n")) local cmd_spec = runner.build_failed_command(last_command, failures, "file") local pattern = cmd_spec.cmd[#cmd_spec.cmd] assert.are.same( { "npx", "jest", "--testLocationInResults", "--reporters", get_reporter_path(), "--runTestsByPath", "/tmp/math.test.js", "--testNamePattern", pattern, }, cmd_spec.cmd ) assert.is_true(pattern:match("%^Math adds%$") ~= nil) assert.is_true(pattern:match("edge") ~= nil) assert.is_true(pattern:find("\\(1", 1, true) ~= nil) assert.is_true(pattern:find("\\+1", 1, true) ~= nil) assert.equals("/tmp", cmd_spec.cwd) end) it("parse_results collects statuses and locations", function() local output = table.concat({ "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/adds", status = "passed", file = "/tmp/math.test.js", location = { line = 3, column = 4 }, }), "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/subtracts", status = "failed", file = "/tmp/math.test.js", location = { line = 10, column = 2 }, }), "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/skipped", status = "skipped", file = "/tmp/math.test.js", location = { line = 20, column = 2 }, }), }, "\n") local results = runner.parse_results(output) assert.are.same({ "Math/adds" }, results.passes) assert.are.same({ "Math/subtracts" }, results.failures) assert.are.same({ "Math/skipped" }, results.skips) assert.are.same({ "Math/adds" }, results.display.passes) assert.are.same({ "Math/subtracts" }, results.display.failures) assert.are.same({ "Math/skipped" }, results.display.skips) end) it("output_parser streams per test case", function() local parser = runner.output_parser() local state = {} local line = "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/adds", status = "failed", file = "/tmp/math.test.js", location = { line = 3, column = 4 }, }) local results = parser.on_line(line, state) assert.are.same({ "Math/adds" }, results.failures) assert.are.same({ "Math/adds" }, results.display.failures) assert.are.same({ "Math/adds" }, results.failures_all) assert.is_nil(parser.on_complete("", state)) end) it("parse_test_output groups output per test", function() local output = table.concat({ "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/adds", status = "failed", output = { "line1", "line2" }, }), "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/adds", status = "failed", output = { "line3" }, }), }, "\n") local results = runner.parse_test_output(output) assert.are.same({ "line1", "line2", "line3" }, results["Math/adds"]) end) it("collect_failed_locations uses cached locations", function() local output = "TSAMURAI_RESULT " .. vim.json.encode({ name = "Math/adds", status = "failed", file = "/tmp/math.test.js", location = { line = 8, column = 2 }, }) runner.parse_results(output) local items = runner.collect_failed_locations({ "Math/adds" }, nil, "file") assert.equals(1, #items) assert.equals("/tmp/math.test.js", items[1].filename) assert.equals(8, items[1].lnum) assert.equals(2, items[1].col) end) end)