A Neovim plugin to quickly switch between JavaScript/TypeScript source files and their corresponding test files.
- Bidirectional toggle: Use the same keybinding to switch from source → test and test → source
- Automatic test file creation: Prompts to create missing test files (with directory creation)
- Configurable: Customize keybindings, test directory, and file suffix
- Zero dependencies: Pure Lua, no external dependencies
{
"gtamasi/test-file-switch",
config = function()
require("test-file-switch").setup()
end,
ft = { "javascript", "typescript", "javascriptreact", "typescriptreact" },
}use {
"gtamasi/test-file-switch",
config = function()
require("test-file-switch").setup()
end,
}Plug 'username/test-file-switch'Then in your config:
require("test-file-switch").setup()git clone https://github.com/gtamasi/test-file-switch \
~/.local/share/nvim/site/pack/plugins/start/test-file-switchrequire("test-file-switch").setup()This enables the default keybinding <leader>fM to toggle between source and test files.
The plugin also provides a command:
:TestFileSwitchWhen editing src/utils/helper.js, pressing <leader>fM opens test/src/utils/helper.spec.js.
When editing test/src/utils/helper.spec.js, pressing <leader>fM opens src/utils/helper.js.
If the test file doesn't exist, the plugin prompts:
Test file does not exist: tests/src/utils/helper.spec.js
Create it? [y/N]
Confirming creates the file (and any missing directories) and opens it.
require("test-file-switch").setup({
-- File switching options
test_dir = "test", -- Root test directory name
test_suffix = ".spec", -- Suffix before extension (.spec.js, .spec.ts)
-- Test file creation
prompt_create = true, -- Prompt to create missing test files
auto_create = false, -- Auto-create without prompting (overrides prompt_create)
-- Keymap options
keymap = "<leader>fM", -- Keybinding to toggle
keymap_mode = "n", -- Mode for keybinding (normal mode)
keymap_enabled = true, -- Whether to register the keymap
})require("test-file-switch").setup({
keymap = "<leader>ts",
})require("test-file-switch").setup({
keymap_enabled = false,
})
-- Set up your own keymap
vim.keymap.set("n", "<C-t>", require("test-file-switch").switch, {
desc = "Toggle between source and test file",
})require("test-file-switch").setup({
auto_create = true, -- Always create missing test files without prompting
})require("test-file-switch").setup({
prompt_create = false, -- Never offer to create test files
})The plugin maps source files to test files as follows:
| Source File | Test File |
|---|---|
src/utils/helper.js |
test/src/utils/helper.spec.js |
src/utils/helper.ts |
test/src/utils/helper.spec.ts |
lib/components/Button.tsx |
test/lib/components/Button.spec.ts |
index.js |
test/index.spec.js |
- Supported file types: Only JavaScript and TypeScript files (
.js,.ts,.jsx,.tsx) - Test directory location: Test files must be in a
test/directory at the project root - Naming convention: Test files must use the
.spec.jsor.spec.tssuffix (configurable viatest_suffix) - No co-located tests: Does not support test files next to source files (e.g.,
Component.test.jsalongsideComponent.js) - Single test directory: Does not support multiple test directories or custom glob patterns
- Neovim only: Requires Neovim with Lua support (not compatible with Vim)
MIT