Add -Context and -Describe to BeforeEach and AfterEach (#1219)#2913
Draft
nohwnd wants to merge 1 commit into
Draft
Add -Context and -Describe to BeforeEach and AfterEach (#1219)#2913nohwnd wants to merge 1 commit into
nohwnd wants to merge 1 commit into
Conversation
BeforeEach and AfterEach so far only run around each test. This exposes the
block level variants that were scaffolded in the runtime but never had a public
setter, so you can run a setup or teardown around each Context or Describe.
Describe 'd' {
BeforeEach -Context { <# runs before every Context in here #> }
AfterEach -Context { <# runs after every Context in here #> }
}
No switch keeps the classic per-test behavior, and the switches combine, so
BeforeEach -Context -It runs before the Context and before each test in it.
-Describe and -Context inherit top down like the other setups (a setup on an
outer block runs before every matching nested block), teardowns run in the
opposite order.
The scriptblock is stored on the block as EachContextSetup / EachDescribeSetup,
and Invoke-Test walks up the ancestors and picks the one that matches how the
child block was defined (FrameworkData.CommandUsed). It runs on the block's
existing OuterSetup/OuterTeardown, so no extra scope is introduced.
Dropped the OneTimeBlock* leftovers while here, BeforeAll -Context makes no
sense since BeforeAll already runs once and its side effects persist for the
whole subtree.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Collaborator
|
I think we'll quickly need a A workaround would be to use nested |
Collaborator
|
Should also work for root block? Currently blocked in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft. Implements #1219.
BeforeEachandAfterEachonly run around each test today. This exposes the block level variants that were scaffolded in the runtime but never had a public setter, using the switches I sketched in the issue:-It).BeforeEach -Context -Itruns before the Context and before each test in it.-DescribetargetsDescribeblocks,-ContexttargetsContextblocks, using how the block was defined (FrameworkData.CommandUsed).No
BeforeAll -Context, that one is redundant,BeforeAllalready runs once and its side effects persist for the whole subtree. I dropped the now unusedOneTimeBlock*leftovers while here.Scoping: it runs on the block's existing
OuterSetup/OuterTeardown, the same pathBeforeEach/BeforeAllalready use, so no extra scope is introduced, the setup is just inherited down to the child.Verification
Full
test.ps1run passes (2617 passed, 0 failed). New tests inSetupTeardown.Tests.ps1cover:-Contextwrapping each Context and not the direct tests, inheritance into nested Contexts,-Describetargeting only Describe blocks, and the combined-Context -Itorder.Still todo before this leaves draft