diff --git a/gmod-addon/lua/sh_jestronaut_gmod_reporter.lua b/gmod-addon/lua/sh_jestronaut_gmod_reporter.lua index 88aed15..4cc45d6 100644 --- a/gmod-addon/lua/sh_jestronaut_gmod_reporter.lua +++ b/gmod-addon/lua/sh_jestronaut_gmod_reporter.lua @@ -96,7 +96,7 @@ end --- Prints the name of the test. --- @param describeOrTest DescribeOrTest -function REPORTER:testStarting(describeOrTest) +function REPORTER:onTestStarting(describeOrTest) -- Override print so there's no interference with the test output. print = function() end -- TODO: Store the print and output it at the end of the test. @@ -115,7 +115,7 @@ end --- Prints the result of the test. --- @param describeOrTest DescribeOrTest --- @param success boolean -function REPORTER:testFinished(describeOrTest, success) +function REPORTER:onTestFinished(describeOrTest, success) print = originalPrint local file = self:getFileByPath(describeOrTest.filePath) @@ -147,7 +147,7 @@ end --- Prints the skip message of the test. --- @param describeOrTest DescribeOrTest -function REPORTER:testSkipped(describeOrTest) +function REPORTER:onTestSkipped(describeOrTest) local file = self:getFileByPath(describeOrTest.filePath) if file then @@ -163,7 +163,7 @@ end --- Prints the retry message of the test. --- @param describeOrTest DescribeOrTest --- @param retryCount number -function REPORTER:testRetrying(describeOrTest, retryCount) +function REPORTER:onTestRetrying(describeOrTest, retryCount) self:redrawSummary(self.isVerbose) end @@ -198,8 +198,9 @@ end --- Stores the tests that will be run and prints the summary with header. --- @param rootDescribe Describe --- @param describesByFilePath table -function REPORTER:startTestSet(rootDescribe, describesByFilePath) - local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount +--- @param skippedTestCount number +function REPORTER:onStartTestSet(rootDescribe, describesByFilePath, skippedTestCount) + local totalTestCount = rootDescribe.childTestCount + rootDescribe.grandChildrenTestCount + skippedTestCount self.summaryHeader = styledText.new(nil, STYLING_DISABLED) :plain("🚀 Starting ") @@ -214,12 +215,13 @@ function REPORTER:startTestSet(rootDescribe, describesByFilePath) end --- Prints the success message of the test. ---- @param rootDescribe Describe +--- @param processedTests DescribeOrTest[] +--- @param passedTestCount number --- @param failedTestCount number --- @param skippedTestCount number ---- @param duration number -function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, duration) - local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount +--- @param testDuration number +function REPORTER:onEndTestSet(processedTests, passedTestCount, failedTestCount, skippedTestCount, testDuration) + local totalTestCount = passedTestCount + failedTestCount + skippedTestCount local notRunCount = failedTestCount + skippedTestCount local relativeSuccess = 1 - (notRunCount / totalTestCount) @@ -259,7 +261,7 @@ function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, dura originalPrint( styledText.new(nil, STYLING_DISABLED) - :plain("Time: " .. duration .. "s") + :plain("Time: " .. testDuration .. "s") ) self:printNewline() diff --git a/libs/jestronaut/environment/runner.lua b/libs/jestronaut/environment/runner.lua index 3d8c920..36f92a4 100644 --- a/libs/jestronaut/environment/runner.lua +++ b/libs/jestronaut/environment/runner.lua @@ -20,6 +20,7 @@ local function newTestRunner(runnerOptions) self.timeout = 5 self.isCompleted = false + self.isStarted = false self.preTestCallback = nil self.modifyTestResultCallback = nil @@ -74,6 +75,7 @@ end function TEST_RUNNER:reset() self.isCompleted = false + self.isStarted = false for _, test in ipairs(self.processedTests) do test.status = "starting" @@ -86,20 +88,17 @@ function TEST_RUNNER:reset() self.processedTests = {} end -function TEST_RUNNER:markFinished(test, status, err) - test.status = status - test.error = err +function TEST_RUNNER:markFinished(queuedTest, status, err) + queuedTest.status = status + queuedTest.error = err - -- TODO: Use reporter instead - if status == "passed" then - print(string.format("✅ PASSED %s", test.name)) - elseif status == "skipped" then - print(string.format("⏭️ SKIPPED %s", test.name)) + table.insert(self.processedTests, queuedTest) + + if (status == nil) then + self.reporter:onTestSkipped(queuedTest.test) else - print(string.format("❌ FAILED %s - %s", test.name, test.error or "Unknown error")) + self.reporter:onTestFinished(queuedTest.test, status, err) end - - table.insert(self.processedTests, test) end function TEST_RUNNER:runTest(queuedTest) @@ -130,15 +129,17 @@ function TEST_RUNNER:runTest(queuedTest) end -- TODO: Should this also run here for async tests? Even though those may not have finished yet? - if self.postTestCallback then - self.postTestCallback(queuedTest.test, queuedTest.status == "passed") + if self.postTestCallback and queuedTest.status ~= nil then + self.postTestCallback(queuedTest.test, queuedTest.status == true) end return status, errorMessage end -function TEST_RUNNER:statusToPassFailText(status) - return status and "passed" or "failed" +function TEST_RUNNER:start(rootDescribe, describesByFilePath, skippedTestCount) + self.isStarted = true + self.startTime = os.time() + self.reporter:onStartTestSet(rootDescribe, describesByFilePath, skippedTestCount) end function TEST_RUNNER:tick() @@ -146,6 +147,10 @@ function TEST_RUNNER:tick() return false end + if not self.isStarted then + error("Test runner not started! Did you forget to call `:start()`?") + end + local remainingTests = {} -- Process queued tests @@ -156,7 +161,7 @@ function TEST_RUNNER:tick() -- Run sync tests immediately local success, errorMessage = self:runTest(queuedTest) - self:markFinished(queuedTest, self:statusToPassFailText(success), errorMessage) + self:markFinished(queuedTest, success, errorMessage) elseif queuedTest.type == "async" then -- Start async test if not started if queuedTest.status == "starting" then @@ -167,7 +172,7 @@ function TEST_RUNNER:tick() -- Failed even while starting the test if not success then - self:markFinished(queuedTest, self:statusToPassFailText(success), errorMessage) + self:markFinished(queuedTest, success, errorMessage) else -- Test started, might need further processing queuedTest.result = queuedTest @@ -187,7 +192,7 @@ function TEST_RUNNER:tick() status, errorMessage = self.modifyTestResultCallback(queuedTest.test, status, errorMessage) end - self:markFinished(queuedTest, status and "passed" or "failed", errorMessage) + self:markFinished(queuedTest, status, errorMessage) elseif (queuedTest.asyncWrapper.isDone) then local errorMessage = queuedTest.asyncWrapper.errorMessage local success = not errorMessage @@ -196,7 +201,7 @@ function TEST_RUNNER:tick() success, errorMessage = self.modifyTestResultCallback(queuedTest.test, success, errorMessage) end - self:markFinished(queuedTest, self:statusToPassFailText(success), errorMessage) + self:markFinished(queuedTest, success, errorMessage) else -- Test still in progress table.insert(remainingTests, queuedTest) @@ -221,32 +226,30 @@ function TEST_RUNNER:finalize() return end - self:printResults() - self.isCompleted = true -end + local testDuration = os.time() - self.startTime + + self.isCompleted = true + self.isStarted = false -function TEST_RUNNER:printResults() - print("\n================================") - print("Test Results:") + self:printResults(testDuration) +end +function TEST_RUNNER:printResults(testDuration) local passed = 0 local failed = 0 local skipped = 0 for _, test in ipairs(self.processedTests) do - if test.status == "passed" then + if test.status == true then passed = passed + 1 - elseif test.status == "skipped" then + elseif test.status == nil then skipped = skipped + 1 else failed = failed + 1 end end - print(string.format("✅ Passed: %d", passed)) - print(string.format("⏭️ Skipped: %d", skipped)) - print(string.format("❌ Failed: %d", failed)) - print("================================\n") + self.reporter:onEndTestSet(self.processedTests, passed, failed, skipped, testDuration) end -- --[[ diff --git a/libs/jestronaut/environment/state.lua b/libs/jestronaut/environment/state.lua index e79995a..12e386a 100644 --- a/libs/jestronaut/environment/state.lua +++ b/libs/jestronaut/environment/state.lua @@ -209,7 +209,9 @@ local DESCRIBE_OR_TEST_META = { assertionCount = 0, parent = nil, childCount = 0, + childTestCount = 0, grandChildrenCount = 0, + grandChildrenTestCount = 0, --- @type DescribeOrTest[] children = nil @@ -222,6 +224,10 @@ DESCRIBE_OR_TEST_META.__index = DESCRIBE_OR_TEST_META function DESCRIBE_OR_TEST_META:addChild(child) self.childCount = self.childCount + 1 + if child.isTest then + self.childTestCount = self.childTestCount + 1 + end + self.children[self.childCount] = child self.childrenLookup[child.name] = self.childCount @@ -229,6 +235,10 @@ function DESCRIBE_OR_TEST_META:addChild(child) if self.parent then self.parent.grandChildrenCount = self.parent.grandChildrenCount + 1 + + if child.isTest then + self.parent.grandChildrenTestCount = self.parent.grandChildrenTestCount + 1 + end end end @@ -253,7 +263,7 @@ end -- local failedTestCount = 0 -- if self.toSkip then --- reporter:testSkipped(self) +-- reporter:onTestSkipped(self) -- return failedTestCount -- end @@ -269,7 +279,7 @@ end -- end -- if self.isTest then --- reporter:testStarting(self) +-- reporter:onTestStarting(self) -- self.isRunning = true -- local testLocalState = getTestLocalState(self.filePath) @@ -296,7 +306,7 @@ end -- end -- if (success or (not retrySettings or retrySettings.options.logErrorsBeforeRetry)) then --- reporter:testFinished(self, success) +-- reporter:onTestFinished(self, success) -- end -- if not success then @@ -306,14 +316,14 @@ end -- if retrySettings.timesRemaining > 0 then -- retrySettings.timesRemaining = retrySettings.timesRemaining - 1 --- reporter:testRetrying(self, retrySettings.timesRemaining + 1) +-- reporter:onTestRetrying(self, retrySettings.timesRemaining + 1) -- return self:run(reporter, runnerOptions) -- end -- end -- if runnerOptions.bail ~= nil and failedTestCount >= runnerOptions.bail then --- reporter:testFinished(self, success) +-- reporter:onTestFinished(self, success) -- error( -- "Bail after " .. failedTestCount .. " failed " @@ -336,7 +346,7 @@ end -- self.success = failedTestCount == 0 -- end --- reporter:testFinished(self, self.success) +-- reporter:onTestFinished(self, self.success) -- end -- return failedTestCount @@ -420,7 +430,7 @@ FILE_FOR_RUN.__index = FILE_FOR_RUN --- TODO: table is that useful anymore. --- @param describeOrTest DescribeOrTest --- @param runnerOptions RunnerOptions ---- @return DescribeOrTestForRun, table, number, table +--- @return DescribeOrTestForRun, table, number local function copyDescribeOrTestForRun(describeOrTest, runnerOptions) local describeOrTestForRun, skippedTestCount = makeDescribeOrTestForRun(describeOrTest, runnerOptions) local describesByFilePath = {} @@ -457,7 +467,7 @@ local function copyDescribeOrTestForRun(describeOrTest, runnerOptions) table.insert(describesByFilePath[fileIndex].describesOrTests, child) end - if describeOrTest.isDescribe then + if describeOrTestForRun.children then for _, child in pairs(describeOrTest.children) do local child, childDescribesByFilePath, childSkippedCount = copyDescribeOrTestForRun(child, runnerOptions) @@ -465,9 +475,7 @@ local function copyDescribeOrTestForRun(describeOrTest, runnerOptions) skippedTestCount = skippedTestCount + childSkippedCount end - end - if describeOrTestForRun.children then for _, child in pairs(describeOrTestForRun.children) do insertChildWithFile(child) end @@ -716,6 +724,7 @@ local function runTests(runnerOptions) i = i + 1 end + runner:start(testSetRoot, describesByFilePath, skippedTestCount) runnerOptions.eventLoopTicker(function() return runner:tick() end) diff --git a/libs/jestronaut/reporter-minimal.lua b/libs/jestronaut/reporter-minimal.lua index 549315c..22c8a58 100644 --- a/libs/jestronaut/reporter-minimal.lua +++ b/libs/jestronaut/reporter-minimal.lua @@ -95,7 +95,7 @@ end --- Prints the name of the test. --- @param describeOrTest DescribeOrTest -function REPORTER:testStarting(describeOrTest) +function REPORTER:onTestStarting(describeOrTest) local file = self:getFileByPath(describeOrTest.filePath) if file then @@ -111,7 +111,7 @@ end --- Prints the result of the test. --- @param describeOrTest DescribeOrTest --- @param success boolean -function REPORTER:testFinished(describeOrTest, success) +function REPORTER:onTestFinished(describeOrTest, success) local file = self:getFileByPath(describeOrTest.filePath) if file then @@ -141,7 +141,7 @@ end --- Prints the skip message of the test. --- @param describeOrTest DescribeOrTest -function REPORTER:testSkipped(describeOrTest) +function REPORTER:onTestSkipped(describeOrTest) local file = self:getFileByPath(describeOrTest.filePath) if file then @@ -157,7 +157,7 @@ end --- Prints the retry message of the test. --- @param describeOrTest DescribeOrTest --- @param retryCount number -function REPORTER:testRetrying(describeOrTest, retryCount) +function REPORTER:onTestRetrying(describeOrTest, retryCount) self:redrawSummary(self.isVerbose) end @@ -192,8 +192,9 @@ end --- Stores the tests that will be run and prints the summary with header. --- @param rootDescribe Describe --- @param describesByFilePath table -function REPORTER:startTestSet(rootDescribe, describesByFilePath) - local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount +--- @param skippedTestCount number +function REPORTER:onStartTestSet(rootDescribe, describesByFilePath, skippedTestCount) + local totalTestCount = rootDescribe.childTestCount + rootDescribe.grandChildrenTestCount + skippedTestCount self.summaryHeader = styledText.new(nil, STYLING_DISABLED) :plain("🚀 Starting ") @@ -208,12 +209,13 @@ function REPORTER:startTestSet(rootDescribe, describesByFilePath) end --- Prints the success message of the test. ---- @param rootDescribe Describe +--- @param processedTests DescribeOrTest[] +--- @param passedTestCount number --- @param failedTestCount number --- @param skippedTestCount number ---- @param duration number -function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, duration) - local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount +--- @param testDuration number +function REPORTER:onEndTestSet(processedTests, passedTestCount, failedTestCount, skippedTestCount, testDuration) + local totalTestCount = passedTestCount + failedTestCount + skippedTestCount local notRunCount = failedTestCount + skippedTestCount local relativeSuccess = 1 - (notRunCount / totalTestCount) @@ -253,7 +255,7 @@ function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, dura originalPrint( styledText.new(nil, STYLING_DISABLED) - :plain("Time: " .. duration .. "s") + :plain("Time: " .. testDuration .. "s") ) self:printNewline() diff --git a/libs/jestronaut/reporter.lua b/libs/jestronaut/reporter.lua index f798342..58ccdd0 100644 --- a/libs/jestronaut/reporter.lua +++ b/libs/jestronaut/reporter.lua @@ -148,7 +148,7 @@ end --- Prints the name of the test. --- @param describeOrTest DescribeOrTest -function REPORTER:testStarting(describeOrTest) +function REPORTER:onTestStarting(describeOrTest) -- Override print so there's no interference with the test output. print = function() end -- TODO: Store the print and output it at the end of the test. @@ -165,7 +165,7 @@ end --- Prints the result of the test. --- @param describeOrTest DescribeOrTest --- @param success boolean -function REPORTER:testFinished(describeOrTest, success) +function REPORTER:onTestFinished(describeOrTest, success) print = originalPrint local file = self:getFileByPath(describeOrTest.filePath) @@ -194,7 +194,7 @@ end --- Prints the skip message of the test. --- @param describeOrTest DescribeOrTest -function REPORTER:testSkipped(describeOrTest) +function REPORTER:onTestSkipped(describeOrTest) local file = self:getFileByPath(describeOrTest.filePath) if file then @@ -209,7 +209,7 @@ end --- Prints the retry message of the test. --- @param describeOrTest DescribeOrTest --- @param retryCount number -function REPORTER:testRetrying(describeOrTest, retryCount) +function REPORTER:onTestRetrying(describeOrTest, retryCount) self:redrawSummary(self.isVerbose) end @@ -244,8 +244,9 @@ end --- Stores the tests that will be run and prints the summary with header. --- @param rootDescribe Describe --- @param describesByFilePath table -function REPORTER:startTestSet(rootDescribe, describesByFilePath) - local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount +--- @param skippedTestCount number +function REPORTER:onStartTestSet(rootDescribe, describesByFilePath, skippedTestCount) + local totalTestCount = rootDescribe.childTestCount + rootDescribe.grandChildrenTestCount + skippedTestCount self.summaryHeader = styledText.new() :plain("🚀 Starting ") @@ -259,12 +260,13 @@ function REPORTER:startTestSet(rootDescribe, describesByFilePath) end --- Prints the success message of the test. ---- @param rootDescribe Describe +--- @param processedTests DescribeOrTest[] +--- @param passedTestCount number --- @param failedTestCount number --- @param skippedTestCount number ---- @param duration number -function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, duration) - local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount +--- @param testDuration number +function REPORTER:onEndTestSet(processedTests, passedTestCount, failedTestCount, skippedTestCount, testDuration) + local totalTestCount = passedTestCount + failedTestCount + skippedTestCount local notRunCount = failedTestCount + skippedTestCount local relativeSuccess = 1 - (notRunCount / totalTestCount) @@ -305,7 +307,7 @@ function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, dura originalPrint( styledText.new() - :plain("Time: " .. duration .. "s") + :plain("Time: " .. testDuration .. "s") ) originalPrint( styledText.new()