Neotest
adapter
for running tests using busted
with
neovim as the lua interpreter.
-l
option.busted
installed (in a project-local, user, or global location, see here).Setup with neotest. Leave values as nil
to leave them unspecified.
require("neotest").setup({
adapters = {
require("neotest-busted")({
-- Leave as nil to let neotest-busted automatically find busted
busted_command = "<path to a busted executable>",
-- Extra arguments to busted
busted_args = { "--shuffle-files" },
-- List of paths to add to package.path in neovim before running busted
busted_paths = { "my/custom/path/?.lua" },
-- List of paths to add to package.cpath in neovim before running busted
busted_cpaths = { "my/custom/path/?.so" },
-- Custom config to load via -u to set up testing.
-- If nil, will look for a 'minimal_init.lua' file
minimal_init = "custom_init.lua",
-- Only use a luarocks installation in the project's directory. If
-- true, installations in $HOME and global installations will be
-- ignored. Useful for isolating the test environment
local_luarocks_only = true,
-- Find parametric tests
parametric_test_discovery = false,
}),
},
})
Please refer to the official busted documentation.
Running an asynchronous test is done by wrapping the test function in a call to
async
. This also works for before_each
and after_each
.
local async = require("neotest-busted.async")
local control = require("neotest.async").control
describe("async", function()
before_each(async(function()
vim.print("async before_each")
end))
it("async test", async(function()
local timer = vim.loop.new_timer()
local event = control.event()
-- Print a message after 2 seconds
timer:start(2000, 0, function()
timer:stop()
timer:close()
vim.print("Hello from async test")
event.set()
end)
-- Wait for the timer to complete
event.wait()
end))
end)
The async
function takes an optional second timeout argument in milliseconds.
If omitted, uses the numerical value of either the
NEOTEST_BUSTED_ASYNC_TEST_TIMEOUT
or PLENARY_TEST_TIMEOUT
environment
variables or a default timeout of 2000 milliseconds.
[!IMPORTANT] Supporting parametric tests requires extra computation to discover them so support is disabled by default. You need to set
parametric_test_discovery
totrue
if you want neotest-busted to find parametric tests.
[!WARNING] There was a bug in neovim 0.9.0 that was fixed in 0.9.2 which shortened the output generated by lua scripts run using
nvim -l
. This is used internally to list all tests via busted for discovering parametric tests so if you experience issues, try upgrading.
neotest-busted
supports parametric tests that are generated at runtime as
opposed to being defined entirely at source-level as shown below. describe
's
can also be parametric and are also supported.
Due to technical limitations, parametric tests won't be shown in the neotest summary until they have been run.
describe("parametric tests", function()
for i = 1, 3 do
it(("test %d"):format(i), function()
assert.are.same(i, i)
end)
end
end)
neotest-busted
has support for debugging tests via local-lua-debugger-vscode
which can be set up via nvim-dap
. Once set up, you can set a breakpoint and run the test with the dap
strategy. Please refer to the neotest
documentation for more information.
Install luarocks from the website. neotest-busted
will try to find a busted
executable automatically at the different locations
listed below and in that priority (i.e. a directory-local install takes
precedence over a global install). You can check the installation by running
luarocks list busted
.
[!WARNING] If you have set
busted_command
to a non-nil value in thesetup
function,neotest-busted
will not know where to look for appropriate lua paths and will not look for installations as specified below to avoid setting up paths for a different busted installation.In this case, you should set
busted_paths
andbusted_cpaths
to appropriate paths.
You can install busted in your project's directory by running the following commands.
> cd <your_project>
> luarocks init
> luarocks config --scope project lua_version 5.1
> luarocks install busted
[!IMPORTANT] You need to set
local_luarocks_only
tofalse
forneotest-busted
to find your home directory installation.
The following command will install busted in your home directory.
> luarocks install --local busted
[!IMPORTANT] You need to set
local_luarocks_only
tofalse
forneotest-busted
to find your global installation.
> luarocks install busted
Thanks for considering to contribute. Please see the instructions here.
Yes. Please see the instructions here.
Busted removed support for async testing in version 2 (even though the docs still mention it) so you could install busted v1 but I haven't tested that.
neotest-busted
tested using plenary?The test could be run via neotest-busted
itself but I decided to use plenary
instead to use another test runner so that bugs in neotest-busted
won't affect
its own tests.