Skip to content

Set experimental fields dynamically #98

@drewbailey

Description

@drewbailey

Is it possible to set experimental fields dynamically? I would like to make a keymap to run with test_table = true, but keep it set to false by default.

Unfortunately I've been unable to configrue neotest/neotest-go to work properly with this setup.

return {
  "nvim-neotest/neotest",
  dependencies = {
    "nvim-neotest/nvim-nio",
    "nvim-neotest/neotest-go",
    "nvim-lua/plenary.nvim",
    "antoinemadec/FixCursorHold.nvim",
    "nvim-treesitter/nvim-treesitter",
  },
  opts = {},
  config = function(_, opts)
    local neotest = require("neotest")
    local neotest_go = require("neotest-go")

    -- Default adapter configuration
    local adapter = neotest_go({
      experimental = {
        test_table = false, -- Default behavior
      },
      args = { "-count=1", "-timeout=30s" },
    })

    opts.adapters = { adapter }
    neotest.setup(opts)

    -- Function to run with `test_table` enabled
    local function run_with_test_table()
      -- Print debug messages
      print("Running with `test_table` enabled")

      -- Update the adapter dynamically
      local temp_adapter = neotest_go({
        experimental = {
          test_table = true,
        },
        args = { "-count=1", "-timeout=30s" },
      })

      -- Reconfigure Neotest with the temporary adapter
      neotest.setup({
        adapters = { temp_adapter },
      })

      -- Run tests
      neotest.run.run(vim.fn.expand("%")) -- Run tests in the current file

      -- Restore the default adapter
      neotest.setup({
        adapters = { adapter },
      })
    end

    -- Keymaps
    vim.keymap.set("n", "<leader>tf", function()
      require("neotest").run.run(vim.fn.expand("%"))
    end, { desc = "[t]est [f]ile" })

    vim.keymap.set("n", "<leader>T", function()
      require("neotest").run.run()
    end, { desc = "[t]est current function" })

    vim.keymap.set("n", "<leader>tS", function()
      require("neotest").run.stop()
    end, { desc = "[S]top" })

    vim.keymap.set("n", "<leader>tw", function()
      require("neotest").watch.toggle(vim.fn.expand("%"))
    end, { desc = "[t]oggle [w]atch" })

    vim.keymap.set("n", "<leader>tts", function()
      require("neotest").summary.toggle()
    end, { desc = "[t]oggle [t]est [s]ummary" })

    vim.keymap.set("n", "<leader>tto", function()
      require("neotest").output.open({ enter = true, auto_close = true })
    end, { desc = "[t]oggle [t]est [o]utput" })

    vim.keymap.set("n", "<leader>ttO", function()
      require("neotest").output_panel.toggle()
    end, { desc = "[t]oggle [t]est [O]utput panel" })

    -- Custom keymap for running with `test_table`
    vim.keymap.set("n", "<leader>ts", function()
      run_with_test_table()
    end, { desc = "Run [t]ests with [s]ingle table test" })
  end,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions