Skip to content

Commit f98366c

Browse files
committed
refactor: more code cleanup
1 parent 4b3f93c commit f98366c

File tree

1 file changed

+126
-130
lines changed

1 file changed

+126
-130
lines changed

lua/neotest-stenciljs/init.lua

+126-130
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local logger = require("neotest.logging")
55
local util = require("neotest-stenciljs.util")
66

77
---@class neotest.StencilOptions
8-
---@field watch? boolean Run test(s) with the `--watchAll` flag
8+
---@field watch? boolean Run test(s) with the `--watch` flag
99
---@field no_build? boolean Run test(s) with the `--no-build` flag
1010
---@field env? table<string, string>|fun(): table<string, string> Set environment variables
1111
---@field cwd? string|fun(): string The current working directory for running tests
@@ -76,131 +76,6 @@ local function is_spec_test_file(file_path)
7676
return string.match(file_path, "%.spec%.tsx?$")
7777
end
7878

79-
adapter.root = function(path)
80-
return lib.files.match_root_pattern("package.json")(path)
81-
end
82-
83-
function adapter.filter_dir(name, relpath, root)
84-
return not vim.tbl_contains(
85-
{ "node_modules", "dist", "hydrate", "www", ".stencil", ".storybook" },
86-
name
87-
)
88-
end
89-
90-
---@param file_path? string
91-
---@return boolean
92-
function adapter.is_test_file(file_path)
93-
return file_path ~= nil
94-
and (is_e2e_test_file(file_path) or is_spec_test_file(file_path))
95-
and has_stencil_dep(file_path)
96-
end
97-
98-
---@async
99-
---@return neotest.Tree | nil
100-
function adapter.discover_positions(path)
101-
local query = [[
102-
; --------------------------------------------------------------------------
103-
; -- Namespaces --
104-
; --------------------------------------------------------------------------
105-
; Matches: `describe('context', () => {})`
106-
((call_expression
107-
function: (identifier) @func_name (#eq? @func_name "describe")
108-
arguments: (arguments
109-
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (arrow_function)
110-
)
111-
)) @namespace.definition
112-
; --------------------------------------------------------------------------
113-
; Matches: `describe('context', function() {})`
114-
((call_expression
115-
function: (identifier) @func_name (#eq? @func_name "describe")
116-
arguments: (arguments
117-
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (function_expression)
118-
)
119-
)) @namespace.definition
120-
; --------------------------------------------------------------------------
121-
; Matches: `describe.only('context', () => {})`
122-
((call_expression
123-
function: (member_expression
124-
object: (identifier) @func_name (#any-of? @func_name "describe")
125-
)
126-
arguments: (arguments
127-
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (arrow_function)
128-
)
129-
)) @namespace.definition
130-
; --------------------------------------------------------------------------
131-
; Matches: `describe.only('context', function() {})`
132-
((call_expression
133-
function: (member_expression
134-
object: (identifier) @func_name (#any-of? @func_name "describe")
135-
)
136-
arguments: (arguments
137-
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (function_expression)
138-
)
139-
)) @namespace.definition
140-
; --------------------------------------------------------------------------
141-
; Matches: `describe.each(['data'])('context', () => {})`
142-
((call_expression
143-
function: (call_expression
144-
function: (member_expression
145-
object: (identifier) @func_name (#any-of? @func_name "describe")
146-
)
147-
)
148-
arguments: (arguments
149-
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (arrow_function)
150-
)
151-
)) @namespace.definition
152-
; --------------------------------------------------------------------------
153-
; Matches: `describe.each(['data'])('context', function() {})`
154-
((call_expression
155-
function: (call_expression
156-
function: (member_expression
157-
object: (identifier) @func_name (#any-of? @func_name "describe")
158-
)
159-
)
160-
arguments: (arguments (string (string_fragment) @namespace.name) (function_expression))
161-
)) @namespace.definition
162-
; --------------------------------------------------------------------------
163-
; -- Tests --
164-
; --------------------------------------------------------------------------
165-
; Matches: `test('test') / it('test')`
166-
((call_expression
167-
function: (identifier) @func_name (#any-of? @func_name "it" "test")
168-
arguments: (arguments
169-
[(string (string_fragment) @test.name) (template_string (string_fragment) @test.name)] [(arrow_function) (function_expression)]
170-
)
171-
)) @test.definition
172-
; --------------------------------------------------------------------------
173-
; Matches: `test.only('test') / it.only('test')`
174-
((call_expression
175-
function: (member_expression
176-
object: (identifier) @func_name (#any-of? @func_name "test" "it")
177-
)
178-
arguments: (arguments
179-
[(string (string_fragment) @test.name) (template_string (string_fragment) @test.name)] [(arrow_function) (function_expression)]
180-
)
181-
)) @test.definition
182-
; --------------------------------------------------------------------------
183-
; Matches: `test.each(['data'])('test') / it.each(['data'])('test')`
184-
((call_expression
185-
function: (call_expression
186-
function: (member_expression
187-
object: (identifier) @func_name (#any-of? @func_name "it" "test")
188-
property: (property_identifier) @each_property (#eq? @each_property "each")
189-
)
190-
)
191-
arguments: (arguments
192-
[(string (string_fragment) @test.name) (template_string (string_fragment) @test.name)] [(arrow_function) (function_expression)]
193-
)
194-
)) @test.definition
195-
]]
196-
197-
return lib.treesitter.parse_positions(
198-
path,
199-
query,
200-
{ nested_tests = false, requires_namespace = false }
201-
)
202-
end
203-
20479
---@param path string
20580
---@return string
20681
local function get_stencil_command(path)
@@ -283,7 +158,6 @@ local function get_cwd(file_path)
283158
return nil
284159
end
285160

286-
local function parsed_json_to_results(data, output_file, consoleOut)
287161
local function parsed_json_to_results(data, output_file, console_out)
288162
local tests = {}
289163

@@ -342,6 +216,131 @@ local function parsed_json_to_results(data, output_file, console_out)
342216
return tests
343217
end
344218

219+
adapter.root = function(path)
220+
return lib.files.match_root_pattern("package.json")(path)
221+
end
222+
223+
function adapter.filter_dir(name, relpath, root)
224+
return not vim.tbl_contains(
225+
{ "node_modules", "dist", "hydrate", "www", ".stencil", ".storybook" },
226+
name
227+
)
228+
end
229+
230+
---@param file_path? string
231+
---@return boolean
232+
function adapter.is_test_file(file_path)
233+
return file_path ~= nil
234+
and (is_e2e_test_file(file_path) or is_spec_test_file(file_path))
235+
and has_stencil_dep(file_path)
236+
end
237+
238+
---@async
239+
---@return neotest.Tree | nil
240+
function adapter.discover_positions(path)
241+
local query = [[
242+
; --------------------------------------------------------------------------
243+
; -- Namespaces --
244+
; --------------------------------------------------------------------------
245+
; Matches: `describe('context', () => {})`
246+
((call_expression
247+
function: (identifier) @func_name (#eq? @func_name "describe")
248+
arguments: (arguments
249+
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (arrow_function)
250+
)
251+
)) @namespace.definition
252+
; --------------------------------------------------------------------------
253+
; Matches: `describe('context', function() {})`
254+
((call_expression
255+
function: (identifier) @func_name (#eq? @func_name "describe")
256+
arguments: (arguments
257+
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (function_expression)
258+
)
259+
)) @namespace.definition
260+
; --------------------------------------------------------------------------
261+
; Matches: `describe.only('context', () => {})`
262+
((call_expression
263+
function: (member_expression
264+
object: (identifier) @func_name (#any-of? @func_name "describe")
265+
)
266+
arguments: (arguments
267+
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (arrow_function)
268+
)
269+
)) @namespace.definition
270+
; --------------------------------------------------------------------------
271+
; Matches: `describe.only('context', function() {})`
272+
((call_expression
273+
function: (member_expression
274+
object: (identifier) @func_name (#any-of? @func_name "describe")
275+
)
276+
arguments: (arguments
277+
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (function_expression)
278+
)
279+
)) @namespace.definition
280+
; --------------------------------------------------------------------------
281+
; Matches: `describe.each(['data'])('context', () => {})`
282+
((call_expression
283+
function: (call_expression
284+
function: (member_expression
285+
object: (identifier) @func_name (#any-of? @func_name "describe")
286+
)
287+
)
288+
arguments: (arguments
289+
[(string (string_fragment) @namespace.name) (template_string (string_fragment) @namespace.name)] (arrow_function)
290+
)
291+
)) @namespace.definition
292+
; --------------------------------------------------------------------------
293+
; Matches: `describe.each(['data'])('context', function() {})`
294+
((call_expression
295+
function: (call_expression
296+
function: (member_expression
297+
object: (identifier) @func_name (#any-of? @func_name "describe")
298+
)
299+
)
300+
arguments: (arguments (string (string_fragment) @namespace.name) (function_expression))
301+
)) @namespace.definition
302+
; --------------------------------------------------------------------------
303+
; -- Tests --
304+
; --------------------------------------------------------------------------
305+
; Matches: `test('test') / it('test')`
306+
((call_expression
307+
function: (identifier) @func_name (#any-of? @func_name "it" "test")
308+
arguments: (arguments
309+
[(string (string_fragment) @test.name) (template_string (string_fragment) @test.name)] [(arrow_function) (function_expression)]
310+
)
311+
)) @test.definition
312+
; --------------------------------------------------------------------------
313+
; Matches: `test.only('test') / it.only('test')`
314+
((call_expression
315+
function: (member_expression
316+
object: (identifier) @func_name (#any-of? @func_name "test" "it")
317+
)
318+
arguments: (arguments
319+
[(string (string_fragment) @test.name) (template_string (string_fragment) @test.name)] [(arrow_function) (function_expression)]
320+
)
321+
)) @test.definition
322+
; --------------------------------------------------------------------------
323+
; Matches: `test.each(['data'])('test') / it.each(['data'])('test')`
324+
((call_expression
325+
function: (call_expression
326+
function: (member_expression
327+
object: (identifier) @func_name (#any-of? @func_name "it" "test")
328+
property: (property_identifier) @each_property (#eq? @each_property "each")
329+
)
330+
)
331+
arguments: (arguments
332+
[(string (string_fragment) @test.name) (template_string (string_fragment) @test.name)] [(arrow_function) (function_expression)]
333+
)
334+
)) @test.definition
335+
]]
336+
337+
return lib.treesitter.parse_positions(
338+
path,
339+
query,
340+
{ nested_tests = false, requires_namespace = false }
341+
)
342+
end
343+
345344
---@param args neotest.RunArgs
346345
---@return neotest.RunSpec | nil
347346
function adapter.build_spec(args)
@@ -427,7 +426,6 @@ function adapter.results(spec, b, tree)
427426
spec.context.stop_stream()
428427

429428
local output_file = spec.context.results_path
430-
431429
local success, data = pcall(lib.files.read, output_file)
432430

433431
if not success then
@@ -436,14 +434,12 @@ function adapter.results(spec, b, tree)
436434
end
437435

438436
local ok, parsed = pcall(vim.json.decode, data, { luanil = { object = true } })
439-
440437
if not ok then
441438
logger.error("Failed to parse test output json ", output_file)
442439
return {}
443440
end
444441

445442
local results = parsed_json_to_results(parsed, output_file, b.output)
446-
447443
return results
448444
end
449445

0 commit comments

Comments
 (0)