Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #50 support data-driven tests (it.each) #51

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion lua/neotest-vitest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ function adapter.is_test_file(file_path)
return is_test_file and hasVitestDependency(file_path)
end

local function get_match_type(captured_nodes)
if captured_nodes["test.name"] then
return "test"
end
if captured_nodes["namespace.name"] then
return "namespace"
end
end

-- Enrich `it.each` tests with metadata about TS node position
function adapter.build_position(file_path, source, captured_nodes)
local match_type = get_match_type(captured_nodes)
if not match_type then
return
end

---@type string
local name = vim.treesitter.get_node_text(captured_nodes[match_type .. ".name"], source)
local definition = captured_nodes[match_type .. ".definition"]

return {
type = match_type,
path = file_path,
name = name,
range = { definition:range() },
is_parameterized = captured_nodes["each_property"] and true or false,
}
end

---@async
---@return neotest.Tree | nil
function adapter.discover_positions(path)
Expand Down Expand Up @@ -141,13 +170,18 @@ function adapter.discover_positions(path)
function: (call_expression
function: (member_expression
object: (identifier) @func_name (#any-of? @func_name "it" "test")
property: (property_identifier) @each_property (#eq? @each_property "each")
)
)
arguments: (arguments (string (string_fragment) @test.name) (arrow_function))
)) @test.definition
]]

return lib.treesitter.parse_positions(path, query, { nested_tests = true })
return lib.treesitter.parse_positions(
path,
query,
{ nested_tests = false, build_position = 'require("neotest-vitest").build_position' }
)
end

---@param path string
Expand Down
3 changes: 3 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ if [ ! -d .testdeps ]; then
mkdir -p .testdeps

git clone --depth 1 https://github.com/nvim-lua/plenary.nvim.git .testdeps/plenary.nvim
git clone --depth 1 https://github.com/nvim-neotest/nvim-nio .testdeps/nvim-nio
git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter.git .testdeps/nvim-treesitter
git clone --depth 1 https://github.com/nvim-neotest/neotest.git .testdeps/neotest
fi


if [[ -n $1 ]]; then
nvim --headless --noplugin -u tests/init.vim -c "PlenaryBustedFile $1" | tee "${tempfile}"
else
Expand All @@ -23,6 +25,7 @@ rm "${tempfile}"

if [[ -n $errors ]]; then
echo "Tests failed"
echo "$errors"
exit 1
fi

Expand Down
1 change: 1 addition & 0 deletions tests/init.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set rtp+=.
set rtp+=.testdeps/plenary.nvim
set rtp+=.testdeps/nvim-nio
set rtp+=.testdeps/nvim-treesitter
set rtp+=.testdeps/neotest

Expand Down
2 changes: 1 addition & 1 deletion tests/init_options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("build_spec with override", function()
assert.contains(command, "--reporter=verbose")
--[[ assert.contains(command, "--config=" .. config_override()) ]]
assert.contains(command, "--testNamePattern=.*")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, "spec/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
assert.is.same(
Expand Down
18 changes: 15 additions & 3 deletions tests/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,34 @@ describe("discover_positions", function()
{
name = "basic.test.ts",
type = "file",
is_parametrized = false,
},
{
{
name = "describe text",
type = "namespace",
is_parametrized = false,
},
{
{
name = "1",
type = "test",
is_parametrized = false,
},
{
name = "2",
type = "test",
is_parametrized = false,
},
{
name = "3",
type = "test",
is_parametrized = false,
},
{
name = "4",
type = "test",
is_parametrized = false,
},
},
},
Expand All @@ -113,29 +119,35 @@ describe("discover_positions", function()
local expected_output = {
{
name = "array.test.ts",
is_parametrized = false,
type = "file",
},
{
{
name = "describe text",
is_parametrized = false,
type = "namespace",
},
{
{
name = "Array1",
type = "test",
is_parametrized = true
},
{
name = "Array2",
type = "test",
is_parametrized = true
},
{
name = "Array3",
type = "test",
is_parametrized = true
},
{
name = "Array4",
type = "test",
is_parametrized = true
},
},
},
Expand Down Expand Up @@ -172,7 +184,7 @@ describe("build_spec", function()
assert.contains(command, "--reporter=verbose")
assert.contains(command, "--testNamePattern=.*")
assert.contains(command, "--config=./spec/vite.config.ts")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, "spec/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand All @@ -193,7 +205,7 @@ describe("build_spec", function()
assert.contains(command, "--reporter=verbose")
assert.contains(command, "--testNamePattern=.*")
assert.contains(command, "--config=./spec/vite.config.ts")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, "spec/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand All @@ -215,7 +227,7 @@ describe("build_spec", function()
assert.contains(command, "--reporter=verbose")
assert.contains(command, "--testNamePattern=^ describe text")
assert.contains(command, "--config=./spec/vite.config.ts")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, "spec/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand Down