Skip to content

Commit

Permalink
(wip) fixing reporter not showing describes
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Dec 7, 2024
1 parent 382260f commit e4c588d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 89 deletions.
4 changes: 2 additions & 2 deletions libs/jestronaut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ local JESTRONAUT = {
function JESTRONAUT:configure(runnerOptions)
environmentLib.resetEnvironment()

-- Setup the root describe
describeLib.describe("root", function() end)
-- Setup the root describe where every other describe and test will be nested under
describeLib.describeTransparent("root", function() end)

runnerOptions = optionsLib.merge(runnerOptions)

Expand Down
22 changes: 18 additions & 4 deletions libs/jestronaut/environment/describe.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
local DESCRIBE_OR_TEST_META = require "jestronaut/environment/state".DESCRIBE_OR_TEST_META
local registerDescribeOrTest = require "jestronaut/environment/state".registerDescribeOrTest
local extendMetaTableIndex = require "jestronaut/utils/metatables".extendMetaTableIndex
local stateLib = require "jestronaut/environment/state"

--- @class Describe
local DESCRIBE_META = {
isDescribe = true,
}

extendMetaTableIndex(DESCRIBE_META, DESCRIBE_OR_TEST_META)
extendMetaTableIndex(DESCRIBE_META, stateLib.DESCRIBE_OR_TEST_META)

--- Creates a new describe.
--- @param name string
Expand All @@ -34,7 +33,7 @@ local function _internalDescribe(name, fn, options)

setmetatable(describe, DESCRIBE_META)

registerDescribeOrTest(describe)
stateLib.registerDescribeOrTest(describe)

return describe
end
Expand Down Expand Up @@ -73,6 +72,19 @@ local function describeSkip(self, name, fn)
return _describe
end

--- Creates a new describe that is transparent, meaning the user will
--- not see it, but it is used for internal purposes like marking root.
--- @param name string
--- @param fn function
--- @param arguments? table
--- @return Describe
local function describeTransparent(name, fn, arguments)
arguments = arguments or {}
arguments.isTransparent = true

return _internalDescribe(name, fn, arguments)
end

return {
describe = describe,

Expand All @@ -81,4 +93,6 @@ return {

describeSkip = describeSkip,
xdescribe = describeSkip,

describeTransparent = describeTransparent,
}
65 changes: 56 additions & 9 deletions libs/jestronaut/environment/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local function newTestRunner(runnerOptions)
self.reporter = callRespectingRequireOverride(function()
return runnerOptions.reporter or (require "jestronaut/reporter".newDefaultReporter())
end)

self.reporter.isVerbose = runnerOptions.verbose

self.runnerOptions = runnerOptions
Expand Down Expand Up @@ -63,7 +64,8 @@ function TEST_RUNNER:queueTest(test)
status = "starting",
shouldSkip = test.toSkip,

test = test,
test = not test.isDescribe and test or nil,
describe = test.isDescribe and test or nil,
}

if isAsync then
Expand All @@ -89,6 +91,14 @@ function TEST_RUNNER:reset()
end

self.processedTests = {}
self.describeMap = {}

-- Map describes and tests, so we can track which ones are done
for _, queuedTest in ipairs(self.queuedTests) do
if queuedTest.test then
self.describeMap[queuedTest.test] = queuedTest
end
end
end

function TEST_RUNNER:markFinished(queuedTest, status, err, retryWithRemainingCount)
Expand All @@ -107,6 +117,32 @@ function TEST_RUNNER:markFinished(queuedTest, status, err, retryWithRemainingCou
else
self.reporter:onTestFinished(queuedTest.test, status, err)
end

self:updateDescribeMap(queuedTest.test)
end

function TEST_RUNNER:updateDescribeMap(testOrDescribe)
-- Remove this test from the map, and check if the describe is done
self.describeMap[testOrDescribe] = nil

local describe = testOrDescribe.parent

if describe then
local describeDone = true

for _, child in ipairs(describe.children) do
if self.describeMap[child] then
describeDone = false
break
end
end

if describeDone then
self.reporter:onTestFinished(describe, true)

self:updateDescribeMap(describe)
end
end
end

function TEST_RUNNER:handleTestFinished(queuedTest, success, errorMessage)
Expand Down Expand Up @@ -206,13 +242,22 @@ function TEST_RUNNER:tick()
error("Test runner not started! Did you forget to call `:start()`?")
end

local slowDown = self.runnerOptions.slowDown
local remainingTests = {}

-- Process queued tests
for i, queuedTest in ipairs(self.queuedTests) do
self.currentTestIndex = i

if (queuedTest.shouldSkip) then
if (slowDown) then
os.execute("sleep " .. (slowDown * .001))
end

self.reporter:onTestStarting(queuedTest.test or queuedTest.describe)

if (queuedTest.describe) then
table.insert(self.processedTests, queuedTest)
elseif (queuedTest.shouldSkip) then
self:handleTestFinished(queuedTest, nil, "Test skipped")
elseif queuedTest.type == "sync" then
-- Run sync tests immediately
Expand Down Expand Up @@ -296,13 +341,15 @@ function TEST_RUNNER:printResults(testDuration)
local failed = 0
local skipped = 0

for _, test in ipairs(self.processedTests) do
if test.status == true then
passed = passed + 1
elseif test.status == nil then
skipped = skipped + 1
else
failed = failed + 1
for _, processedTest in ipairs(self.processedTests) do
if processedTest.test then
if processedTest.status == true then
passed = passed + 1
elseif processedTest.status == nil then
skipped = skipped + 1
else
failed = failed + 1
end
end
end

Expand Down
5 changes: 2 additions & 3 deletions libs/jestronaut/environment/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ local function registerDescribeOrTest(describeOrTest)
testFunctionLookupByRegistered[describeOrTest] = testFunctionLookup[filePath][lookupIndex]

if not currentParent then
assert(describeOrTest.name == "root", "Root describe not set. Use `jestronaut.describe('root', function() end)` to set the root describe.")
currentParent = describeOrTest
else
currentParent:addChild(describeOrTest)
Expand Down Expand Up @@ -626,9 +627,7 @@ local function runTests(runnerOptions)
local testSetRootCopy, describesByFilePath, skippedTestCount = copyDescribeOrTestForRun(currentParent, runnerOptions)

local function queueTestIfTest(describeOrTestCopy)
if describeOrTestCopy.isTest then
runner:queueTest(describeOrTestCopy)
end
runner:queueTest(describeOrTestCopy)

if describeOrTestCopy.children then
for _, childCopy in ipairs(describeOrTestCopy.children) do
Expand Down
81 changes: 43 additions & 38 deletions libs/jestronaut/reporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,13 @@ local function drawDescribeOrTest(describeOrTest)

summary:plain(" " .. describeOrTest.name .. "\n")

if (describeOrTest.hasRun and not describeOrTest.success) then
summary:plain(describeOrTest.errorMessage .. "\n\n")
end

if (describeOrTest.isRunning and describeOrTest.children) then
for _, describeOrTest in ipairs(describeOrTest.children) do
if describeOrTest.isDescribe then
summary:plain(drawDescribeOrTest(describeOrTest))
else
summary:plain(getIndentations(describeOrTest))

if describeOrTest.hasRun then
if describeOrTest.success then
summary:colored("", styledText.foregroundColors.green)
else
summary:colored("", styledText.foregroundColors.red)
end
elseif describeOrTest.toSkip then
summary:colored("", styledText.foregroundColors.blue)
else
summary:colored("o", styledText.foregroundColors.yellow)
end

summary:plain(" " .. describeOrTest.name .. "\n")

if (describeOrTest.hasRun and not describeOrTest.success) then
summary:plain(describeOrTest.errorMessage .. "\n\n")
end
end
summary:plain(drawDescribeOrTest(describeOrTest))
end
elseif (describeOrTest.hasRun and not describeOrTest.success) then
summary:plain(describeOrTest.errorMessage .. "\n\n")
Expand Down Expand Up @@ -158,8 +140,9 @@ function REPORTER:onTestStarting(describeOrTest)
file.isRunning = true
end

describeOrTest.isRunning = true

self:redrawSummary(self.isVerbose)
return
end

--- Prints the result of the test.
Expand All @@ -171,24 +154,44 @@ function REPORTER:onTestFinished(describeOrTest, success)
local file = self:getFileByPath(describeOrTest.filePath)

if file then
if not self.lastFile then
self.lastFile = file
elseif self.lastFile ~= file then
self.lastFile.isRunning = false
self.lastFile.hasRun = true
self.lastFile.success = true -- TODO: Check if all tests passed.

self.lastFile = file
-- Commented, since this wont work for async tests. We need
-- to keep track if all describes in a file have run instead
-- if not self.lastFile then
-- self.lastFile = file
-- elseif self.lastFile ~= file then
-- self.lastFile.isRunning = false
-- self.lastFile.hasRun = true
-- self.lastFile.success = true -- TODO: Check if all tests passed.

-- self.lastFile = file
-- end
local allDescribesInFileHaveRun = true

for _, describe in ipairs(file.describesOrTests) do
if not describe.hasRun then
allDescribesInFileHaveRun = false
break
end
end

file.isRunning = true
if allDescribesInFileHaveRun then
file.isRunning = false
file.hasRun = true
file.success = true
else
file.isRunning = true
end

if not success then
file.hasRun = true
file.success = false
end
end

describeOrTest.success = success
describeOrTest.hasRun = true
describeOrTest.isRunning = false

self:redrawSummary(self.isVerbose)
end

Expand Down Expand Up @@ -270,11 +273,13 @@ function REPORTER:onEndTestSet(processedTests, passedTestCount, failedTestCount,
local notRunCount = failedTestCount + skippedTestCount
local relativeSuccess = 1 - (notRunCount / totalTestCount)

if self.lastFile then
self.lastFile.isRunning = false
self.lastFile.hasRun = true
self.lastFile.success = true -- TODO: Check if all tests passed?
end
-- Commented, since this wont work for async tests. We need
-- to keep track if all describes in a file have run instead
-- if self.lastFile then
-- self.lastFile.isRunning = false
-- self.lastFile.hasRun = true
-- self.lastFile.success = true -- TODO: Check if all tests passed?
-- end

self:redrawSummary()
self:printNewline()
Expand Down
5 changes: 4 additions & 1 deletion test-tmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ require "jestronaut":withGlobals()

jestronaut
:configure({
bail = 1,
roots = {
"./tests/",
},

reporter = require "jestronaut.reporter-minimal".newMinimalReporter(),
-- reporter = require "jestronaut.reporter-minimal".newMinimalReporter(),
verbose = true,
slowDown = 50,
})
:registerTests(function()
-- Setup and register the tests:
Expand Down
Loading

0 comments on commit e4c588d

Please sign in to comment.