gaelcolas / Sampler

Module template with build pipeline and examples, including DSC elements.
MIT License
171 stars 42 forks source link

Pester tests are failing in standard 'CompleteSample' #417

Open raandree opened 1 year ago

raandree commented 1 year ago

Problem description

When creating a 'CompleteSample' with the command

New-SampleModule -DestinationPath d:\ -ModuleType CompleteSample -ModuleAuthor Me -ModuleName Test1 -ModuleDescription None -ModuleVersion 0.1.0 -LicenseType MIT

Pester tests are failing with this error message

[-] Returns a string from Get-PrivateFunction 42ms (39ms|2ms)
    RuntimeException: Should -Invoke: Could not find Mock for command Get-PrivateFunction in module Test1. Was the mock defined? Did you use the same -ModuleName as on the Mock? When using InModuleScope are InModuleScope, Mock and Should -Invoke using the same -ModuleName?

The issue is with the Assert-MockCalled calls. However, the allegedly missing mock is defined:

Mock Get-PrivateFunction { $PrivateData }

Verbose logs

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\randr> cd D:\Test1\
PS D:\Test1> .\build.ps1
[pre-build] Starting Build Init
[pre-build] Pre-pending 'D:\Test1\output\RequiredModules' folder to PSModulePath
[build] Starting build with InvokeBuild.
[build] Parsing defined tasks
[build] Loading Configuration from D:\Test1\build.yaml
[build] Pre-pending 'D:\Test1\output\module' folder to PSModulePath
Importing tasks from module Sampler.GitHubTasks
         Loading New-Release.GitHub.build.Sampler.GitHubTasks.ib.tasks...
Importing tasks from module Sampler
         Loading Build-Module.ModuleBuilder.build.Sampler.ib.tasks...
         Loading ChocolateyPackage.build.Sampler.ib.tasks...
         Loading Clean.ModuleBuilder.build.Sampler.ib.tasks...
         Loading Create_Changelog_Branch.build.Sampler.ib.tasks...
         Loading Create_Release_Git_Tag.build.Sampler.ib.tasks...
         Loading DeployAll.PSDeploy.build.Sampler.ib.tasks...
         Loading DscResource.Test.build.Sampler.ib.tasks...
         Loading generateHelp.PlatyPS.build.Sampler.ib.tasks...
         Loading GuestConfig.build.Sampler.ib.tasks...
         Loading Invoke-Pester.pester.build.Sampler.ib.tasks...
         Loading JaCoCo.coverage.build.Sampler.ib.tasks...
         Loading release.module.build.Sampler.ib.tasks...
         Loading Set-SamplerTaskVariable...
Adding Workflow from configuration:
  +-> test
  +-> pack
  +-> build
  +-> publish
  +-> .
[build] Executing requested workflow: .
Build . D:\Test1\build.ps1
Redefined task '.'.

===============================================================================
                        CLEAN
Deleting the content of the Build Output folder, except ./modules
-------------------------------------------------------------------------------
  /./build/Clean
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Clean.ModuleBuilder.build.ps1:16

Removing D:\Test1\output\* excluding RequiredModulesDone /./build/Clean 00:00:00.0486100===============================================================================                        BUILD MODULEOUTPUT MODULEBUILDER
Build the Module based on its Build.psd1 definition
-------------------------------------------------------------------------------
  /./build/Build_Module_ModuleBuilder/Build_ModuleOutput_ModuleBuilder
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Build-Module.ModuleBuilder.build.ps1:41

        Project Name               = 'Test1'
        Source Path                = 'D:\Test1\source'
        Output Directory           = 'D:\Test1\output'
        Release Notes path         = 'D:\Test1\output\ReleaseNotes.md'
        Built Module Subdirectory  = 'D:\Test1\output\module'
        Module Manifest Path (src) = 'D:\Test1\source\Test1.psd1'
Cannot find the .git directory
        Module Version             = ''

WARNING: The names of some imported commands from the module 'ModuleBuilder' include unapproved verbs that might make
them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.
Adding OutputDirectory with value D:\Test1\output from current Variables
Adding VersionedOutputDirectory with value True from current Variables
Adding CopyPaths with value en-US from Build Info
Adding Encoding with value UTF8 from Build Info
Building Module to D:\Test1\output\module\Test1...
Done /./build/Build_Module_ModuleBuilder/Build_ModuleOutput_ModuleBuilder 00:00:01.0155653

