Files
awesome-copilot/skills/pester-migration/references/v3-to-v4.md
T
Jakub Jareš fb0fae173f Add pester-migration skill (experimental) (#2163)
* Add pester-migration skill

A self-contained, experimental skill that helps upgrade PowerShell Pester
test suites across major versions (v3->v4, v4->v5, v5->v6). One router
SKILL.md plus per-jump references. The v5->v6 guidance tracks Pester 6,
which is still a release candidate.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Regenerate skills index for pester-migration

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address review feedback on pester-migration skill

- Pin the stable-v5 install to -MaximumVersion 5.99.99 so it keeps
  installing v5 after Pester 6 goes GA (SKILL.md).
- Make the baseline run command version-agnostic (bare Invoke-Pester)
  and note that parameters differ across majors (SKILL.md).
- Replace the '->' mapping arrows inside powershell fences with
  comment + valid replacement lines so snippets are copy/paste-safe
  (SKILL.md v5->v6 cheat sheet, v5-to-v6.md, and v3-to-v4.md).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address review: scope -SkipPublisherCheck, trim status callout, order references

- Drop -SkipPublisherCheck from the default install commands; scope it to
  Windows PowerShell 5.1 (installing over the OS's Microsoft-signed built-in
  Pester 3) per pester.dev install docs, instead of an unconditional default.
- Trim the experimental status callout: keep the preview marker, drop the
  date-based, human-oriented wording.
- Reorder the References table into version progression order (v3->v4, v4->v5,
  v5->v6).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: nohwnd <jakub@jares.cz>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Aaron Powell <me@aaron-powell.com>
2026-07-02 10:29:37 +10:00

3.7 KiB

Migrating Pester v3 → v4

This is the smallest jump — mostly an assertion-syntax rename. Many suites need only minor changes, some need none. It is largely script-automatable, but always review the diff and re-run the suite.

Official guide: https://pester.dev/docs/migrations/v3-to-v4

Heads-up: if your goal is a modern Pester (5 or 6), v3→v4 is only the first step. Do it, get green, then continue with v4-to-v5.md and v5-to-v6.md.


Change 1 — Dashed Should assertion syntax

v4 introduced the parameter-style Should syntax. The bareword form still ran in v4 but is removed in v5, so converting now saves a later migration.

# v3 (bareword)
It 'checks something' { 10 | Should Be 10 }

# v4+ (dashed)
It 'checks something' { 10 | Should -Be 10 }

The rename applies to every operator: Be, BeExactly, Match, Throw, BeNullOrEmpty, Contain, etc. → -Be, -BeExactly, -Match, -Throw, -BeNullOrEmpty, …

There is a well-known AST-based converter, Update-PesterTest (Chris Dent / Wojciech Sciesinski), that inserts the dashes safely by parsing the file rather than regexing it: https://gist.github.com/indented-automation/aeb14825e39dd8849beee44f681fbab3 — it's also reproduced in the official v3→v4 guide. Review its output, especially for non-UTF-8/ASCII files, where it can change encoding.


Change 2 — ContainFileContentMatch

The Contain assertion was renamed to FileContentMatch (it tests file contents, which the old name made ambiguous against collection containment).

# Should Contain      -> Should -FileContentMatch
# Should Not Contain  -> Should -Not -FileContentMatch
'app.config' | Should -FileContentMatch 'setting'
'app.config' | Should -Not -FileContentMatch 'secret'

A simple regex-based migration script from the official guide (verify results — it can produce false positives):

$content = Get-Content -Path $file -Encoding $encoding
$content = $content -replace 'Should\s+\-?Contain',        'Should -FileContentMatch'
$content = $content -replace 'Should\s+\-?Not\s*-?Contain', 'Should -Not -FileContentMatch'
$content = $content -replace 'Assert-VerifiableMocks',      'Assert-VerifiableMock'
$content | Set-Content -Path $file -Encoding $encoding

Change 3 — Assert-VerifiableMocksAssert-VerifiableMock

The cmdlet was renamed (dropped the trailing s). Rename all occurrences.

In Pester 5 this is deprecated and in Pester 6 it is removed — when you continue past v4, switch to Should -InvokeVerifiable. See v5-to-v6.md.


Change 4 — Array assertions (watch for edge cases)

Should gained array assertions in v4. This is transparent for most tests, but there are edge cases where an array test that passed under v3 fails under v4. If an array-related test changes result after the rename, inspect it manually rather than forcing it to pass. Background: https://github.com/pester/Pester/issues/873.

Mocking also shifted subtly when Pester moved from functions to aliases; there are no required changes, but if mocked-command behavior looks off, see https://github.com/pester/Pester/issues/810 and https://github.com/pester/Pester/issues/812.


v3 → v4 checklist

  • Suite runs on v3 first (baseline).
  • All Should <Operator> converted to Should -<Operator> (prefer the AST converter).
  • Should ContainShould -FileContentMatch (and the -Not form).
  • Assert-VerifiableMocksAssert-VerifiableMock.
  • Array-assertion behavior changes reviewed manually.
  • File encoding preserved by any scripted replacement.
  • Suite green on v4; diff reviewed; committed.