detect and use project jest.setup.{j,t}s files
All checks were successful
tests / test (push) Successful in 8s
All checks were successful
tests / test (push) Successful in 8s
This commit is contained in:
@@ -351,8 +351,26 @@ local function setup_path()
|
||||
return vim.fs.normalize(dir .. "/../../reporter/test_samurai_jest_stdout.js")
|
||||
end
|
||||
|
||||
local function base_cmd()
|
||||
return {
|
||||
local function find_jest_setup(cwd)
|
||||
if not cwd or cwd == "" then
|
||||
return nil
|
||||
end
|
||||
local candidates = {
|
||||
cwd .. "/jest.setup.js",
|
||||
cwd .. "/jest.setup.ts",
|
||||
cwd .. "/test/.bin/jest.setup.js",
|
||||
cwd .. "/test/.bin/jest.setup.ts",
|
||||
}
|
||||
for _, candidate in ipairs(candidates) do
|
||||
if vim.fn.filereadable(candidate) == 1 then
|
||||
return candidate
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function base_cmd(cwd)
|
||||
local cmd = {
|
||||
"npx",
|
||||
"jest",
|
||||
"--testLocationInResults",
|
||||
@@ -361,6 +379,12 @@ local function base_cmd()
|
||||
"--setupFilesAfterEnv",
|
||||
setup_path(),
|
||||
}
|
||||
local project_setup = find_jest_setup(cwd)
|
||||
if project_setup then
|
||||
table.insert(cmd, "--setupFilesAfterEnv")
|
||||
table.insert(cmd, project_setup)
|
||||
end
|
||||
return cmd
|
||||
end
|
||||
|
||||
local function parse_result_line(line)
|
||||
@@ -502,15 +526,15 @@ end
|
||||
function runner.build_command(spec)
|
||||
local file = spec.file
|
||||
if not file or file == "" then
|
||||
return { cmd = base_cmd(), cwd = spec.cwd }
|
||||
return { cmd = base_cmd(spec.cwd), cwd = spec.cwd }
|
||||
end
|
||||
if spec.kind == "file" then
|
||||
local cmd = base_cmd()
|
||||
local cmd = base_cmd(spec.cwd)
|
||||
table.insert(cmd, "--runTestsByPath")
|
||||
table.insert(cmd, file)
|
||||
return { cmd = cmd, cwd = spec.cwd }
|
||||
end
|
||||
local cmd = base_cmd()
|
||||
local cmd = base_cmd(spec.cwd)
|
||||
table.insert(cmd, "--runTestsByPath")
|
||||
table.insert(cmd, file)
|
||||
local ok, pattern = pcall(function()
|
||||
@@ -546,7 +570,7 @@ function runner.build_file_command(bufnr)
|
||||
"jest.config.mjs",
|
||||
"jest.config.json",
|
||||
})
|
||||
local cmd = base_cmd()
|
||||
local cmd = base_cmd(cwd)
|
||||
table.insert(cmd, "--runTestsByPath")
|
||||
table.insert(cmd, path)
|
||||
return { cmd = cmd, cwd = cwd }
|
||||
@@ -562,7 +586,7 @@ function runner.build_all_command(bufnr)
|
||||
"jest.config.mjs",
|
||||
"jest.config.json",
|
||||
})
|
||||
local cmd = base_cmd()
|
||||
local cmd = base_cmd(cwd)
|
||||
return { cmd = cmd, cwd = cwd }
|
||||
end
|
||||
|
||||
@@ -571,7 +595,8 @@ function runner.build_failed_command(last_command, failures, _scope_kind)
|
||||
if last_command and last_command.cmd then
|
||||
return { cmd = last_command.cmd, cwd = last_command.cwd }
|
||||
end
|
||||
return { cmd = base_cmd() }
|
||||
local cwd = vim.loop.cwd()
|
||||
return { cmd = base_cmd(cwd), cwd = cwd }
|
||||
end
|
||||
|
||||
local pattern_parts = {}
|
||||
@@ -595,7 +620,7 @@ function runner.build_failed_command(last_command, failures, _scope_kind)
|
||||
end
|
||||
end
|
||||
if #cmd == 0 then
|
||||
cmd = base_cmd()
|
||||
cmd = base_cmd(last_command and last_command.cwd or vim.loop.cwd())
|
||||
end
|
||||
table.insert(cmd, "--testNamePattern")
|
||||
table.insert(cmd, pattern)
|
||||
|
||||
Reference in New Issue
Block a user