This is a basic neotest adapter that allows running tests using the swift test
command.
- Running a single test case, all the tests in a file, all the tests in a target or all the tests in a project
- Test watching
- Virtual text showing test failure messages
- Displaying full and short test output
- DAP support
use {
"nvim-neotest/neotest",
requires = {
"nvim-neotest/nvim-nio",
"nvim-lua/plenary.nvim",
"antoinemadec/FixCursorHold.nvim",
"nvim-treesitter/nvim-treesitter",
"ehmurray8/neotest-swift"
}
}
Provide your adapters and other config to the setup function.
require("neotest").setup({
adapters = {
require("neotest-swift")({ }),
},
output = {
enabled = true,
open_on_run = false
}
})
Requires:
- nvim-dap
- codelldb
- I installed it with Mason
- (Optional) nvim-dap-ui
local dap = require("dap")
local dapui = require("dapui")
dapui.setup()
-- Automatically attach and detach from DAPUI
dap.listeners.before.attach.dapui_config = function()
dapui.open()
end
dap.listeners.before.launch.dapui_config = function()
dapui.open()
end
dap.listeners.before.event_terminated.dapui_config = function()
dapui.close()
end
dap.listeners.before.event_exited.dapui_config = function()
dapui.close()
end
-- Use swift's LLDB
local libLLDB = "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB"
dap.adapters.swift = {
type = "server",
port = "${port}",
executable = {
command = "/Users/emmet/.local/share/nvim/mason/bin/codelldb", -- Use your exectuable I got this from Mason
args = { "--liblldb", libLLDB, "--port", "${port}" },
},
}
dap.configurations.swift = {
{
name = "Launch file",
type = "swift",
request = "launch",
program = function()
return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
end,
cwd = "${workspaceFolder}",
stopOnEntry = false,
},
}
-- Neotest
vim.keymap.set("n", "<Leader>tr", function() require("neotest").run.run() end, { desc = 'Run nearest test' })
vim.keymap.set("n", "<Leader>tf", function() require("neotest").run.run(vim.fn.expand("%")) end, { desc = 'Run all tests in file' })
vim.keymap.set("n", "<Leader>ta", function() require("neotest").run.run({ suite = true }) end, { desc = 'Run all tests in project' })
vim.keymap.set("n", "<Leader>tt", function() require("neotest").run.run({ suite = true, extra_args = { target = true } }) end, { desc = 'Run all tests in target (swift).' })
vim.keymap.set("n", "<Leader>tw", function() require("neotest").watch.toggle() end, { silent = true, desc = 'Watch test' })
vim.keymap.set("n", "<Leader>ts", function() require("neotest").summary.toggle() end, { silent = true, desc = 'Test summary' })
vim.keymap.set("n", "<Leader>to", function() require("neotest").output.open({ short = true, enter = true }) end, { silent = true, desc = 'Open test output' })
vim.keymap.set("n", "<Leader>tp", function() require("neotest").output_panel.toggle() end, { silent = true, desc = 'Toggle test output pane' })
-- nvim-dap
vim.keymap.set("n", "<Leader>et", function() require("neotest").run.run({ strategy = "dap" }) end, { desc = 'Debug nearest test' })
vim.keymap.set("n", "<Leader>eb", function() require("dap").toggle_breakpoint() end, { desc = "Debug set breakpoint" })
vim.keymap.set("v", "<leader>ee", function() require("dapui").eval() end, { desc = "Debug evaluate" })
vim.keymap.set("n", "<Leader>ec", function() require("dap").continue() end, { desc = "Debug continue" })
vim.keymap.set("n", "<Leader>eo", function() require("dap").step_over() end, { desc = "Debug step over" })
vim.keymap.set("n", "<Leader>ei", function() require("dap").step_into() end, { desc = "Debug step into" })
vim.api.nvim_create_user_command("DAPUI", function() require("dapui").toggle() end, { desc = "Open DAPUI" })