-
-
Couldn't load subscription status.
- Fork 476
Description
Checklist
- Issue has a meaningful title
- I have searched the existing issues. See all issues
- I have tested using the latest version of Pester. See Installation and update guide.
What is the issue?
I am having unexpected behavior where an alias is not being cleared out between testfiles.
In a Before-All block in TestFile1.Tests.ps1, I dot-source a file, X.ps1, which declares an alias Test-AliasX. Then in the same Before-All block I call Import-Module to import C.psm1, which itself also dot-sources X.ps1. At the end of TestFile1.Tests.ps1 in an After-All block, I Remove-Module C. Then in the next test file, TestFile2.Tests.ps1, I test for the existence of Test-AliasX, and it still exists. It does not get cleaned up between test files.
In short:
TestFile1
BeforeAll
-> dot-source X.ps1
Create alias X
-> Import-Module C.psm1
-> dot-source X.ps1 (again)
Create alias X (again)
Create alias C
# Some tests here
AfterAll
Remove-Module C
TestFile2
Test asserting module C is gone (Success)
Test asserting alias C is gone (Success)
Test asserting alias X is gone <------ Failing here.
I've had similar results with Functions.
In contrast, I have done some related tests where the alias does get cleaned up as expected:
- dot-source just X.ps1. The alias seems to get cleaned up automatically.
- Import-Module just C.psm1 by itself. In the After-All block I Remove-Module C. Even though C.psm1 dot-sources X.ps1, the alias is cleaned up in time for the next test file.
Expected Behavior
The following test from TestFile2.Tests.ps1 should pass since the alias got cleaned up, just as the alias does in other cases mentioned.
It "X Alias Does Not Exist" {
# TODO - THIS IS WHERE THE FAILURE IS
Get-Alias Test-AliasX -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
Steps To Reproduce
#----------------------------------------------------------
# X.ps1
Function Test-FunctionX() {
echo "Running Test-X"
}
New-Alias -Name Test-AliasX -Value Test-FunctionX
#-----------------------------------------------------------
# C.psm1
. $PSScriptRoot\X.ps1
Function Test-FunctionC() {
echo "Running Test-C"
}
New-Alias -Name Test-AliasC -Value Test-FunctionC
#----------------------------------------------------------
# TestFile1.Tests.ps1
Describe "Test ps1 include and psm1 with ps1 include" {
BeforeAll {
. $PSScriptRoot\X.ps1
Import-Module $PSScriptRoot\C.psm1
}
It "Alias X Exists" {
Get-Alias Test-AliasX | Should -Not -BeNullOrEmpty
}
It "Function X Exists" {
Get-Item "Function:Test-FunctionX" | Should -Not -BeNullOrEmpty
}
It "Alias C Exists" {
Get-Alias Test-AliasC | Should -Not -BeNullOrEmpty
}
It "Function C Exists" {
Get-Item "Function:Test-FunctionC" | Should -Not -BeNullOrEmpty
}
AfterAll {
Remove-Module C
}
}
#-----------------------------------------------------------------
# TestFile2.Tests.ps1
Describe "Cleanup psm1 including ps1" {
It "Module C Does Not Exist" {
Get-Module C -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
It "Alias C Does Not Exist" {
Get-Alias Test-AliasC -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
It "Function C Does Not Exist" {
Get-Item "Function:Test-FunctionC" -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
It "Module X Does Not Exist" {
Get-Module X -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
It "X Alias Does Not Exist" {
# TODO - THIS IS WHERE THE FAILURE IS
Get-Alias Test-AliasX -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
It "X Function Does Not Exist" {
Get-Item "Function:Test-FunctionX" -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
}
Describe your environment
Pester version : 5.7.1 C:\Users\jfinny\Documents\PowerShell\Modules\Pester\5.7.1\Pester.psm1
PowerShell version : 7.5.3
OS version : Microsoft Windows NT 10.0.26100.0
Possible Solution?
The only thing I can come up with is that the directly-included X.ps1 and the transitively included X.ps1 thru C.psm1 are interacting in such a way as to leave the alias in the session.