Skip to content

Instantly share code, notes, and snippets.

@kylechui
Last active August 8, 2022 22:21
Show Gist options
  • Save kylechui/b86a37c28d0369bda41245a07ddb839e to your computer and use it in GitHub Desktop.
Save kylechui/b86a37c28d0369bda41245a07ddb839e to your computer and use it in GitHub Desktop.
Test your Neovim plugin from anywhere in the project
-- This assumes that you're testing with plenary, and your test file is in tests/[plugin]_spec.lua
-- Put this in `ftplugin/lua.lua`
vim.keymap.set("n", "<Leader>t", function()
-- Get the current path and traverse upwards until you find the folder that contains tests/
local cur_path = vim.fn.expand("%:p:h")
while cur_path and vim.fn.finddir("tests", cur_path) == "" do
cur_path = cur_path:match("(.*)/.*")
end
if cur_path then
require("plenary.test_harness").test_directory(cur_path .. "/tests", {
-- Optionally include a minimal configuration for testing purposes
minimal_init = cur_path .. "/tests/minimal_init.lua",
})
end
end, {
silent = true,
buffer = true,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment