From 9bb5a4ab158761ebee2aede4aac257396b41b611 Mon Sep 17 00:00:00 2001 From: luttje <2738114+luttje@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:58:06 +0100 Subject: [PATCH] Fix tests not running in order --- libs/jestronaut/environment/runner.lua | 4 +-- libs/jestronaut/environment/state.lua | 16 ++++------ libs/jestronaut/expect/matchers/toEqual.lua | 2 +- .../expect/matchers/toHaveBeenCalledWith.lua | 6 ++-- .../matchers/toHaveBeenLastCalledWith.lua | 29 ++++++++++++------ .../matchers/toHaveBeenNthCalledWith.lua | 30 ++++++++++++------- 6 files changed, 50 insertions(+), 37 deletions(-) diff --git a/libs/jestronaut/environment/runner.lua b/libs/jestronaut/environment/runner.lua index 806c80f..bd80734 100644 --- a/libs/jestronaut/environment/runner.lua +++ b/libs/jestronaut/environment/runner.lua @@ -70,8 +70,7 @@ function TEST_RUNNER:queueTest(test) queuedTest.fn = testFnOrAsyncWrapper end - -- Put in front of the queue, so the tests are run in order - table.insert(self.queuedTests, 1, queuedTest) + table.insert(self.queuedTests, queuedTest) end function TEST_RUNNER:reset() @@ -134,7 +133,6 @@ function TEST_RUNNER:runTest(queuedTest) testFnParameter = queuedTest.asyncWrapper end - -- local status, errorMessage = pcall(testFn, testFnParameter) local status, errorMessage = xpcall(function() testFn(testFnParameter) end, function(err) diff --git a/libs/jestronaut/environment/state.lua b/libs/jestronaut/environment/state.lua index 0c1a545..968374e 100644 --- a/libs/jestronaut/environment/state.lua +++ b/libs/jestronaut/environment/state.lua @@ -707,23 +707,17 @@ local function runTests(runnerOptions) if describeOrTest.isTest then runner:queueTest(describeOrTest) end - end - - -- Find all nested describes and tests and add them to the runner queue - local i = 1 - - while i <= #testSetRoot.children do - local describeOrTest = testSetRoot.children[i] - - queueTestIfTest(describeOrTest) if describeOrTest.children then for _, child in ipairs(describeOrTest.children) do - table.insert(testSetRoot.children, i + 1, child) + queueTestIfTest(child) end end + end - i = i + 1 + -- Find all nested describes and tests and add them to the runner queue + for _, describeOrTest in ipairs(testSetRoot.children) do + queueTestIfTest(describeOrTest) end runner:start(testSetRoot, describesByFilePath, skippedTestCount) diff --git a/libs/jestronaut/expect/matchers/toEqual.lua b/libs/jestronaut/expect/matchers/toEqual.lua index 4dc8abd..947d59f 100644 --- a/libs/jestronaut/expect/matchers/toEqual.lua +++ b/libs/jestronaut/expect/matchers/toEqual.lua @@ -1,5 +1,5 @@ local wrapAndTagVarargsOrReturn = require "jestronaut/expect/asymmetricmatchers/varargsMatching" -.wrapAndTagVarargsOrReturn + .wrapAndTagVarargsOrReturn local asymmetricMatcherLib = require "jestronaut/expect/asymmetricmatchers/asymmetricmatcher" local tableLib = require "jestronaut/utils/tables" diff --git a/libs/jestronaut/expect/matchers/toHaveBeenCalledWith.lua b/libs/jestronaut/expect/matchers/toHaveBeenCalledWith.lua index bcc2652..c72b3b7 100644 --- a/libs/jestronaut/expect/matchers/toHaveBeenCalledWith.lua +++ b/libs/jestronaut/expect/matchers/toHaveBeenCalledWith.lua @@ -17,18 +17,18 @@ local function toHaveBeenCalledWith(expect, ...) error("Expected " .. tostring(actual) .. " to have been called with no arguments but it was called with " .. - tableLib.implode(actual:getAllCallArgs(), ", ")) + tableLib.implode(actual:getCallArgs(), ", ")) else error("Expected " .. tostring(actual) .. " to have been called with " .. tableLib.implode(args, ", ") .. - " but it was called with " .. tableLib.implode(actual:getAllCallArgs(), ", ")) + " but it was called with " .. tableLib.implode(actual:getCallArgs(), ", ")) end else error( "Expected " .. tostring(actual) .. " to have been called with " .. tostring(args) - .. " but it was called with " .. tableLib.implode(actual:getAllCallArgs(), ", ") + .. " but it was called with " .. tableLib.implode(actual:getCallArgs(), ", ") ) end end diff --git a/libs/jestronaut/expect/matchers/toHaveBeenLastCalledWith.lua b/libs/jestronaut/expect/matchers/toHaveBeenLastCalledWith.lua index 6e364cd..4aaf584 100644 --- a/libs/jestronaut/expect/matchers/toHaveBeenLastCalledWith.lua +++ b/libs/jestronaut/expect/matchers/toHaveBeenLastCalledWith.lua @@ -10,16 +10,27 @@ local function toHaveBeenLastCalledWith(expect, ...) if not expect:checkEquals(true, actual:wasLastCalledWith(...)) then local args = wrapAndTagVarargsOrReturn(...) - if tableLib.count(args) == 0 then - error("Expected " .. - tostring(actual) .. - " to have been called last with no arguments but it was called with " .. - tableLib.implode({ actual:getCallArgs() }, ", ")) + if (not actual:wasCalled()) then + error("Expected " .. tostring(actual) .. " to have been last called but it was never called") + elseif (type(args) == "table") then + if tableLib.count(args) == 0 then + error( + "Expected " .. tostring(actual) + .. " to have been called last with no arguments but it was last called with " + .. tableLib.implode({ actual:getCallArgs() }, ", ") + ) + else + error( + "Expected " .. tostring(actual) .. " to have been called last with " + .. tableImplode(args, ", ") .. " but it was last called with " + .. tableLib.implode({ actual:getCallArgs() }, ", ") + ) + end else - error("Expected " .. - tostring(actual) .. - " to have been called last with " .. - tableImplode(args, ", ") .. " but it was called with " .. tableLib.implode({ actual:getCallArgs() }, ", ")) + error( + "Expected " .. tostring(actual) .. " to have been last called with " .. tostring(args) + .. " but it was last called with " .. tableLib.implode(actual:getCallArgs(), ", ") + ) end end diff --git a/libs/jestronaut/expect/matchers/toHaveBeenNthCalledWith.lua b/libs/jestronaut/expect/matchers/toHaveBeenNthCalledWith.lua index 23de0d1..58e17ca 100644 --- a/libs/jestronaut/expect/matchers/toHaveBeenNthCalledWith.lua +++ b/libs/jestronaut/expect/matchers/toHaveBeenNthCalledWith.lua @@ -10,17 +10,27 @@ local function toHaveBeenNthCalledWith(expect, nthCall, ...) if not expect:checkEquals(true, actual:wasNthCalledWith(nthCall, ...)) then local args = wrapAndTagVarargsOrReturn(...) - if tableLib.count(args) == 0 then - error("Expected " .. - tostring(actual) .. - " to have been called with no arguments (on call " .. - nthCall .. ") but it was called with " .. tableLib.implode({ actual:getCallArgs() }, ", ")) + if (not actual:wasCalled()) then + error("Expected " .. tostring(actual) .. " to have been called but it was never called") + elseif (type(args) == "table") then + if tableLib.count(args) == 0 then + error( + "Expected " .. tostring(actual) + .. " to have been called with no arguments but it was called with " + .. tableLib.implode({ actual:getCallArgs() }, ", ") + ) + else + error( + "Expected " .. tostring(actual) .. " to have been called with " + .. tableImplode(args, ", ") .. " but it was called with " + .. tableLib.implode({ actual:getCallArgs() }, ", ") + ) + end else - error("Expected " .. - tostring(actual) .. - " to have been called with " .. - tableLib.implode(args, ", ") .. - " (on call " .. nthCall .. ") but it was called with " .. tableLib.implode({ actual:getCallArgs() }, ", ")) + error( + "Expected " .. tostring(actual) .. " to have been called with " .. tostring(args) + .. " but it was called with " .. tableLib.implode(actual:getCallArgs(), ", ") + ) end end