From e5e6691b73c607610a51b94de3346844a8540c73 Mon Sep 17 00:00:00 2001 From: Steve Vermeulen Date: Wed, 20 Sep 2023 03:34:19 +0800 Subject: [PATCH] fix(json-output): convert error object to string before encoding (#730) --- busted/core.lua | 2 +- busted/outputHandlers/json.lua | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/busted/core.lua b/busted/core.lua index a00912db..6250e108 100644 --- a/busted/core.lua +++ b/busted/core.lua @@ -79,7 +79,7 @@ return function() end info.traceback = debug.traceback('', level) - info.message = msg + info.message = tostring(msg) local file = busted.getFile(element) return file and file.getTrace(file.name, info) or trimTrace(info) diff --git a/busted/outputHandlers/json.lua b/busted/outputHandlers/json.lua index 880e639d..97a07f37 100644 --- a/busted/outputHandlers/json.lua +++ b/busted/outputHandlers/json.lua @@ -7,13 +7,21 @@ return function(options) local handler = require 'busted.outputHandlers.base'() handler.suiteEnd = function() - io_write(json.encode({ + local error_info = { pendings = handler.pendings, successes = handler.successes, failures = handler.failures, errors = handler.errors, duration = handler.getDuration() - })) + } + local ok, result = pcall(json.encode, error_info) + + if ok then + io_write(result) + else + io_write("Failed to encode test results to json: " .. result) + end + io_write("\n") io_flush()