nvim-neotest / neotest

An extensible framework for interacting with tests within NeoVim.
MIT License
2.12k stars 105 forks source link

[BUG] Cannot run nearest test or all tests from file #386

Closed rluetzner closed 2 months ago

rluetzner commented 3 months ago

NeoVim Version NVIM v0.9.5

Describe the bug When I try to run any test directly from a file, I get an exception that indicates that no client is set.

E5108: Error executing lua ...obertl/.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:95: Async task failed without callback: The coroutine failed with this message: 
...al/share/nvim/lazy/neotest/lua/neotest/consumers/run.lua:37: attempt to index upvalue 'client' (a nil value)
stack traceback:
    ...al/share/nvim/lazy/neotest/lua/neotest/consumers/run.lua: in function <...al/share/nvim/lazy/neotest/lua/neotest/consumers/run.lua:24>
    ...al/share/nvim/lazy/neotest/lua/neotest/consumers/run.lua:38: in function 'get_tree_from_args'
    ...al/share/nvim/lazy/neotest/lua/neotest/consumers/run.lua:142: in function 'func'
    ...obertl/.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:168: in function <...obertl/.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:167>
stack traceback:
    [C]: in function 'error'
    ...obertl/.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:95: in function 'close_task'
    ...obertl/.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:117: in function 'step'
    ...obertl/.local/share/nvim/lazy/nvim-nio/lua/nio/tasks.lua:145: in function 'stop'
    [string ":lua"]:1: in main chunk

Surprisingly enough, when I open up the summary instead, all tests are discovered and can be run without problems. Well, almost...after I trigger the above exception once, I can't ran tests via the summary pane either.

To Reproduce Please provide a minimal init.lua to reproduce which can be run as the following:

nvim --clean -u minimal.lua

You can edit the following example file to include your adapters and other required setup.

cal lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

-- Example using a list of specs with the default options
vim.g.mapleader = " " -- Make sure to set `mapleader` before lazy so your mappings are correct
vim.g.maplocalleader = "\\" -- Same for `maplocalleader`

require("lazy").setup({
  {
    "nvim-treesitter/nvim-treesitter",
    opts = {
      ensure_installed = {
        "c_sharp",
        "go",
      },
      auto_install = true,
    },
  },
  {"nvim-neotest/neotest",
    dependencies = {
      "nvim-neotest/nvim-nio",
      "nvim-lua/plenary.nvim",
      "antoinemadec/FixCursorHold.nvim",
      "Issafalcon/neotest-dotnet",
      "nvim-neotest/neotest-go"
    },
    config = function()
      -- get neotest namespace (api call creates or returns namespace)
      local neotest_ns = vim.api.nvim_create_namespace("neotest")
      vim.diagnostic.config({
        virtual_text = {
          format = function(diagnostic)
            local message =
              diagnostic.message:gsub("\n", " "):gsub("\t", " "):gsub("%s+", " "):gsub("^%s+", "")
            return message
          end,
        },
      }, neotest_ns)

      -- setup keybindings
      vim.api.nvim_set_keymap("n", "<Leader>tt", ':lua require("neotest").run().run()<CR>', { noremap = true, silent = true, desc = 'Run nearest [t]est' })
      vim.api.nvim_set_keymap("n", "<Leader>tf", ':lua require("neotest").run().run(vim.fn.expand("%"))<CR>', { noremap = true, silent = true, desc = 'Run all tests in [f]ile' })
      vim.api.nvim_set_keymap("n", "<Leader>tt", ':lua require("neotest").run().stop()<CR>', { noremap = true, silent = true, desc = '[S]top nearest test' })
      vim.api.nvim_set_keymap("n", "<Leader>to", ':lua require("neotest").output.open()<CR>', { noremap = true, silent = true, desc = 'Show test [o]utput' })
      vim.api.nvim_set_keymap("n", "<Leader>tO", ':lua require("neotest").output.open({ enter = true })<CR>', { noremap = true, silent = true, desc = 'Show test [O]utput and enter' })
      vim.api.nvim_set_keymap("n", "<Leader>ts", ':lua require("neotest").summary.toggle()<CR>', { noremap = true, silent = true, desc = 'Show test [s]ummary' })

      require('neotest').setup({
        adapters = {
          require('neotest-dotnet'),
          require('neotest-go'),
        }
      })
    end,
  }
})

Steps to reproduce the behavior:

  1. Open a dotnet or go testfile.
  2. Use <Leader>ts to toggle the summary view.
  3. Execute a test.
  4. Use :lua require('neotest').run.run() or <Leader>tt to run the nearest test.
  5. See error.
  6. Try to execute the test via the summary view.
  7. See the same error.

Please provide example test files to reproduce. --> I will provide these, if it turns out that this cannot be reproduced with other languages. I don't have time to write a new set of C# or Go tests. I assume that this behavior is not limited to C# or Go and I'm surprised that nobody else seems to have reported it yet.

Expected behavior When I hit <Leader>tt or execute :lua require('neotest').run.run() I should run the nearest test. When I hit <Leader>tf, I should run all tests from the file.

Logs neotest.log

Additional context Add any other context about the problem here.

GoldenChrysus commented 3 months ago

Same issue as well. Sometimes it works, sometimes it doesn't (applies to all commands, like watch, summary, etc.). Using rustaceanvim. Have had to abandon using neotest for now due to the issue. Tried versions 5.0.1, 5.0.0, and 4.9.0; issue occurs in all of them.

rcarriga commented 3 months ago

Can you please provide the logs as requested, the error suggests the setup is not completing correctly

rluetzner commented 2 months ago

Sorry @rcarriga , I forgot about this for a while. I've appended the log file to the bug report.

I did a quick scan myself, but couldn't find any errors or warnings in it.

koptan commented 2 months ago

Same issue here, I am using NvChad

  {
  "nvim-neotest/neotest",
  lazy = false,
  dependencies = {
    "nvim-neotest/nvim-nio",
    "nvim-lua/plenary.nvim",
    "antoinemadec/FixCursorHold.nvim",
    "nvim-treesitter/nvim-treesitter"
  },
  config = function()
    require("neotest").setup({
      disable_all = true,
      enable = {
        "test_nearest",
        "test_file",
        "test_suite",
        "last_failed",
        "visit",
        "rerun",
        "debug",
      },
      term_pos = "vert",
      term_opener = "vsplit",
      term_closer = "q",
      term_geo = { "curwin", "vertical", "botright", "split" },
      adapters = {
           require('rustaceanvim.neotest')
      },
     }
       )
  end
},
koptan commented 2 months ago

@rluetzner I found the issue, don't call first run as function

.run().run() it should be run.run()

rluetzner commented 2 months ago

Thank you. That did it! 🙂