marleyvim/nvim/lua/plugins/test/neotest.lua

147 lines
3.2 KiB
Lua
Raw Permalink Normal View History

2025-01-04 12:31:37 -08:00
return {
'neotest',
keys = {
{ '<LEADER>t', '', desc = '+test' },
{
'<LEADER>tl',
function()
require('neotest').run.run_last()
end,
desc = 'run last',
},
{
'<LEADER>to',
function()
require('neotest').output.open({ enter = true, auto_close = true })
end,
desc = 'show output',
},
{
'<LEADER>tO',
function()
require('neotest').output_panel.toggle()
end,
desc = 'toggle output panel',
},
{
'<LEADER>tr',
function()
require('neotest').run.run()
end,
desc = 'run nearest',
},
{
'<LEADER>ts',
function()
require('neotest').summary.toggle()
end,
desc = 'toggle summary',
},
{
'<LEADER>tS',
function()
require('neotest').run.stop()
end,
desc = 'stop tests',
},
{
'<LEADER>tt',
function()
require('neotest').run.run(vim.fn.expand('%'))
end,
desc = 'run file tests',
},
{
'<LEADER>tT',
function()
require('neotest').run.run(vim.uv.cwd())
end,
desc = 'run all tests',
},
{
'<LEADER>tw',
function()
require('neotest').watch.toggle(vim.fn.expand('%'))
end,
desc = 'toggle watch',
},
},
before = function()
require('lz.n').trigger_load({
'nvim-nio',
'plenary.nvim',
'nvim-treesitter',
})
end,
after = function()
local neotest_ns = vim.api.nvim_create_namespace('neotest')
vim.diagnostic.config({
virtual_text = {
format = function(diagnostic)
-- Replace newline/tab chars with spaces.
return diagnostic.message
:gsub('\n', ' ')
:gsub('\t', ' ')
:gsub('%s+', ' ')
:gsub('^%s+', ' ')
end,
},
}, neotest_ns)
local consumers = {}
-- Refresh and auto-close trouble after running tests.
local has_trouble, _ = pcall(require, 'trouble')
if has_trouble then
---@type neotest.Consumer
consumers.trouble = function(client)
client.listeners.results = function(adapter_id, results, partial)
if partial then
return
end
local tree =
assert(client:get_position(nil, { adapter = adapter_id }))
local failed = 0
for pos_id, result in pairs(results) do
if result.status == 'failed' and tree:get_key(pos_id) then
failed = failed + 1
end
end
vim.schedule(function()
local trouble = require('trouble')
if trouble.is_open() then
trouble.refresh()
if failed == 0 then
trouble.close()
end
end
end)
end
return {}
end
end
require('neotest').setup({
status = { virtual_text = true },
output = { virtual_text = true },
quickfix = {
open = function()
if has_trouble then
require('trouble').open({ mode = 'quickfix', focus = false })
else
vim.cmd('copen')
end
end,
},
consumers = consumers,
})
end,
}