Skip to content

Commit

Permalink
Fix tests not running in order
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Dec 4, 2024
1 parent 055616b commit 9bb5a4a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
4 changes: 1 addition & 3 deletions libs/jestronaut/environment/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions libs/jestronaut/environment/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libs/jestronaut/expect/matchers/toEqual.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
6 changes: 3 additions & 3 deletions libs/jestronaut/expect/matchers/toHaveBeenCalledWith.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 20 additions & 9 deletions libs/jestronaut/expect/matchers/toHaveBeenLastCalledWith.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 20 additions & 10 deletions libs/jestronaut/expect/matchers/toHaveBeenNthCalledWith.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9bb5a4a

Please sign in to comment.