Skip to content

Commit 8092d71

Browse files
WIP on supporting object properties [skip ci]
1 parent d7206e2 commit 8092d71

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

lua/neotest-jest/init.lua

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function adapter.build_position(file_path, source, captured_nodes)
7171
local name = vim.treesitter.get_node_text(node, source)
7272
local definition = captured_nodes[match_type .. ".definition"]
7373
local type = node:type()
74+
local nonStringNode = false
7475

7576
if type == "string" then
7677
-- If the node is a string then strip the quotes from the name by getting
@@ -94,6 +95,8 @@ function adapter.build_position(file_path, source, captured_nodes)
9495
end
9596

9697
name = table.concat(new_name, "")
98+
else
99+
nonStringNode = true
97100
end
98101

99102
return {
@@ -103,7 +106,7 @@ function adapter.build_position(file_path, source, captured_nodes)
103106
range = { definition:range() },
104107
-- Record the position of the line where the string name occurs
105108
test_name_range = match_type == "test" and { node:range() } or nil,
106-
is_parameterized = captured_nodes["each_property"] and true or false,
109+
is_parameterized = (captured_nodes["each_property"] or nonStringNode) and true or false,
107110
}
108111
end
109112

@@ -335,10 +338,10 @@ end
335338

336339
local function cleanAnsi(s)
337340
return s:gsub("\x1b%[%d+;%d+;%d+;%d+;%d+m", "")
338-
:gsub("\x1b%[%d+;%d+;%d+;%d+m", "")
339-
:gsub("\x1b%[%d+;%d+;%d+m", "")
340-
:gsub("\x1b%[%d+;%d+m", "")
341-
:gsub("\x1b%[%d+m", "")
341+
:gsub("\x1b%[%d+;%d+;%d+;%d+m", "")
342+
:gsub("\x1b%[%d+;%d+;%d+m", "")
343+
:gsub("\x1b%[%d+;%d+m", "")
344+
:gsub("\x1b%[%d+m", "")
342345
end
343346

344347
local function findErrorPosition(file, errStr)
@@ -418,8 +421,16 @@ function adapter.build_spec(args)
418421
local testNamePattern = ".*"
419422

420423
if pos.type == types.PositionType.test or pos.type == types.PositionType.namespace then
421-
-- pos.id in form "path/to/file::Describe text::test text"
422-
local testName = pos.id:sub(pos.id:find("::") + 2)
424+
local temp = parameterized_tests.getParametricTestToSourceLevelTest(pos.path, pos.id)
425+
local testName
426+
427+
if temp then
428+
testName = temp:sub(pos.id:find("::") + 2)
429+
else
430+
-- pos.id in form "path/to/file::Describe text::test text"
431+
testName = pos.id:sub(pos.id:find("::") + 2)
432+
end
433+
423434
testName, _ = testName:gsub("::", " ")
424435
testNamePattern = util.escapeTestPattern(testName)
425436

@@ -452,7 +463,7 @@ function adapter.build_spec(args)
452463
}
453464

454465
local options =
455-
getJestArguments(jest_util.getJestDefaultArguments(jestArgsContext), jestArgsContext)
466+
getJestArguments(jest_util.getJestDefaultArguments(jestArgsContext), jestArgsContext)
456467

457468
if compat.tbl_islist(options) then
458469
vim.list_extend(command, options)
@@ -540,7 +551,7 @@ function adapter.results(spec, result, tree)
540551

541552
-- FIX: Generate results for source-level parametrized namespaces
542553
if
543-
adapter.jest_test_discovery == true and parameterized_tests.isPositionParameterized(tree, pos)
554+
adapter.jest_test_discovery == true and parameterized_tests.isPositionParameterized(tree, pos)
544555
then
545556
local status
546557

lua/neotest-jest/parameterized-tests.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ local M = {}
1111
---@field namespace_pos_id string
1212
---@field namespace_name string
1313

14+
---@type table<string, table<string, string>>
15+
local parametric_test_to_source_level_test = {}
16+
1417
local JEST_PARAMETER_TYPES = {
1518
"%%p",
1619
"%%s",
@@ -209,14 +212,15 @@ function M.enrichPositionsWithParameterizedTests(file_path, parsed_parameterized
209212
-- Get all runtime test information for path
210213
local jest_test_discovery_output = runJestTestDiscovery(file_path)
211214

212-
logger.warn(jest_test_discovery_output)
213-
214215
if jest_test_discovery_output == nil then
215216
return
216217
end
217218

218219
local tests_by_position = getTestsByPosition(jest_test_discovery_output)
219220

221+
-- Reset map
222+
parametric_test_to_source_level_test[file_path] = {}
223+
220224
-- For each parameterized test, find all tests that were in the same position
221225
-- as it and add new range-less (range = nil) children to the tree
222226
for _, tree in pairs(parsed_parameterized_tests_positions) do
@@ -256,7 +260,6 @@ function M.enrichPositionsWithParameterizedTests(file_path, parsed_parameterized
256260
-- There is no way for neotest-jest or jest to distinguish between
257261
-- tests that share the same name anyway so not creating new nodes is
258262
-- acceptable for now
259-
-- if hasTestParameters(tree, pos) then
260263
if not tree:get_key(test_result.pos_id) then
261264
createNewChildNode(
262265
ns_tree or tree,
@@ -272,7 +275,13 @@ function M.enrichPositionsWithParameterizedTests(file_path, parsed_parameterized
272275
source_pos_id = pos.id,
273276
}
274277
)
278+
279+
if not parametric_test_to_source_level_test[file_path] then
280+
parametric_test_to_source_level_test[file_path] = {}
281+
end
275282
end
283+
284+
parametric_test_to_source_level_test[file_path][pos.id] = test_result.pos_id
276285
end
277286
end
278287
end
@@ -329,4 +338,12 @@ function M.replaceTestParametersWithRegex(test_name)
329338
return result
330339
end
331340

341+
function M.getParametricTestToSourceLevelTest(path, pos_id)
342+
if parametric_test_to_source_level_test[path] then
343+
return parametric_test_to_source_level_test[path][pos_id]
344+
end
345+
346+
return nil
347+
end
348+
332349
return M
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Test {
2+
public prop: string = 'prop'
3+
}
4+
5+
const func1 = function () {}
6+
7+
const func2 = () => {}
8+
9+
describe('non-string test names', () => {
10+
it(Test, () => {
11+
12+
})
13+
14+
it(Test.prop, () => {
15+
16+
})
17+
18+
it(func1, () => {
19+
20+
})
21+
22+
it(func2, () => {
23+
24+
})
25+
26+
it(1, () => {
27+
28+
})
29+
})

0 commit comments

Comments
 (0)