===============================================================================
                        BUILD DSCRESOURCESTOEXPORT MODULEBUILDER

-------------------------------------------------------------------------------
  /./build/Build_Module_ModuleBuilder/Build_DscResourcesToExport_ModuleBuilder
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Build-Module.ModuleBuilder.build.ps1:327

        Project Name               = 'Test1'
        Source Path                = 'D:\Test1\source'
        Output Directory           = 'D:\Test1\output'
        Release Notes path         = 'D:\Test1\output\ReleaseNotes.md'
        Built Module Subdirectory  = 'D:\Test1\output\module'
        Module Manifest Path (src) = 'D:\Test1\source\Test1.psd1'
        Versioned Output Directory = 'True'
        Built Module Manifest      = 'D:\Test1\output\module\Test1\0.1.0\Test1.psd1'
        Built Module Base          = 'D:\Test1\output\module\Test1\0.1.0'
        Module Version             = '0.1.0'
        Module Version Folder      = '0.1.0'
        Pre-release Tag            = ''
        Built Module Root Script   = 'D:\Test1\output\module\Test1\0.1.0\Test1.psm1'

WARNING: The names of some imported commands from the module 'ModuleBuilder' include unapproved verbs that might make
them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.
        Built DSC Resource Path  = 'D:\Test1\output\module\Test1\0.1.0\DSCResources'
Looking for class-based DSC Resources in in 'D:\Test1\output\module\Test1\0.1.0\Test1.psm1'
Looking for MOF-based DSC Resources in 'D:\Test1\output\module\Test1\0.1.0\DSCResources'
Looking for DSC Composite Resources in 'D:\Test1\output\module\Test1\0.1.0\DSCResources'
Done /./build/Build_Module_ModuleBuilder/Build_DscResourcesToExport_ModuleBuilder 00:00:00.1352907
Done /./build/Build_Module_ModuleBuilder 00:00:01.1533572

===============================================================================
                        BUILD NESTEDMODULES MODULEBUILDER

-------------------------------------------------------------------------------
  /./build/Build_NestedModules_ModuleBuilder
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Build-Module.ModuleBuilder.build.ps1:129

        Project Name               = 'Test1'
        Source Path                = 'D:\Test1\source'
        Output Directory           = 'D:\Test1\output'
        Release Notes path         = 'D:\Test1\output\ReleaseNotes.md'
        Built Module Subdirectory  = 'D:\Test1\output\module'
        Module Manifest Path (src) = 'D:\Test1\source\Test1.psd1'
        Versioned Output Directory = 'True'
        Built Module Manifest      = 'D:\Test1\output\module\Test1\0.1.0\Test1.psd1'
        Built Module Base          = 'D:\Test1\output\module\Test1\0.1.0'
        Module Version             = '0.1.0'
        Module Version Folder      = '0.1.0'
        Pre-release Tag            = ''
        Built Module Root Script   = 'D:\Test1\output\module\Test1\0.1.0\Test1.psm1'

WARNING: The names of some imported commands from the module 'ModuleBuilder' include unapproved verbs that might make
them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.
Done /./build/Build_NestedModules_ModuleBuilder 00:00:00.0605633

===============================================================================
                        CREATE CHANGELOG RELEASE OUTPUT
Create ReleaseNotes from changelog and update the Changelog for release
-------------------------------------------------------------------------------
  /./build/Create_changelog_release_output
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\release.module.build.ps1:103

        Project Name               = 'Test1'
        Source Path                = 'D:\Test1\source'
        Output Directory           = 'D:\Test1\output'
        Release Notes path         = 'D:\Test1\output\ReleaseNotes.md'
        Built Module Subdirectory  = 'D:\Test1\output\module'
        Module Manifest Path (src) = 'D:\Test1\source\Test1.psd1'
        Versioned Output Directory = 'True'
        Built Module Manifest      = 'D:\Test1\output\module\Test1\0.1.0\Test1.psd1'
        Built Module Base          = 'D:\Test1\output\module\Test1\0.1.0'
        Module Version             = '0.1.0'
        Module Version Folder      = '0.1.0'
        Pre-release Tag            = ''
        Built Module Root Script   = 'D:\Test1\output\module\Test1\0.1.0\Test1.psm1'

        ChangeLogOutputPath   = 'D:\Test1\output\CHANGELOG.md'
        Creating 'D:\Test1\output\CHANGELOG.md'...
