nvim-neotest / neotest

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

[Question] Show test output when running tests? #253

Open Kindlewing opened 1 year ago

Kindlewing commented 1 year ago

Is there a way to show test output when running the tests? Below is my config:

    config = function()
        require('neotest').setup({
            require('neodev').setup({
                library = { plugins = { 'neotest' }, types = true },
            }),
            adapters = {
                require('neotest-phpunit')({
                    phpunit_cmd = function()
                        return 'vendor/bin/phpunit'
                    end,
                }),
            },
            default_strategy = 'integrated',
            output = {
                open_on_run = true,
                enter = true,
            },
            benchmark = {
                enabled = true,
            },
            diagnostic = {
                enabled = true,
                severity = 1,
            },
            consumers = {},
            strategies = {
                integrated = {
                    width = 120,
                    height = 40,
                },
            },
        })
    end

Keymap for running:

maps.n['<leader>Uf'] = {
    function()
        tests.run.run(vim.fn.expand('%'))
    end,
    desc = 'Run all tests in current file',
}
aaronmcadam commented 1 year ago

I've been wondering the same, @Kindlewing. What if the run method could take a callback? We could use that to do whatever we wanted after running tests.

robamu commented 10 months ago

It would be really nice if it was possible to jump into the test output directly after running the test. Maybe I overlooked something inside the documentation, but I don't know how to jump into the output without an extra command. I have since written a :NtShow custom command for this, but it would be really nice to just enter the output directly if I am running the test from code.

yuchanns commented 10 months ago

Just noticed that it would show test outputs if the test failed:

https://github.com/nvim-neotest/neotest/blob/1e67a504d03def3a6a1125d934cb511680f72555/lua/neotest/consumers/output.lua#L120

The option open_on_run is ambiguous, making users expect the output to show after the run action, IMO. It can be easily done by just removing this line of code. But I believe that there must be a better way to solve that.

wookayin commented 10 months ago

See #50; show_output or attach depending on the test job status.

yuchanns commented 10 months ago

See #50; show_output or attach depending on the test job status.

Update:

No problem directly with vim.loop instead of plenary.job (I guess vim.uv is ok too):

  local neotest = require("neotest")
  neotest.setup({
    adapters = {
      require("neotest-go"),
      require("neotest-rust")({ args = { "--no-capture" }, dap_adapter = "lldb" }),
    },
    status = { virtual_text = true },
    output = { enabled = true, open_on_run = false },
    consumers = {
      attach_or_output = function(client)
        local M = {}
        function M.open(opts)
          opts = opts or {}
          local pos = neotest.run.get_tree_from_args(opts)
          if pos and client:is_running(pos:data().id) then
            neotest.run.attach()
          else
            neotest.output.open({ enter = true })
          end
        end

        return M
      end,
    },
  })
  vim.api.nvim_create_user_command("NTestOutput", function()
    neotest.run.run()
    local handle
    handle, _ = vim.loop.spawn(
      "sleep",
      { args = { "3s" }, stdio = nil },
      vim.schedule_wrap(function(_)
        handle:close()
        neotest.attach_or_output.open()
      end)
    )
  end, {})

The custom consumer can choose a proper way to attach running or open results. But I always have to execute run and attach_or_output sequentially.

I have tried to create a command combined with run which executes attach_or_output after sleeping one second using plenary.job. But it failed on the coroutine callback to the open method.

I should see what I can do on run's listeners