finish first MVP with test-runner-detection for Go, Mocha.js, Jest.js and ViTest.js
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
local test_samurai = require("test-samurai")
|
||||
local core = require("test-samurai.core")
|
||||
local util = require("test-samurai.util")
|
||||
|
||||
local orig_find_root = util.find_root
|
||||
local orig_fs_stat = vim.loop.fs_stat
|
||||
local orig_readfile = vim.fn.readfile
|
||||
|
||||
describe("test-samurai core", function()
|
||||
before_each(function()
|
||||
test_samurai.setup()
|
||||
util.find_root = orig_find_root
|
||||
vim.loop.fs_stat = orig_fs_stat
|
||||
vim.fn.readfile = orig_readfile
|
||||
end)
|
||||
|
||||
it("selects Go runner for _test.go files", function()
|
||||
@@ -23,4 +31,43 @@ describe("test-samurai core", function()
|
||||
assert.is_not_nil(runner)
|
||||
assert.equals("js-jest", runner.name)
|
||||
end)
|
||||
|
||||
it("prefers mocha runner when mocha is in package.json", function()
|
||||
util.find_root = function(_, _)
|
||||
return "/tmp/mocha_proj"
|
||||
end
|
||||
|
||||
vim.loop.fs_stat = function(path)
|
||||
if path == "/tmp/mocha_proj/package.json" then
|
||||
return { type = "file" }
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
vim.fn.readfile = function(path)
|
||||
if path == "/tmp/mocha_proj/package.json" then
|
||||
return {
|
||||
"{",
|
||||
' "devDependencies": { "mocha": "^10.0.0" }',
|
||||
"}",
|
||||
}
|
||||
end
|
||||
return {}
|
||||
end
|
||||
|
||||
test_samurai.setup()
|
||||
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/mocha_proj/foo.test.ts")
|
||||
vim.bo[bufnr].filetype = "typescript"
|
||||
|
||||
local runner = core.get_runner_for_buf(bufnr)
|
||||
|
||||
util.find_root = orig_find_root
|
||||
vim.loop.fs_stat = orig_fs_stat
|
||||
vim.fn.readfile = orig_readfile
|
||||
|
||||
assert.is_not_nil(runner)
|
||||
assert.equals("js-mocha", runner.name)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -3,17 +3,17 @@ local go_runner = require("test-samurai.runners.go")
|
||||
describe("test-samurai go runner", function()
|
||||
it("detects Go test files by suffix", function()
|
||||
local bufnr1 = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr1, "/tmp/foo_test.go")
|
||||
vim.api.nvim_buf_set_name(bufnr1, "/tmp/go_suffix_test.go")
|
||||
assert.is_true(go_runner.is_test_file(bufnr1))
|
||||
|
||||
local bufnr2 = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr2, "/tmp/foo.go")
|
||||
vim.api.nvim_buf_set_name(bufnr2, "/tmp/go_main.go")
|
||||
assert.is_false(go_runner.is_test_file(bufnr2))
|
||||
end)
|
||||
|
||||
it("finds subtest when cursor is inside t.Run block", function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/foo_test.go")
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/go_subtest_test.go")
|
||||
local lines = {
|
||||
"package main",
|
||||
"import \"testing\"",
|
||||
@@ -44,13 +44,13 @@ describe("test-samurai go runner", function()
|
||||
assert.is_not_nil(spec)
|
||||
assert.equals("TestFoo/first", spec.test_path)
|
||||
assert.equals("subtest", spec.scope)
|
||||
assert.equals("/tmp/foo_test.go", spec.file)
|
||||
assert.equals("/tmp", spec.cwd)
|
||||
assert.is_true(spec.file:match("go_subtest_test%.go$") ~= nil)
|
||||
assert.is_true(spec.cwd:match("tmp$") ~= nil)
|
||||
end)
|
||||
|
||||
it("falls back to whole test function when between subtests", function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/foo_test.go")
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/go_between_test.go")
|
||||
local lines = {
|
||||
"package main",
|
||||
"import \"testing\"",
|
||||
@@ -81,5 +81,35 @@ describe("test-samurai go runner", function()
|
||||
assert.is_not_nil(spec)
|
||||
assert.equals("TestFoo", spec.test_path)
|
||||
assert.equals("function", spec.scope)
|
||||
assert.is_true(spec.file:match("go_between_test%.go$") ~= nil)
|
||||
assert.is_true(spec.cwd:match("tmp$") ~= nil)
|
||||
end)
|
||||
|
||||
it("build_command uses current package and correct run pattern", function()
|
||||
local spec_sub = {
|
||||
file = "/tmp/project/pkg/foo_test.go",
|
||||
cwd = "/tmp/project",
|
||||
test_path = "TestFoo/first",
|
||||
scope = "subtest",
|
||||
}
|
||||
|
||||
local cmd_spec_sub = go_runner.build_command(spec_sub)
|
||||
assert.are.same(
|
||||
{ "go", "test", "-v", "./pkg", "-run", "^TestFoo%/first$" },
|
||||
cmd_spec_sub.cmd
|
||||
)
|
||||
|
||||
local spec_func = {
|
||||
file = "/tmp/project/foo_test.go",
|
||||
cwd = "/tmp/project",
|
||||
test_path = "TestFoo",
|
||||
scope = "function",
|
||||
}
|
||||
|
||||
local cmd_spec_func = go_runner.build_command(spec_func)
|
||||
assert.are.same(
|
||||
{ "go", "test", "-v", "./", "-run", "^TestFoo$|^TestFoo/" },
|
||||
cmd_spec_func.cmd
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -1,29 +1,30 @@
|
||||
local jest = require("test-samurai.runners.js-jest")
|
||||
local mocha = require("test-samurai.runners.js-mocha")
|
||||
|
||||
describe("test-samurai js runner (jest)", function()
|
||||
it("detects JS/TS test files by name and filetype", function()
|
||||
local bufnr1 = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr1, "/tmp/foo.test.ts")
|
||||
vim.api.nvim_buf_set_name(bufnr1, "/tmp/foo_detect.test.ts")
|
||||
vim.bo[bufnr1].filetype = "typescript"
|
||||
assert.is_true(jest.is_test_file(bufnr1))
|
||||
|
||||
local bufnr2 = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr2, "/tmp/foo.ts")
|
||||
vim.api.nvim_buf_set_name(bufnr2, "/tmp/foo_detect.ts")
|
||||
vim.bo[bufnr2].filetype = "typescript"
|
||||
assert.is_false(jest.is_test_file(bufnr2))
|
||||
end)
|
||||
|
||||
it("finds nearest it() call as test name", function()
|
||||
it("finds nearest it() call as test name and builds full_name when cursor is inside the test", function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/foo.test.ts")
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/foo_nearest.test.ts")
|
||||
vim.bo[bufnr].filetype = "typescript"
|
||||
local lines = {
|
||||
"describe(\"outer\", function() {",
|
||||
" it(\"inner 1\", function() {",
|
||||
'describe("outer", function() {',
|
||||
' it("inner 1", function() {',
|
||||
" -- inside 1",
|
||||
" })",
|
||||
"",
|
||||
" it(\"inner 2\", function() {",
|
||||
' it("inner 2", function() {',
|
||||
" -- inside 2",
|
||||
" })",
|
||||
"})",
|
||||
@@ -31,10 +32,85 @@ describe("test-samurai js runner (jest)", function()
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
|
||||
local orig_fs_find = vim.fs.find
|
||||
vim.fs.find = function(markers, opts)
|
||||
vim.fs.find = function(names, opts)
|
||||
return { "/tmp/package.json" }
|
||||
end
|
||||
|
||||
-- Cursor in der zweiten it()-Body
|
||||
local row_inside_second = 6 -- 0-basiert -> Zeile mit "-- inside 2"
|
||||
local spec, err = jest.find_nearest(bufnr, row_inside_second, 0)
|
||||
|
||||
vim.fs.find = orig_fs_find
|
||||
|
||||
assert.is_nil(err)
|
||||
assert.is_not_nil(spec)
|
||||
assert.equals("inner 2", spec.test_name)
|
||||
assert.equals("outer inner 2", spec.full_name)
|
||||
assert.equals("jest", spec.framework)
|
||||
assert.is_true(spec.file:match("foo_nearest%.test%.ts$") ~= nil)
|
||||
assert.is_true(spec.cwd:match("tmp$") ~= nil)
|
||||
|
||||
local cmd_spec = jest.build_command(spec)
|
||||
assert.are.same({ "npx", "jest", spec.file, "-t", "inner 2" }, cmd_spec.cmd)
|
||||
end)
|
||||
|
||||
it("returns describe block when cursor is between it() calls", function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/foo_between.test.ts")
|
||||
vim.bo[bufnr].filetype = "typescript"
|
||||
local lines = {
|
||||
'describe("outer", function() {',
|
||||
' it("inner 1", function() {',
|
||||
" -- inside 1",
|
||||
" })",
|
||||
"",
|
||||
' it("inner 2", function() {',
|
||||
" -- inside 2",
|
||||
" })",
|
||||
"})",
|
||||
}
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
|
||||
local orig_fs_find = vim.fs.find
|
||||
vim.fs.find = function(names, opts)
|
||||
return { "/tmp/package.json" }
|
||||
end
|
||||
|
||||
-- Cursor auf der Leerzeile zwischen den beiden it()-Blöcken
|
||||
local row_between = 4 -- 0-basiert -> leere Zeile zwischen den Tests
|
||||
local spec, err = jest.find_nearest(bufnr, row_between, 0)
|
||||
|
||||
vim.fs.find = orig_fs_find
|
||||
|
||||
assert.is_nil(err)
|
||||
assert.is_not_nil(spec)
|
||||
assert.equals("outer", spec.test_name)
|
||||
assert.equals("outer", spec.full_name)
|
||||
assert.equals("jest", spec.framework)
|
||||
end)
|
||||
|
||||
it("treats jest.config in test/.bin as project root parent", function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "/tmp/foo_binroot.test.ts")
|
||||
vim.bo[bufnr].filetype = "typescript"
|
||||
local lines = {
|
||||
'describe("outer", function() {',
|
||||
' it("inner 1", function() {',
|
||||
" -- inside 1",
|
||||
" })",
|
||||
"",
|
||||
' it("inner 2", function() {',
|
||||
" -- inside 2",
|
||||
" })",
|
||||
"})",
|
||||
}
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
|
||||
local orig_fs_find = vim.fs.find
|
||||
vim.fs.find = function(names, opts)
|
||||
return { "/tmp/test/.bin/jest.config.js" }
|
||||
end
|
||||
|
||||
local row_inside_second = 6
|
||||
local spec, err = jest.find_nearest(bufnr, row_inside_second, 0)
|
||||
|
||||
@@ -42,12 +118,25 @@ describe("test-samurai js runner (jest)", function()
|
||||
|
||||
assert.is_nil(err)
|
||||
assert.is_not_nil(spec)
|
||||
assert.equals("inner 2", spec.test_name)
|
||||
assert.equals("jest", spec.framework)
|
||||
assert.equals("/tmp/foo.test.ts", spec.file)
|
||||
assert.equals("/tmp", spec.cwd)
|
||||
|
||||
local cmd_spec = jest.build_command(spec)
|
||||
assert.are.same({ "npx", "jest", "/tmp/foo.test.ts", "-t", "inner 2" }, cmd_spec.cmd)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("test-samurai js runner (mocha)", function()
|
||||
it("builds mocha command with fgrep and full test title", function()
|
||||
local spec = {
|
||||
file = "/tmp/project/test/foo_nearest.test.ts",
|
||||
cwd = "/tmp/project",
|
||||
test_name = "inner 2",
|
||||
full_name = "outer inner 2",
|
||||
}
|
||||
|
||||
local cmd_spec = mocha.build_command(spec)
|
||||
|
||||
assert.are.same(
|
||||
{ "npx", "mocha", "--fgrep", "outer inner 2", spec.file },
|
||||
cmd_spec.cmd
|
||||
)
|
||||
assert.equals("/tmp/project", cmd_spec.cwd)
|
||||
end)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user