Built Manifest D:\Test1\output\module\Test1\0.1.0\Test1.psd1
Done /./build/Create_changelog_release_output 00:00:00.1949059
Done /./build 00:00:01.4744441

===============================================================================
                        IMPORT PESTER

-------------------------------------------------------------------------------
  /./test/Pester_Tests_Stop_On_Fail/Import_Pester
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:55

Done /./test/Pester_Tests_Stop_On_Fail/Import_Pester 00:00:02.7543040

===============================================================================
                        INVOKE PESTER TESTS V4
Making sure the Module meets some quality standard (help, tests) using Pester 4.
-------------------------------------------------------------------------------
  /./test/Pester_Tests_Stop_On_Fail/Invoke_Pester_Tests_v4
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:63

Pester 4 is not used in the pipeline, skipping task.

Done /./test/Pester_Tests_Stop_On_Fail/Invoke_Pester_Tests_v4 00:00:00.0185158

===============================================================================
                        INVOKE PESTER TESTS V5
Making sure the Module meets some quality standard (help, tests) using Pester 5.
-------------------------------------------------------------------------------
  /./test/Pester_Tests_Stop_On_Fail/Invoke_Pester_Tests_v5
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:422

        Project Name               = 'Test1'
        Source Path                = 'D:\Test1\source'
        Output Directory           = 'D:\Test1\output'
        Release Notes path         = 'D:\Test1\output\ReleaseNotes.md'
        Built Module Subdirectory  = 'D:\Test1\output\module'
        Module Manifest Path (src) = 'D:\Test1\source\Test1.psd1'
        Versioned Output Directory = 'True'
        Built Module Manifest      = 'D:\Test1\output\module\Test1\0.1.0\Test1.psd1'
        Built Module Base          = 'D:\Test1\output\module\Test1\0.1.0'
        Module Version             = '0.1.0'
        Module Version Folder      = '0.1.0'
        Pre-release Tag            = ''
        Built Module Root Script   = 'D:\Test1\output\module\Test1\0.1.0\Test1.psm1'

        Pester Output Folder    = 'D:\Test1\output\testResults
Creating folder D:\Test1\output\testResults

-------------------------------------------------------------------------------------
Consider updating the build configuration to the new advanced configuration options:
-------------------------------------------------------------------------------------
# PESTER CONFIG START
Pester:
  # Pester Advanced configuration.
  # If a key is not set it will be using Sampler pipeline default value.
  Configuration:
    Run:
      Path:

      ExcludePath:
    Filter:
      Tag:

      ExcludeTag:

    Output:
      Verbosity:
    CodeCoverage:
      Path:
      OutputFormat:
      CoveragePercentTarget: 85
      OutputPath:
      OutputEncoding:
      ExcludeTests:
    TestResult:
      OutputFormat: NUnitXML
      OutputPath:
      OutputEncoding:
      TestSuiteName:
  # Sampler pipeline configuration
  ExcludeFromCodeCoverage:

# PESTER CONFIG END
-------------------------------------------------------------------------------------

Using deprecated build configuration for OutputFormat as a invocation task parameter.
Using deprecated build configuration for CodeCoverageThreshold as a invocation task parameter.

Using PesterConfigurationCodeCoverageCoveragePercentTarget from build invocation task parameter.
Using PesterConfigurationTestResultOutputFormat from build invocation task parameter.

        Pester Exclude Path =
        Pester Exclude Tags =
        Pester Tags         =
        Pester Verbosity    = Detailed

        Pester Code Coverage Source Path       = D:\Test1\output\module\Test1\0.1.0\Test1.psm1
        Pester Exclude Code Coverage Path      =
        Pester Exclude Tests Source Path       = True
        Pester Code Coverage Output Path       = D:\Test1\output\testResults\CodeCov_Test1_v0.1.0.Windows.PSv.5.1.22621.963.xml
        Pester Code Coverage Output Format     = JaCoCo
        Pester Code Coverage Output Encoding   = UTF8
        Pester Code Coverage Percent Threshold = 85

        Pester Test Result Test Suite Name     = Test1
        Pester Test Result Output Path         = D:\Test1\output\testResults\NUnitXml_Test1_v0.1.0.Windows.PSv.5.1.22621.963.xml
        Pester Test Result Output Format       = NUnitXML
        Pester Test Result Output Encoding     = UTF8
        Pester Test Result Percent Threshold   = 85

        Pester Test Scripts = D:\Test1\tests
