diff --git a/lua/gluatest/runner/helpers.lua b/lua/gluatest/runner/helpers.lua index 13eabd8..22f44ae 100644 --- a/lua/gluatest/runner/helpers.lua +++ b/lua/gluatest/runner/helpers.lua @@ -194,7 +194,7 @@ function Helpers.FailCallback( reason ) } end -function Helpers.MakeAsyncEnv( onDone, onFailedExpectation ) +function Helpers.MakeAsyncEnv( done, fail, onFailedExpectation ) -- TODO: How can we make Stubs safer in Async environments? local stub, stubCleanup = stubMaker() local testEnv, envCleanup = makeTestLibStubs() @@ -227,7 +227,8 @@ function Helpers.MakeAsyncEnv( onDone, onFailedExpectation ) return built end, - done = onDone, + done = done, + fail = fail, stub = stub, }, { diff --git a/lua/gluatest/runner/runner.lua b/lua/gluatest/runner/runner.lua index 42d4006..3570cab 100644 --- a/lua/gluatest/runner/runner.lua +++ b/lua/gluatest/runner/runner.lua @@ -210,7 +210,7 @@ return function( allTestGroups ) checkComplete() end - local onDone = function() + local done = function() if callbacks[case.id] ~= nil then return end if not expectationFailure then @@ -221,6 +221,14 @@ return function( allTestGroups ) case.testComplete() end + local fail = function( reason ) + if callbacks[case.id] ~= nil then return end + + setFailed( case, { reason = reason or "fail() called" } ) + callbacks[case.id] = false + case.testComplete() + end + -- Received an expectation failure -- We will record it here, but still expect them -- to call done(). @@ -234,7 +242,7 @@ return function( allTestGroups ) expectationFailure = true end - local asyncEnv, asyncCleanupFunc = MakeAsyncEnv( onDone, onFailedExpectation ) + local asyncEnv, asyncCleanupFunc = MakeAsyncEnv( done, fail, onFailedExpectation ) asyncCleanup = asyncCleanupFunc setfenv( testGroup.beforeEach, asyncEnv ) @@ -270,6 +278,7 @@ return function( allTestGroups ) for id, case in pairs( asyncCases ) do if callbacks[id] == nil then setTimedOut( case ) + callbacks[case.id] = false local shouldCheckComplete = false case.testComplete( shouldCheckComplete )