The -ModuleName parameter tells Mock where to inject the mock, not where to find the function definition.
What you're currently doing is saying that any calls to testScript from inside your test.psm1 module should be intercepted and replaced with the mock, but any calls from outside the module (e.g. in your test-tests.ps1) still call the original testScript.
From https://pester-docs.netlify.app/docs/usage/modules:
Notice that in this example test script, all calls to Mock and Should -Invoke have had the -ModuleName MyModule parameter added. This tells Pester to inject the mock into the module's scope, which causes any calls to those commands from inside the module to execute the mock instead.
The simple fix is to remove the -ModuleName test parameter.
To make it a bit clearer, try adding a second function into your module:
test.psm1
function testScript
{
write-host 'hello'
}
function testAnother
{
testScript
}
and update your test suite to be this:
test-tests.ps1
Describe "test" {
Context "test call" {
Import-Module .\test.psm1
Mock -ModuleName 'test' testScript { write-host 'hello3' }
testScript
testAnother
}
}
If you try with and without -ModuleName 'test' you'll see the output is either
hello3
hello
or
hello
hello3
depending on where the mock is being injected (i.e. inside the module, or inside the test suite).