Skip to content

Fix #2701: Should -BeOfType resolves PowerShell classes from actual value#2729

Merged
nohwnd merged 7 commits into
mainfrom
fix/2701-beoftype
Jun 18, 2026
Merged

Fix #2701: Should -BeOfType resolves PowerShell classes from actual value#2729
nohwnd merged 7 commits into
mainfrom
fix/2701-beoftype

Conversation

@nohwnd

@nohwnd nohwnd commented Jun 17, 2026

Copy link
Copy Markdown
Member

Fixes #2701

PowerShell classes loaded via dot-sourcing in BeforeAll are not visible to the Pester module's session state, so -as [Type] fails when the expected type is passed as a string (e.g. Should -BeOfType testInstance).

When the string-to-type resolution fails, we now walk the actual value's type hierarchy comparing Name and FullName. This lets the assertion pass when the object genuinely is of the expected type, even if the type isn't globally resolvable.

Still throws ArgumentException if neither -as [Type] nor the fallback finds a match (e.g. actual is \$null or genuinely wrong type).

nohwnd and others added 2 commits June 17, 2026 15:47
When the expected type is passed as a string and -as [Type] fails
(common with PowerShell classes loaded via dot-sourcing in BeforeAll),
fall back to walking the actual value's type hierarchy and comparing
type names. This lets 'Should -BeOfType testInstance' work even when
the class isn't visible to the module's session state.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tests the fallback path that resolves types from the actual value's
inheritance chain when -as [Type] fails. Includes an integration test
with a PowerShell class not visible to the module scope.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd enabled auto-merge (squash) June 17, 2026 13:59
nohwnd and others added 5 commits June 17, 2026 16:26
The nested Invoke-Pester corrupted module state causing the next
test file (BeTrueOrFalse.Tests.ps1) to fail in CI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd merged commit 76f6e99 into main Jun 18, 2026
13 checks passed
@nohwnd nohwnd deleted the fix/2701-beoftype branch June 18, 2026 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-beOfType doesn't use the instance inside the script

1 participant