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:
@@ -62,3 +62,4 @@ Use the standard `test-samurai.nvim` commands (e.g. `TSamNearest`, `TSamFile`, `
|
|||||||
- The reporter lives at `reporter/test_samurai_jest_reporter.js` and is loaded via `--reporters`.
|
- The reporter lives at `reporter/test_samurai_jest_reporter.js` and is loaded via `--reporters`.
|
||||||
- Test names are reported as `Describe/It` for grouping in the listing.
|
- Test names are reported as `Describe/It` for grouping in the listing.
|
||||||
- Stdout capture uses `reporter/test_samurai_jest_stdout.js` via `--setupFilesAfterEnv` and tags output as `TSAMURAI_STDOUT`.
|
- Stdout capture uses `reporter/test_samurai_jest_stdout.js` via `--setupFilesAfterEnv` and tags output as `TSAMURAI_STDOUT`.
|
||||||
|
- If present, `jest.setup.js` or `jest.setup.ts` from the project root or `test/.bin` is appended via `--setupFilesAfterEnv`.
|
||||||
|
|||||||
@@ -351,8 +351,26 @@ local function setup_path()
|
|||||||
return vim.fs.normalize(dir .. "/../../reporter/test_samurai_jest_stdout.js")
|
return vim.fs.normalize(dir .. "/../../reporter/test_samurai_jest_stdout.js")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function base_cmd()
|
local function find_jest_setup(cwd)
|
||||||
return {
|
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",
|
"npx",
|
||||||
"jest",
|
"jest",
|
||||||
"--testLocationInResults",
|
"--testLocationInResults",
|
||||||
@@ -361,6 +379,12 @@ local function base_cmd()
|
|||||||
"--setupFilesAfterEnv",
|
"--setupFilesAfterEnv",
|
||||||
setup_path(),
|
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
|
end
|
||||||
|
|
||||||
local function parse_result_line(line)
|
local function parse_result_line(line)
|
||||||
@@ -502,15 +526,15 @@ end
|
|||||||
function runner.build_command(spec)
|
function runner.build_command(spec)
|
||||||
local file = spec.file
|
local file = spec.file
|
||||||
if not file or file == "" then
|
if not file or file == "" then
|
||||||
return { cmd = base_cmd(), cwd = spec.cwd }
|
return { cmd = base_cmd(spec.cwd), cwd = spec.cwd }
|
||||||
end
|
end
|
||||||
if spec.kind == "file" then
|
if spec.kind == "file" then
|
||||||
local cmd = base_cmd()
|
local cmd = base_cmd(spec.cwd)
|
||||||
table.insert(cmd, "--runTestsByPath")
|
table.insert(cmd, "--runTestsByPath")
|
||||||
table.insert(cmd, file)
|
table.insert(cmd, file)
|
||||||
return { cmd = cmd, cwd = spec.cwd }
|
return { cmd = cmd, cwd = spec.cwd }
|
||||||
end
|
end
|
||||||
local cmd = base_cmd()
|
local cmd = base_cmd(spec.cwd)
|
||||||
table.insert(cmd, "--runTestsByPath")
|
table.insert(cmd, "--runTestsByPath")
|
||||||
table.insert(cmd, file)
|
table.insert(cmd, file)
|
||||||
local ok, pattern = pcall(function()
|
local ok, pattern = pcall(function()
|
||||||
@@ -546,7 +570,7 @@ function runner.build_file_command(bufnr)
|
|||||||
"jest.config.mjs",
|
"jest.config.mjs",
|
||||||
"jest.config.json",
|
"jest.config.json",
|
||||||
})
|
})
|
||||||
local cmd = base_cmd()
|
local cmd = base_cmd(cwd)
|
||||||
table.insert(cmd, "--runTestsByPath")
|
table.insert(cmd, "--runTestsByPath")
|
||||||
table.insert(cmd, path)
|
table.insert(cmd, path)
|
||||||
return { cmd = cmd, cwd = cwd }
|
return { cmd = cmd, cwd = cwd }
|
||||||
@@ -562,7 +586,7 @@ function runner.build_all_command(bufnr)
|
|||||||
"jest.config.mjs",
|
"jest.config.mjs",
|
||||||
"jest.config.json",
|
"jest.config.json",
|
||||||
})
|
})
|
||||||
local cmd = base_cmd()
|
local cmd = base_cmd(cwd)
|
||||||
return { cmd = cmd, cwd = cwd }
|
return { cmd = cmd, cwd = cwd }
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -571,7 +595,8 @@ function runner.build_failed_command(last_command, failures, _scope_kind)
|
|||||||
if last_command and last_command.cmd then
|
if last_command and last_command.cmd then
|
||||||
return { cmd = last_command.cmd, cwd = last_command.cwd }
|
return { cmd = last_command.cmd, cwd = last_command.cwd }
|
||||||
end
|
end
|
||||||
return { cmd = base_cmd() }
|
local cwd = vim.loop.cwd()
|
||||||
|
return { cmd = base_cmd(cwd), cwd = cwd }
|
||||||
end
|
end
|
||||||
|
|
||||||
local pattern_parts = {}
|
local pattern_parts = {}
|
||||||
@@ -595,7 +620,7 @@ function runner.build_failed_command(last_command, failures, _scope_kind)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if #cmd == 0 then
|
if #cmd == 0 then
|
||||||
cmd = base_cmd()
|
cmd = base_cmd(last_command and last_command.cwd or vim.loop.cwd())
|
||||||
end
|
end
|
||||||
table.insert(cmd, "--testNamePattern")
|
table.insert(cmd, "--testNamePattern")
|
||||||
table.insert(cmd, pattern)
|
table.insert(cmd, pattern)
|
||||||
|
|||||||
@@ -279,6 +279,62 @@ describe("test-samurai-jest-runner", function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("adds jest.setup.js from project root to setupFilesAfterEnv", function()
|
||||||
|
with_project(JEST_PACKAGE, function(root)
|
||||||
|
write_file(root .. "/jest.setup.js", "console.log('setup')")
|
||||||
|
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
vim.api.nvim_buf_set_name(bufnr, root .. "/foo.test.ts")
|
||||||
|
|
||||||
|
local cmd_spec = runner.build_file_command(bufnr)
|
||||||
|
|
||||||
|
assert.are.same(
|
||||||
|
{
|
||||||
|
"npx",
|
||||||
|
"jest",
|
||||||
|
"--testLocationInResults",
|
||||||
|
"--reporters",
|
||||||
|
get_reporter_path(),
|
||||||
|
"--setupFilesAfterEnv",
|
||||||
|
get_setup_path(),
|
||||||
|
"--setupFilesAfterEnv",
|
||||||
|
root .. "/jest.setup.js",
|
||||||
|
"--runTestsByPath",
|
||||||
|
root .. "/foo.test.ts",
|
||||||
|
},
|
||||||
|
cmd_spec.cmd
|
||||||
|
)
|
||||||
|
assert.equals(root, cmd_spec.cwd)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("adds jest.setup.ts from test/.bin when root setup is missing", function()
|
||||||
|
with_project(JEST_PACKAGE, function(root)
|
||||||
|
write_file(root .. "/test/.bin/jest.setup.ts", "console.log('setup')")
|
||||||
|
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
vim.api.nvim_buf_set_name(bufnr, root .. "/bar.test.ts")
|
||||||
|
|
||||||
|
local cmd_spec = runner.build_file_command(bufnr)
|
||||||
|
|
||||||
|
assert.are.same(
|
||||||
|
{
|
||||||
|
"npx",
|
||||||
|
"jest",
|
||||||
|
"--testLocationInResults",
|
||||||
|
"--reporters",
|
||||||
|
get_reporter_path(),
|
||||||
|
"--setupFilesAfterEnv",
|
||||||
|
get_setup_path(),
|
||||||
|
"--setupFilesAfterEnv",
|
||||||
|
root .. "/test/.bin/jest.setup.ts",
|
||||||
|
"--runTestsByPath",
|
||||||
|
root .. "/bar.test.ts",
|
||||||
|
},
|
||||||
|
cmd_spec.cmd
|
||||||
|
)
|
||||||
|
assert.equals(root, cmd_spec.cwd)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
it("build_all_command runs project tests", function()
|
it("build_all_command runs project tests", function()
|
||||||
with_project(JEST_PACKAGE, function(root)
|
with_project(JEST_PACKAGE, function(root)
|
||||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
|||||||
Reference in New Issue
Block a user