diff --git a/lazy-lock.json b/lazy-lock.json index 5932098..094b022 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -28,6 +28,7 @@ "mason.nvim": { "branch": "main", "commit": "2a6940af80375532e5e9e7c1f2fc6319a1b7a69d" }, "neotest": { "branch": "master", "commit": "27bf921498043f7ecd821d6db68d05de244bbd02" }, "neotest-golang": { "branch": "main", "commit": "05cd7bf89a146cf1fa2848a9fe277a1e68a3a51d" }, + "neotest-vitest": { "branch": "main", "commit": "b0c265743c877058068dbc40d104ea5d40e6b257" }, "neotest-vstest": { "branch": "main", "commit": "de30c7978937d07e6d29ab7807a00fad1c61120b" }, "nvim-autopairs": { "branch": "master", "commit": "3172f40c16563db871a58f20d205c710ad782311" }, "nvim-cmp": { "branch": "main", "commit": "2ffe79f1f021def8dd1fcd81deb16f1bb0d989f3" }, diff --git a/lua/simonkdev/lazy/testing.lua b/lua/simonkdev/lazy/testing.lua index cbe4c90..74e9926 100644 --- a/lua/simonkdev/lazy/testing.lua +++ b/lua/simonkdev/lazy/testing.lua @@ -1,3 +1,66 @@ +-- neotest-vitest assumes Unix paths. On Windows it runs node_modules/.bin/vitest, a shell script Windows can't +-- start, mixes C:/ and C:\ paths, and can't find the project's config: tests aren't found when Neovim was started +-- above the project (e.g. at a monorepo root), tests that use Vitest globals fail, and every result reads as +-- failed (marilari88/neotest-vitest#90; the unmerged fix #74 covers only the last part). These patches use +-- Windows paths throughout; drop them once upstream handles Windows. +local function vitest_adapter() + local adapter = require("neotest-vitest") + if vim.fn.has("win32") == 0 then + return adapter({}) + end + + local function native(path) + return (path:gsub("^%a:", string.upper):gsub("/", "\\")) + end + + local root = adapter.root + adapter.root = function(path) + local dir = root(path) + return dir and native(dir) + end + + local util = require("neotest-vitest.util") + local to_results = util.parsed_json_to_results + util.parsed_json_to_results = function(data, ...) + for _, file in pairs(data.testResults or {}) do + file.name = native(file.name) + end + return to_results(data, ...) + end + + return adapter({ + vitestCommand = function(path) + for dir in vim.fs.parents(path) do + local bin = vim.fs.joinpath(dir, "node_modules", ".bin", "vitest.cmd") + if vim.uv.fs_stat(bin) then + return bin + end + end + return "vitest" + end, + -- It finds the project with a {vite,vitest}.config.* glob, and Windows glob has no braces. Run from the + -- package directory instead, where Vitest picks up vite.config.ts (globals etc.) itself. + cwd = function(path) + return vim.fs.root(path, "package.json") + end, + }) +end + +-- neotest parses test files in a child Neovim started with -u NONE, which never loads nvim-treesitter's filetype +-- aliases (typescriptreact -> tsx), so .tsx test files show no tests (nvim-neotest/neotest#630, #631). Adding +-- nvim-treesitter to the child's runtimepath fixes it: neotest's add_paths_to_rtp sources plugin/filetypes.lua. +local function load_treesitter_aliases_in_subprocess() + local subprocess = require("neotest.lib").subprocess + local init = subprocess.init + subprocess.init = function(...) + init(...) + local treesitter = require("lazy.core.config").plugins["nvim-treesitter"] + if subprocess.enabled() and treesitter then + subprocess.add_paths_to_rtp({ treesitter.dir }) + end + end +end + return { { "nvim-neotest/neotest", @@ -8,8 +71,10 @@ return { "nvim-treesitter/nvim-treesitter", "Nsidorenco/neotest-vstest", { "fredrikaverpil/neotest-golang", version = "^2" }, + "marilari88/neotest-vitest", }, config = function() + load_treesitter_aliases_in_subprocess() require("neotest").setup({ adapters = { require("neotest-vstest")({ @@ -18,6 +83,7 @@ return { }), require("neotest-golang")(), -- debugs tests through nvim-dap-go require("rustaceanvim.neotest"), -- debugs tests through codelldb + vitest_adapter(), }, status = { virtual_text = true }, output = { diff --git a/tests/smoke.lua b/tests/smoke.lua index 520cdce..b9e9f3f 100644 --- a/tests/smoke.lua +++ b/tests/smoke.lua @@ -203,6 +203,14 @@ local function run() vim.api.nvim_buf_delete(buf, { force = true }) end + -- Every language has a test adapter; a missing one leaves T… silently finding no tests. + local adapters = vim.tbl_map(function(a) + return a.name + end, require("neotest.config").adapters) + for _, name in ipairs({ "neotest-vstest", "neotest-golang", "rustaceanvim", "neotest-vitest" }) do + check(vim.list_contains(adapters, name), "neotest has no " .. name .. " adapter") + end + -- Files without a formatter (and C#, after Roslyn) are trimmed on save; patches are left alone. check(vim.tbl_contains(conform.formatters_by_ft.cs, "trim_whitespace"), "cs buffers aren't trimmed") local input = { " a ", "", "" }