--[[
	Runs the given TestPlan and returns a TestResults object representing the
	results of the run.
]]

function TestRunner.runPlan(plan)
	local session = TestSession.new(plan)
	local lifecycleHooks = LifecycleHooks.new()

	local exclusiveNodes = plan:findNodes(function(node)
		return node.modifier == TestEnum.NodeModifier.Focus
	end)

	session.hasFocusNodes = #exclusiveNodes > 0

	_G[JEST_TEST_CONTEXT] = {
		blocks = {},
		instance = nil,
		snapshotState = nil,
	}

	-- ROBLOX deviation START: adding startTime so that jest reporters can report time elapsed for tests
	local fenv = getfenv()
	-- ROBLOX NOTE: additional check as DateTime only exists in Luau and not in native Lua
	if fenv.DateTime then
		session.results.startTime = fenv.DateTime.now().UnixTimestampMillis
	end
	-- ROBLOX deviation END

	TestRunner.runPlanNode(session, plan, lifecycleHooks)

	_G[JEST_TEST_CONTEXT] = nil

	return session:finalize()
end