Pester v5.4.0

Starting discovery in 7 files.
Discovery found 41 tests in 1.22s.
Starting code coverage.
Code Coverage preparation finished after 240 ms.
Running tests.

Running tests from 'module.tests.ps1'
Describing Changelog Management
  [!] Changelog has been updated 29ms (0ms|29ms)
  [+] Changelog format compliant with keepachangelog format 45ms (35ms|10ms)
  [+] Changelog should have an Unreleased header 14ms (12ms|2ms)

Describing General module control
  [+] Should import without errors 28ms (22ms|5ms)
  [+] Should remove without error 7ms (5ms|1ms)

Describing Quality for module
  [+] Should have a unit test for Get-PrivateFunction 13ms (8ms|5ms)
  [+] Should have a unit test for Get-Something 5ms (3ms|2ms)
  [+] Should pass Script Analyzer for Get-PrivateFunction 273ms (271ms|2ms)
  [+] Should pass Script Analyzer for Get-Something 123ms (118ms|5ms)

Describing Help for module
  [+] Should have .SYNOPSIS for Get-PrivateFunction 26ms (20ms|6ms)
  [+] Should have .SYNOPSIS for Get-Something 7ms (5ms|1ms)
  [+] Should have a .DESCRIPTION with length greater than 40 characters for Get-PrivateFunction 20ms (18ms|1ms)
  [+] Should have a .DESCRIPTION with length greater than 40 characters for Get-Something 9ms (7ms|2ms)
  [+] Should have at least one (1) example for Get-PrivateFunction 38ms (19ms|20ms)
  [+] Should have at least one (1) example for Get-Something 12ms (10ms|2ms)
  [+] Should have described all parameters for Get-PrivateFunction 28ms (27ms|1ms)
  [+] Should have described all parameters for Get-Something 10ms (9ms|1ms)

Running tests from 'class1.tests.ps1'
Describing class1
 Context Type creation
   [+] Has created a type named class1 14ms (7ms|7ms)
 Context Constructors
   [+] Has a default constructor 31ms (28ms|3ms)
 Context Methods
   [+] Overrides the ToString method 8ms (5ms|3ms)
 Context Properties
   [+] Has a Name property 9ms (6ms|3ms)

Running tests from 'class11.tests.ps1'
Describing class11
 Context Type creation
   [+] Has created a type named class11 12ms (3ms|8ms)
 Context Constructors
   [+] Has a default constructor 16ms (13ms|3ms)
 Context Methods
   [+] Overrides the ToString method 13ms (11ms|3ms)
 Context Properties
   [+] Has a Name property 10ms (7ms|3ms)

Running tests from 'class12.tests.ps1'
Describing class12
 Context Type creation
   [+] Has created a type named class12 15ms (3ms|12ms)
 Context Constructors
   [+] Has a default constructor 17ms (13ms|4ms)
 Context Methods
   [+] Overrides the ToString method 9ms (6ms|3ms)
 Context Properties
   [+] Has a Name property 9ms (5ms|3ms)

Running tests from 'class2.tests.ps1'
Describing class2
 Context Type creation
   [+] Has created a type named class2 11ms (6ms|4ms)
 Context Constructors
   [+] Has a default constructor 15ms (12ms|3ms)
 Context Methods
   [+] Overrides the ToString method 12ms (8ms|3ms)
 Context Properties
   [+] Has a Name property 14ms (8ms|6ms)

Running tests from 'Get-PrivateFunction.tests.ps1'
Describing Get-PrivateFunction
 Context Default
   [+] Returns a single object 19ms (15ms|4ms)
   [+] Returns a string based on the parameter PrivateData 7ms (5ms|2ms)

