This commit is contained in:
@@ -38,6 +38,47 @@ local function find_root(path, markers)
|
||||
return vim.fs.dirname(found[1])
|
||||
end
|
||||
|
||||
local function find_nearest_package_root(path)
|
||||
if not path or path == "" then
|
||||
return nil
|
||||
end
|
||||
local dir = vim.fs.dirname(path)
|
||||
local prev = nil
|
||||
while dir and dir ~= prev do
|
||||
local candidate = dir .. "/package.json"
|
||||
if vim.fn.filereadable(candidate) == 1 then
|
||||
return dir
|
||||
end
|
||||
prev = dir
|
||||
dir = vim.fs.dirname(dir)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function read_package_json(root)
|
||||
if not root then
|
||||
return nil
|
||||
end
|
||||
local ok, lines = pcall(vim.fn.readfile, root .. "/package.json")
|
||||
if not ok then
|
||||
return nil
|
||||
end
|
||||
local ok_decode, data = pcall(vim.json.decode, table.concat(lines, "\n"))
|
||||
if not ok_decode then
|
||||
return nil
|
||||
end
|
||||
return data
|
||||
end
|
||||
|
||||
local function has_jest_dependency(pkg)
|
||||
if not pkg or type(pkg) ~= "table" then
|
||||
return false
|
||||
end
|
||||
local deps = pkg.dependencies or {}
|
||||
local dev_deps = pkg.devDependencies or {}
|
||||
return deps.jest ~= nil or dev_deps.jest ~= nil
|
||||
end
|
||||
|
||||
local function count_char(line, ch)
|
||||
local count = 0
|
||||
for i = 1, #line do
|
||||
@@ -346,7 +387,12 @@ end
|
||||
|
||||
function runner.is_test_file(bufnr)
|
||||
local path = get_buf_path(bufnr)
|
||||
return test_file_path(path)
|
||||
if not test_file_path(path) then
|
||||
return false
|
||||
end
|
||||
local root = find_nearest_package_root(path)
|
||||
local pkg = read_package_json(root)
|
||||
return has_jest_dependency(pkg)
|
||||
end
|
||||
|
||||
function runner.find_nearest(bufnr, row, _col)
|
||||
@@ -354,7 +400,7 @@ function runner.find_nearest(bufnr, row, _col)
|
||||
if not path or path == "" then
|
||||
return nil, "no file name"
|
||||
end
|
||||
if not test_file_path(path) then
|
||||
if not runner.is_test_file(bufnr) then
|
||||
return nil, "not a jest test file"
|
||||
end
|
||||
local lines = get_buf_lines(bufnr)
|
||||
|
||||
Reference in New Issue
Block a user