Skip to content

Commit f8dbaea

Browse files
authored
Refactor test scripts to use consistent module name variable (#2385)
1 parent 352435f commit f8dbaea

File tree

317 files changed

+1500
-1378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

317 files changed

+1500
-1378
lines changed

.github/instructions/dsc-community-style-guidelines-integration-tests.instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ BeforeDiscovery {
4747
BeforeAll {
4848
$script:moduleName = '{MyModuleName}'
4949
50-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
50+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
5151
}
5252
```

.github/instructions/dsc-community-style-guidelines-unit-tests.instructions.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ BeforeDiscovery {
4343
BeforeAll {
4444
$script:moduleName = '{MyModuleName}'
4545
46-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
46+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
4747
4848
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName
4949
$PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName
@@ -54,9 +54,6 @@ AfterAll {
5454
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
5555
$PSDefaultParameterValues.Remove('Mock:ModuleName')
5656
$PSDefaultParameterValues.Remove('Should:ModuleName')
57-
58-
# Unload the module being tested so that it doesn't impact any other tests.
59-
Get-Module -Name $script:moduleName -All | Remove-Module -Force
6057
}
6158
```
6259

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
104104

105105
### Fixed
106106

107+
- Unit Tests
108+
- Fixed PowerShell class type identity issues that caused "Cannot convert
109+
'Type' to 'Type'" errors when running multiple test files in the same
110+
Pester job. The issue occurred because using `Import-Module -Force` and
111+
`Remove-Module` between test files caused PowerShell to create new type
112+
identities for module-defined classes. Changed all unit test files to not
113+
use `-Force` when importing the module being tested and removed the module
114+
unload from `AfterAll` blocks. This prevents class type identity mismatches
115+
while maintaining test isolation through mock cleanup.
107116
- Fixed all `Invoke-WebRequest` calls throughout the codebase to include the
108117
`-UseBasicParsing` parameter. This addresses a Windows PowerShell 5.1 security
109118
update (CVE-2025-54100) released December 9, 2025, which changed the default

tests/Integration/Commands/Add-SqlDscFileGroup.Integration.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ BeforeDiscovery {
2626
BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

29-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
29+
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
30+
# PowerShell class types to get new identities, breaking type comparisons.
31+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
3032
}
3133

3234
Describe 'Add-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {

tests/Integration/Commands/Add-SqlDscTraceFlag.Integration.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ BeforeDiscovery {
2626
BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

29-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
29+
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
30+
# PowerShell class types to get new identities, breaking type comparisons.
31+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
3032
}
3133

3234
Describe 'Add-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {

tests/Integration/Commands/Assert-SqlDscAgentOperator.Integration.Tests.ps1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ BeforeDiscovery {
2626
BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

29-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
29+
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
30+
# PowerShell class types to get new identities, breaking type comparisons.
31+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
3032

3133
$env:SqlServerDscCI = $true
3234

@@ -36,9 +38,6 @@ BeforeAll {
3638

3739
AfterAll {
3840
$null = Remove-Item -Path 'Env:\SqlServerDscCI' -ErrorAction 'Stop'
39-
40-
# Unload the module being tested so that it doesn't impact any other tests.
41-
Get-Module -Name $script:moduleName -All | Remove-Module -Force
4241
}
4342

4443
Describe 'Assert-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {

tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ BeforeDiscovery {
2626
BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

29-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
29+
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
30+
# PowerShell class types to get new identities, breaking type comparisons.
31+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
3032
}
3133

3234
Describe 'Assert-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {

tests/Integration/Commands/Backup-SqlDscDatabase.Integration.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ BeforeDiscovery {
2626
BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

29-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
29+
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
30+
# PowerShell class types to get new identities, breaking type comparisons.
31+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
3032
}
3133

3234
Describe 'Backup-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {

tests/Integration/Commands/Complete-SqlDscImage.Integration.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ BeforeDiscovery {
2727
BeforeAll {
2828
$script:moduleName = 'SqlServerDsc'
2929

30-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
30+
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
31+
# PowerShell class types to get new identities, breaking type comparisons.
32+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
3133
}
3234

3335
# cSpell: ignore DSCSQLTEST, PrepareImage, CompleteImage

tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ BeforeDiscovery {
2626
BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

29-
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
29+
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
30+
# PowerShell class types to get new identities, breaking type comparisons.
31+
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
3032
}
3133

3234
# cSpell: ignore DSCSQLTEST

0 commit comments

Comments
 (0)