Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Added beforeAll, beforeEach, afterEach, afterAll lifecycle hooks for testing #57

Merged
merged 24 commits into from
Jan 21, 2020

Conversation

benbrimeyer
Copy link
Collaborator

@benbrimeyer benbrimeyer commented Jan 15, 2020

The setup and teardown behavior of these hooks attempt to reach feature parity with jest.

Test nodes will be marked as failures if the beforeEach, beforeAll, or afterEach hook throws. Currently there is a limitation in this changelist that does not mark the nodes as failures if afterAll hook throws.

This pull request replaces the need for #39

Copy link
Contributor

@AmaranthineCodices AmaranthineCodices left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple minor things I noticed

tests/lifecycleHooks.lua Show resolved Hide resolved
errorMessage = nodeResult
for _, hook in pairs(lifecycleHooks:getAfterEachHooks()) do
if success then
success, errorMessage = runCallback(hook, true, "after each hook: ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message prefix here is inconsistent with the prefix for the other lifecycle hooks:

Suggested change
success, errorMessage = runCallback(hook, true, "after each hook: ")
success, errorMessage = runCallback(hook, true, "afterEach hook: ")

Copy link
Contributor

@LPGhatguy LPGhatguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just took a cursory glance for my evening reading, I'll dive in more at work tomorrow. 😄

Can you revert the changes to files that are just trailing line endings at the end of files? We can go back through the codebase at a later time to catch those and do some other reformatting work.

.gitignore Outdated
/site
/lua_install
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you running hererocks locally? This is an acceptable change, it's just interesting that this is how you got your Lua install to work 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was carry over from the previous pull request. I can remove it if needed.

.luacheckrc Outdated
},
}

std = "lua51+roblox+testez"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we're testing TestEZ itself, we don't generally have access to TestEZ, so I'm not sure that adding it to our Luacheck config is correct here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I dont add it here, Tests/lifecycleHooks.lua wont pass luacheck since we are testing these globals

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should narrow down the scope of what files we inject these globals into, then. I don't want to make Luacheck think that the entire TestEZ codebase has access to these values, it mostly doesn't.

We've got two options I think:

  1. Use Luacheck assertions on the functions that use these methods, if there's a small number of them.
  2. Only apply the testez std for the tests folder or even the specific test that depends on these.

Copy link
Collaborator Author

@benbrimeyer benbrimeyer Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. seems to be the most applicable, since this sounds like a problem of scoping.
    We can create a new luacheckrc file for the tests directory that has the testez std. Would that work?

Edit: We could do 1. as well, there's about 4 tests in lifecycleHooks that use these functions at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to make a new luacheckrc file, just scope the rules using globs to only affect the files in the tests folder like we do in Roact:

https://github.com/Roblox/roact/blob/master/.luacheckrc

@benbrimeyer
Copy link
Collaborator Author

Thanks for the first round of feedback. All of it should be addressed now.

lib/TestRunner.lua Show resolved Hide resolved
Copy link
Contributor

@LPGhatguy LPGhatguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a more detailed look through this.

lib/LifecycleHooks.lua Show resolved Hide resolved
lib/LifecycleHooks.lua Show resolved Hide resolved
[TestEnum.NodeType.AfterAll] = self:_getAfterAllHooksUncalledAtCurrentLevel(planNode.children),
[TestEnum.NodeType.BeforeEach] = self:_getHooksOfType(planNode.children, TestEnum.NodeType.BeforeEach),
[TestEnum.NodeType.AfterEach] = self:_getHooksOfType(planNode.children, TestEnum.NodeType.AfterEach),
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mouthful!

lib/LifecycleHooks.lua Show resolved Hide resolved
lib/TestRunner.lua Outdated Show resolved Hide resolved
for _, hook in pairs(lifecycleHooks:getPendingBeforeHooks()) do
if success then
success, errorMessage = runCallback(hook, false, "beforeAll hook: ")
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't run afterAll hooks in this block, but we run beforeAll here.

Why don't we process beforeAll above, before we enter this child loop?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do that, but if the beforeAll node fails I'm afraid I wont have access to the child nodes it was wrapping as easily

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if we move beforeAll out, we wont be able to filter out describe blocks with beforeAlls with no It blocks very easily.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have the same problem with afterAll not being filtered? I wonder if we should try to patch this up a little in TestEZ and then start fresh in the new test framework with lifecycle events in mind from the start.

@@ -110,7 +144,7 @@ function TestRunner.runPlanNode(session, planNode, tryStack, noXpcall)
end
elseif childPlanNode.type == TestEnum.NodeType.Describe or childPlanNode.type == TestEnum.NodeType.Try then
if childPlanNode.type == TestEnum.NodeType.Try then tryStack:push({isOk = true, failedNode = nil}) end
TestRunner.runPlanNode(session, childPlanNode, tryStack, childPlanNode.HACK_NO_XPCALL)
TestRunner.runPlanNode(session, childPlanNode, tryStack, lifecycleHooks, childPlanNode.HACK_NO_XPCALL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function's starting to get a lot of arguments. We're right at the threshold where refactoring it to take a table of arguments might be a good idea, but we can leave that out of this PR.

tests/lifecycleHooks.lua Outdated Show resolved Hide resolved
tests/lifecycleHooks.lua Outdated Show resolved Hide resolved

local results = TestEZ.TestRunner.runPlan(plan)
return results, lifecycleOrder
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a neat way to test this feature.

Copy link
Contributor

@LPGhatguy LPGhatguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LEt's merge to master!

@LPGhatguy LPGhatguy merged commit 5fa229b into Roblox:master Jan 21, 2020
@benbrimeyer benbrimeyer deleted the revive-lifecycle branch January 21, 2020 22:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants