From 2d5889dce3644094fd9a494b62cf7ecb9cbe96f1 Mon Sep 17 00:00:00 2001 From: "M.Schirmer" Date: Tue, 6 Jan 2026 16:20:15 +0100 Subject: [PATCH] add configurable glob test pattern for TSamAll --- README.md | 10 +++++++++- lua/test-samurai-mocha-runner/init.lua | 18 +++++++++++++++++- tests/test_mocha_runner_spec.lua | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cb4428f..3988983 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Mocha.js runner for the test-samurai Neovim plugin. - Provides quickfix locations and per-test output. - 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. -- `TSamAll` runs `test/**/*.{test,spec}.{t,j}s` to discover tests reliably. +- `TSamAll` runs `test/**/*.test.js` by default. ## 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 Run tests: diff --git a/lua/test-samurai-mocha-runner/init.lua b/lua/test-samurai-mocha-runner/init.lua index ccf2112..e2f5b11 100644 --- a/lua/test-samurai-mocha-runner/init.lua +++ b/lua/test-samurai-mocha-runner/init.lua @@ -3,6 +3,21 @@ local runner = { 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 source = debug.getinfo(1, "S").source if source:sub(1, 1) == "@" then @@ -573,6 +588,7 @@ function runner.build_all_command(bufnr) if not cwd then return { cmd = { "echo", "no package.json found" } } end + local glob = config and config.all_test_glob or default_config.all_test_glob return { cmd = { "npx", @@ -583,7 +599,7 @@ function runner.build_all_command(bufnr) RUNNER_PATHS.ui, "--reporter", RUNNER_PATHS.reporter, - "test/**/*.{test,spec}.{t,j}s", + glob, }, cwd = cwd, } diff --git a/tests/test_mocha_runner_spec.lua b/tests/test_mocha_runner_spec.lua index 92463c2..1711bb6 100644 --- a/tests/test_mocha_runner_spec.lua +++ b/tests/test_mocha_runner_spec.lua @@ -24,6 +24,10 @@ local function with_project(structure, fn) end describe("test-samurai-mocha-runner", function() + before_each(function() + runner.setup() + end) + it("detects mocha test files via package.json", function() with_project({ ["package.json"] = [[{"devDependencies":{"mocha":"10.0.0"}}]], @@ -170,7 +174,18 @@ describe("test-samurai-mocha-runner", function() }, function(root) local bufnr = make_buffer(root .. "/test/sample.spec.js", "") 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)