Running tests from 'Get-Something.tests.ps1'
Describing Get-Something
 Context Return values
   [+] Returns a single object 16ms (12ms|4ms)
   [-] Returns a string from Get-PrivateFunction 42ms (40ms|2ms)
    RuntimeException: Should -Invoke: Could not find Mock for command Get-PrivateFunction in module Test1. Was the mockdefined? Did you use the same -ModuleName as on the Mock? When using InModuleScope are InModuleScope, Mock and Should -Invoke using the same -ModuleName?
 Context Pipeline
   [-] Accepts values from the pipeline by value 11ms (7ms|4ms)
    RuntimeException: Should -Invoke: Could not find Mock for command Get-PrivateFunction in module Test1. Was the mockdefined? Did you use the same -ModuleName as on the Mock? When using InModuleScope are InModuleScope, Mock and Should -Invoke using the same -ModuleName?
   [-] Accepts value from the pipeline by property name 10ms (8ms|1ms)
    RuntimeException: Should -Invoke: Could not find Mock for command Get-PrivateFunction in module Test1. Was the mockdefined? Did you use the same -ModuleName as on the Mock? When using InModuleScope are InModuleScope, Mock and Should -Invoke using the same -ModuleName?
 Context ShouldProcess
What if: Performing the operation "Get-Something" on target "value".
   [+] Supports WhatIf 13ms (11ms|2ms)
What if: Performing the operation "Get-Something" on target "value".
   [-] Does not call Get-PrivateFunction if WhatIf is set 9ms (7ms|1ms)
    RuntimeException: Should -Invoke: Could not find Mock for command Get-PrivateFunction in module Test1. Was the mockdefined? Did you use the same -ModuleName as on the Mock? When using InModuleScope are InModuleScope, Mock and Should -Invoke using the same -ModuleName?
Tests completed in 3.4s
Tests Passed: 36, Failed: 4, Skipped: 1 NotRun: 0
Processing code coverage result.
Code Coverage result processed in 194 ms.
Covered 100% / 85%. 14 analyzed Commands in 1 File.
Done /./test/Pester_Tests_Stop_On_Fail/Invoke_Pester_Tests_v5 00:00:05.5963005
Task /./test/Pester_Tests_Stop_On_Fail/Upload_Test_Results_To_AppVeyor skipped.

===============================================================================
                        PESTER RUN TIMES
List the run time for each Pester test.
-------------------------------------------------------------------------------
  /./test/Pester_Tests_Stop_On_Fail/Pester_Run_Times
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:1068

        Project Name               = 'Test1'
        Source Path                = 'D:\Test1\source'
        Output Directory           = 'D:\Test1\output'
        Release Notes path         = 'D:\Test1\output\ReleaseNotes.md'
        Built Module Subdirectory  = 'D:\Test1\output\module'
        Module Manifest Path (src) = 'D:\Test1\source\Test1.psd1'
        Versioned Output Directory = 'True'
        Built Module Manifest      = 'D:\Test1\output\module\Test1\0.1.0\Test1.psd1'
        Built Module Base          = 'D:\Test1\output\module\Test1\0.1.0'
        Module Version             = '0.1.0'
        Module Version Folder      = '0.1.0'
        Pre-release Tag            = ''
        Built Module Root Script   = 'D:\Test1\output\module\Test1\0.1.0\Test1.psm1'

        Pester Output Folder     = D:\Test1\output\testResults
        Pester Output Object     = D:\Test1\output\testResults\PesterObject_Test1_v0.1.0.Windows.PSv.5.1.22621.963.xml

Test Script File               Duration Result Passed Failed Skipped Total
----------------               -------- ------ ------ ------ ------- -----
module.tests.ps1              2 seconds Passed     16      0       1    17
class1.tests.ps1              0 seconds Passed      4      0       0     4
class11.tests.ps1             0 seconds Passed      4      0       0     4
class12.tests.ps1             0 seconds Passed      4      0       0     4
class2.tests.ps1              0 seconds Passed      4      0       0     4
Get-PrivateFunction.tests.ps1 0 seconds Passed      2      0       0     2
Get-Something.tests.ps1       0 seconds Failed      2      4       0     6

Total run time: 0 minutes 3 seconds (41 tests was run)

Done /./test/Pester_Tests_Stop_On_Fail/Pester_Run_Times 00:00:00.1081768

===============================================================================
                        FAIL BUILD IF PESTER TESTS FAILED
This task ensures the build job fails if the test aren't successful.
-------------------------------------------------------------------------------
  /./test/Pester_Tests_Stop_On_Fail/Fail_Build_If_Pester_Tests_Failed
  D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:361

