add configurable glob test pattern for TSamAll

This commit is contained in:
2026-01-06 16:20:15 +01:00
parent f3350cad98
commit 2d5889dce3
3 changed files with 42 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ Mocha.js runner for the test-samurai Neovim plugin.
- Provides quickfix locations and per-test output. - Provides quickfix locations and per-test output.
- Uses `--grep` with escaped patterns to match titles safely, even when running through `npm test`. - Uses `--grep` with escaped patterns to match titles safely, even when running through `npm test`.
- Executes tests via `npx mocha` with the bundled UI + reporter so projects need no extra setup. - Executes tests via `npx mocha` with the bundled UI + reporter so projects need no extra setup.
- `TSamAll` runs `test/**/*.{test,spec}.{t,j}s` to discover tests reliably. - `TSamAll` runs `test/**/*.test.js` by default.
## Full Name Convention ## Full Name Convention
@@ -62,6 +62,14 @@ require("test-samurai").setup({
}) })
``` ```
Override the default glob for `TSamAll`:
```lua
require("test-samurai-mocha-runner").setup({
all_test_glob = "test/**/*.test.js",
})
```
## Development ## Development
Run tests: Run tests:

View File

@@ -3,6 +3,21 @@ local runner = {
framework = "javascript", framework = "javascript",
} }
local default_config = {
all_test_glob = "test/**/*.test.js",
}
local config = vim.deepcopy(default_config)
function runner.setup(opts)
if not opts then
config = vim.deepcopy(default_config)
return config
end
config = vim.tbl_deep_extend("force", vim.deepcopy(default_config), opts)
return config
end
local function runner_root() local function runner_root()
local source = debug.getinfo(1, "S").source local source = debug.getinfo(1, "S").source
if source:sub(1, 1) == "@" then if source:sub(1, 1) == "@" then
@@ -573,6 +588,7 @@ function runner.build_all_command(bufnr)
if not cwd then if not cwd then
return { cmd = { "echo", "no package.json found" } } return { cmd = { "echo", "no package.json found" } }
end end
local glob = config and config.all_test_glob or default_config.all_test_glob
return { return {
cmd = { cmd = {
"npx", "npx",
@@ -583,7 +599,7 @@ function runner.build_all_command(bufnr)
RUNNER_PATHS.ui, RUNNER_PATHS.ui,
"--reporter", "--reporter",
RUNNER_PATHS.reporter, RUNNER_PATHS.reporter,
"test/**/*.{test,spec}.{t,j}s", glob,
}, },
cwd = cwd, cwd = cwd,
} }

View File

@@ -24,6 +24,10 @@ local function with_project(structure, fn)
end end
describe("test-samurai-mocha-runner", function() describe("test-samurai-mocha-runner", function()
before_each(function()
runner.setup()
end)
it("detects mocha test files via package.json", function() it("detects mocha test files via package.json", function()
with_project({ with_project({
["package.json"] = [[{"devDependencies":{"mocha":"10.0.0"}}]], ["package.json"] = [[{"devDependencies":{"mocha":"10.0.0"}}]],
@@ -170,7 +174,18 @@ describe("test-samurai-mocha-runner", function()
}, function(root) }, function(root)
local bufnr = make_buffer(root .. "/test/sample.spec.js", "") local bufnr = make_buffer(root .. "/test/sample.spec.js", "")
local command = runner.build_all_command(bufnr) local command = runner.build_all_command(bufnr)
assert.is_true(vim.tbl_contains(command.cmd, "test/**/*.{test,spec}.{t,j}s")) assert.is_true(vim.tbl_contains(command.cmd, "test/**/*.test.js"))
end)
end)
it("allows overriding the TSamAll test glob via setup", function()
with_project({
["package.json"] = [[{"devDependencies":{"mocha":"10.0.0"}}]],
}, function(root)
runner.setup({ all_test_glob = "spec/**/*_spec.js" })
local bufnr = make_buffer(root .. "/test/sample.spec.js", "")
local command = runner.build_all_command(bufnr)
assert.is_true(vim.tbl_contains(command.cmd, "spec/**/*_spec.js"))
end) end)
end) end)