Setup neotest for testing in neovim

main
Ensar Sarajčić 2023-08-02 19:04:01 +02:00
parent 3bea81ed1a
commit b2c5ed330e
3 changed files with 48 additions and 61 deletions

View File

@ -2,68 +2,49 @@
-- - Vim-test and general testing config - -- - Vim-test and general testing config -
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- make test commands execute using dispatch.vim local neotest = require("neotest")
vim.g["test#strategy"] = "dispatch" neotest.setup({
vim.g["test#csharp#runner"] = "dotnettest" adapters = {
require("neotest-rust"),
require("neotest-plenary"),
require("neotest-vim-test")({
ignore_file_types = { "rust", "lua" },
}),
},
})
local last = nil
-- Map test running commands -- Map test running commands
local opts = { silent = true } local opts = { silent = true }
vim.keymap.set("n", "<Leader>tn", ":TestNearest<CR>", opts) vim.keymap.set("n", "<Leader>tn", function()
vim.keymap.set("n", "<Leader>tf", ":TestFile<CR>", opts) last = nil
vim.keymap.set("n", "<Leader>ts", ":TestSuite<CR>", opts) neotest.run.run()
vim.keymap.set("n", "<Leader>tl", ":TestLast<CR>", opts) end, opts)
vim.keymap.set("n", "<Leader>tg", ":TestVisit<CR>", opts) vim.keymap.set("n", "<Leader>tdn", function()
last = { strategy = "dap", suite = false }
local last_path = nil neotest.run.run(last)
end, opts)
local function get_plenary_test_opts() vim.keymap.set("n", "<Leader>tf", function()
if vim.g["esensar#testing#use_minimal"] then last = vim.fn.expand("%")
return { neotest.run.run(last)
minimal_init = vim.g["esensar#testing#minimal_init"] or "tests/minimal.vim", end, opts)
} vim.keymap.set("n", "<Leader>tdf", function()
last = { vim.fn.expand("%"), strategy = "dap", suite = false }
neotest.run.run(last)
end, opts)
vim.keymap.set("n", "<Leader>ts", function()
last = { suite = true }
neotest.run.run(last)
end, opts)
vim.keymap.set("n", "<Leader>tds", function()
last = { strategy = "dap", suite = true }
neotest.run.run(last)
end, opts)
vim.keymap.set("n", "<Leader>tl", function()
if last then
neotest.run.run(last)
else else
return nil neotest.run.run()
end end
end end, opts)
vim.api.nvim_create_user_command("PlenaryTestFile", function()
last_path = vim.fn.expand("%:p")
require("plenary.test_harness").test_directory(last_path, get_plenary_test_opts())
end, { desc = "Test current file using plenary.nvim" })
vim.api.nvim_create_user_command("PlenaryTestSuite", function()
last_path = vim.fn["projectionist#path"]()
require("plenary.test_harness").test_directory(last_path, get_plenary_test_opts())
end, { desc = "Run all tests using plenary.nvim" })
vim.api.nvim_create_user_command("PlenaryTestLast", function()
if not last_path then
vim.notify("No plenary tests run yet! Nothing to do here", vim.log.levels.WARN)
return
end
require("plenary.test_harness").test_directory(last_path, get_plenary_test_opts())
end, { desc = "Run last run test using plenary.nvim" })
vim.api.nvim_create_user_command("PlenaryVisitLastTest", function()
if not last_path then
vim.notify("No plenary tests run yet! Nothing to do here", vim.log.levels.WARN)
return
end
vim.cmd("edit " .. last_path)
end, { desc = "Visit latest run test using plenary.nvim" })
local au_id = vim.api.nvim_create_augroup("plenary_test_group", {})
vim.api.nvim_create_autocmd("FileType", {
pattern = "lua",
group = au_id,
callback = function()
local local_opts = { silent = true, buffer = true }
vim.keymap.set("n", "<Leader>tn", ":PlenaryTestFile<CR>", local_opts)
vim.keymap.set("n", "<Leader>tf", ":PlenaryTestFile<CR>", local_opts)
vim.keymap.set("n", "<Leader>ts", ":PlenaryTestSuite<CR>", local_opts)
vim.keymap.set("n", "<Leader>tl", ":PlenaryTestLast<CR>", local_opts)
vim.keymap.set("n", "<Leader>tg", ":PlenaryVisitLastTest<CR>", local_opts)
end,
})

View File

@ -43,6 +43,10 @@ return require("packer").startup({
-- Tools -- Tools
use("direnv/direnv.vim") -- Integration with Direnv use("direnv/direnv.vim") -- Integration with Direnv
use("nvim-neotest/neotest") -- Running tests from NeoVim
use("nvim-neotest/neotest-plenary")
use("rouge8/neotest-rust")
use("nvim-neotest/neotest-vim-test") -- vim-test plugin for neotest
use("vim-test/vim-test") -- Running tests from vim use("vim-test/vim-test") -- Running tests from vim
use("mfussenegger/nvim-dap") -- Debug Adapter Protocol use("mfussenegger/nvim-dap") -- Debug Adapter Protocol
use("rcarriga/nvim-dap-ui") -- UI components for DAP use("rcarriga/nvim-dap-ui") -- UI components for DAP

View File

@ -5,7 +5,9 @@
local lspconfig = require("lspconfig") local lspconfig = require("lspconfig")
require("mason").setup() require("mason").setup()
require("mason-lspconfig").setup() require("mason-lspconfig").setup()
require("neodev").setup() require("neodev").setup({
library = { plugins = { "neotest" }, types = true },
})
local common_config = require("esensar.lsp.server_config") local common_config = require("esensar.lsp.server_config")
-- Language specific LSP config overrides -- Language specific LSP config overrides