Asserting that no test failed

        Project Name               = 'Test1'
        Source Path                = 'D:\Test1\source'
        Output Directory           = 'D:\Test1\output'
        Release Notes path         = 'D:\Test1\output\ReleaseNotes.md'
        Built Module Subdirectory  = 'D:\Test1\output\module'
        Module Manifest Path (src) = 'D:\Test1\source\Test1.psd1'
        Versioned Output Directory = 'True'
        Built Module Manifest      = 'D:\Test1\output\module\Test1\0.1.0\Test1.psd1'
        Built Module Base          = 'D:\Test1\output\module\Test1\0.1.0'
        Module Version             = '0.1.0'
        Module Version Folder      = '0.1.0'
        Pre-release Tag            = ''
        Built Module Root Script   = 'D:\Test1\output\module\Test1\0.1.0\Test1.psm1'

        Pester Output Folder     = 'D:\Test1\output\testResults'
        Code Coverage Threshold  = '85'
        Pester Output Object = D:\Test1\output\testResults\PesterObject_Test1_v0.1.0.Windows.PSv.5.1.22621.963.xml
ERROR: Assertion failed. Failed 4 tests. Aborting Build
At D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:415 char:9
+         Assert-Build -Condition ($pesterObject.FailedCount -eq 0) -Me ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:361 char:1
+ task Fail_Build_If_Pester_Tests_Failed {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Build FAILED. 15 tasks, 1 errors, 0 warnings 00:00:12.8248949
Assert-Build : Assertion failed. Failed 4 tests. Aborting Build
At D:\Test1\output\RequiredModules\Sampler\0.116.1\tasks\Invoke-Pester.pester.build.ps1:415 char:9
+         Assert-Build -Condition ($pesterObject.FailedCount -eq 0) -Me ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Assert-Build], Exception
    + FullyQualifiedErrorId : Assert-Build

How to reproduce

New-SampleModule -DestinationPath d:\ -ModuleType CompleteSample -ModuleAuthor Me -ModuleName Test1 -ModuleDescription None -ModuleVersion 0.1.0 -LicenseType MIT

cd D:\Test1

.\build.ps1

Expected behavior

No failure when running the Pester tests.

Current behavior

see logs.

Suggested solution

NA

Operating system the target node is running

OsName               : Microsoft Windows 11 Pro for Workstations
OsOperatingSystemSKU : 161
OsArchitecture       : 64-bit
WindowsVersion       : 2009
WindowsBuildLabEx    : 22621.1.amd64fre.ni_release.220506-1250
OsLanguage           : en-US
OsMuiLanguages       : {en-US, de-DE, en-GB}

PowerShell version and build the target node is running

Name                           Value
----                           -----
PSVersion                      5.1.22621.963
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.963
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module version used

Name    Version Path
----    ------- ----
Sampler 0.116.1 D:\Test1\output\RequiredModules\Sampler\0.116.1\Sampler.psd1
Sampler 0.116.2 C:\Program Files\WindowsPowerShell\Modules\Sampler\0.116.2\Sampler.psd1
fslef commented 1 year ago

I'm not confortable with pester, but my assumption is the code provided in the Get-PrivateFunction.tests.ps1 could not work for private functions.

The first lines are for loading the powershell module we are working on, but as it is a private functions, we can't call it.

$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
        ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
        $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } )
    }).BaseName

Import-Module $ProjectName

I think the solution would be to dot source the private function. This is where I am stuck now.

johlju commented 1 year ago

The test shpuld use InModuleScope to access the private function.

It 'Should test something` {
    InModuleScope -ModuleName 'MyModuleName' -ScriptBlock {
        $result = Get-MyPrivateFunction
        result | Should -Be 'MyResult'
    }
}
dh4711 commented 1 year ago

As i understand Pester 5 syntax the Mock needs to go in BeforeEach, at least that is how i got it working with the module sample, so in Get-Something.tests.ps1 i added Context 'Return values' { BeforeEach { Mock Get-PrivateFunction { $PrivateData } $return = Get-Something -Data 'value' }

johlju commented 1 year ago

The public and private functions tests are fixed in PR #439 and changed to support Pester 5. But there are more tests that has not been fixed yet. Must be fixed in other PRs.