From a0c35f90b5ca23f8ef30ac47a9eddf35e97d815a Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Sat, 4 Nov 2023 07:09:50 +0000 Subject: [PATCH] refactor(vim): move jest commands to use Run rather than run in process The jest out put is quite hard to process. This points that were add into the quickfix list where often incorrect. This is a bit pointless and I would find myself running it again in the terminal to see all of the output. This will have todo until I can find time to create proper test runner, I really don't like the look of any of the plugins available. --- .../core/files/vim/plugin/ts-jest.lua | 97 ++----------------- 1 file changed, 8 insertions(+), 89 deletions(-) diff --git a/site-modules/core/files/vim/plugin/ts-jest.lua b/site-modules/core/files/vim/plugin/ts-jest.lua index f9e1340..8699a06 100644 --- a/site-modules/core/files/vim/plugin/ts-jest.lua +++ b/site-modules/core/files/vim/plugin/ts-jest.lua @@ -68,105 +68,24 @@ local function get_all_tests() return tests end -local function parse_results(results) - local issues = {} - - for _, result in ipairs(results.testResults) do - if result.status ~= "passed" then - local file = result.message:match "([^:(]+):%d+:%d+" - local line = result.message:match "[^:]+:(%d+):%d+" - local message = result.message:match "\n\n%s+([^\n]+)" - - table.insert(issues, { text = message, filename = file, lnum = line }) - end - end - - return issues -end +local base_jest_command = "jest --maxWorkers=25\\% --silent" local function run_at_cursor() - vim.fn.jobstart({ - "jest", - "--maxWorkers=25%", - "--silent", - "--json", - "-t", - get_test_name_at_cursor(), - vim.fn.expand "%", - }, { - stdout_buffered = true, - on_stdout = function(_, output) - local results = vim.json.decode(output[1]) - local issues = parse_results(results) - - if #issues == 0 then - -- print "All tests passed" - else - vim.fn.setqflist({}, "r", { title = "Jest Failers", items = issues }) - vim.cmd "copen" - end - end, - on_stderr = function(_, data) - -- This will display the progress for the tests - print(data[1]) - end, - }) + local command = string.format("%s -t '%s' %s", base_jest_command, get_test_name_at_cursor(), vim.fn.expand "%") + vim.cmd("Run " .. command) end local function run_file() - vim.fn.jobstart({ - "jest", - "--maxWorkers=25%", - "--silent", - "--json", - vim.fn.expand "%", - }, { - stdout_buffered = true, - on_stdout = function(_, output) - local results = vim.json.decode(output[1]) - local issues = parse_results(results) - - if #issues == 0 then - -- print "All tests passed" - else - vim.fn.setqflist({}, "r", { title = "Jest Failers", items = issues }) - vim.cmd "copen" - end - end, - on_stderr = function(_, data) - -- This will display the progress for the tests - print(data[1]) - end, - }) + local command = string.format("%s %s", base_jest_command, vim.fn.expand "%") + vim.cmd("Run " .. command) end local function run_all() - vim.fn.jobstart({ - "jest", - "--maxWorkers=25%", - "--silent", - "--json", - }, { - stdout_buffered = true, - on_stdout = function(_, output) - local results = vim.json.decode(output[1]) - local issues = parse_results(results) - - if #issues == 0 then - -- print "All tests passed" - else - vim.fn.setqflist({}, "r", { title = "Jest Failers", items = issues }) - vim.cmd "copen" - end - end, - on_stderr = function(_, data) - -- This will display the progress for the tests - print(data[1]) - end, - }) + vim.cmd("Run " .. base_jest_command) end vim.api.nvim_create_user_command("Jest", run_all, { bang = true }) vim.api.nvim_create_user_command("JestFile", run_file, { bang = true }) vim.api.nvim_create_user_command("JestAtCursor", run_at_cursor, { bang = true }) -vim.api.nvim_set_keymap("n", "t", "JestAtCursor", { nowait = true, silent = true }) +vim.api.nvim_set_keymap("n", "tt", "JestAtCursor", { nowait = true, silent = true }) +vim.api.nvim_set_keymap("n", "tf", "JestFile", { nowait = true, silent = true })