Pester Migration

Pester Migration

Upgrades PowerShell Pester test suites one major version at a time (v3 to v6), explaining the Discovery/Run model, mock changes and breaking changes.

Skill files4 files

references/

SKILL.md150 lines
1
---
2
name: pester-migration
3
description: 'Pester migration skill for upgrading PowerShell Pester test suites across major versions — v3→v4, v4→v5, and v5→v6. Covers the Discovery/Run two-phase model, moving setup into BeforeAll, $PSScriptRoot vs $MyInvocation, mock changes (Assert-MockCalled → Should -Invoke, removed fall-through), Invoke-Pester parameters → PesterConfiguration, data-driven -ForEach/-TestCases, and the v6 breaking changes. Use when the user asks to upgrade, migrate, or modernize Pester tests, fix *.Tests.ps1 files that broke after bumping the Pester version, or convert legacy Should / Invoke-Pester syntax.'
4
---
5
6
# Pester Migration
7
8
Pester is the test framework for PowerShell. Test files end in `*.Tests.ps1` and use
9
`Describe` / `Context` / `It` blocks with `Should` assertions. This skill upgrades an existing
10
suite from one major Pester version to the next and gets it green again.
11
12
> **Mental model:** each major jump has a different character. **v3→v4** is mostly a syntax
13
> rename. **v4→v5** is a *fundamental runtime change* (the Discovery/Run split) and is the hard
14
> one. **v5→v6** is largely backwards-compatible — a handful of previously-deprecated things now
15
> throw. Migrate **one major at a time**; never skip a version.
16
17
Detailed, symptom-driven guides live in `references/` — load the one(s) for the jump you are doing.
18
19
## References
20
21
| Reference | When to load |
22
|---|---|
23
| [v3-to-v4.md](references/v3-to-v4.md) | `Should Be` → `Should -Be`, `Contain` → `FileContentMatch`, `Assert-VerifiableMocks` → `Assert-VerifiableMock`, array-assertion edge cases. |
24
| [v4-to-v5.md](references/v4-to-v5.md) | The big one. Discovery/Run phases, `BeforeAll` setup, `$PSScriptRoot`, `BeforeDiscovery`, `-ForEach`, mock scoping, `Should -Throw` wildcards, `Invoke-Pester` → `New-PesterConfiguration`. |
25
| [v5-to-v6.md](references/v5-to-v6.md) | PowerShell 5.1/7.4+ only, per-file discovery+run, empty `-ForEach` throws, duplicate setup blocks throw, name `<...>` templates evaluate, `Assert-MockCalled` removed, mocks no longer fall through, code-coverage tracer, legacy `Invoke-Pester` params removed. |
26
27
Canonical source: the official migration guides at https://pester.dev/docs/migrations/ — this skill
28
mirrors them. When in doubt, prefer the website.
29
30
## Step 0 — Detect where you are and where you're going
31
32
Find the installed version(s) and the version the **tests** were written for. These can differ.
33
34
```powershell
35
# Installed Pester version(s) on this machine
36
Get-Module Pester -ListAvailable | Select-Object Name, Version, Path
37
38
# Version currently imported in the session
39
(Get-Module Pester).Version
40
```
41
42
Tell the source version from the **test code** with these heuristics:
43
44
| You see in `*.Tests.ps1` / build scripts | Suite was written for |
45
|---|---|
46
| `Should Be` / `Should Contain` (no dash) | v3 or earlier → start at [v3-to-v4](references/v3-to-v4.md) |
47
| `$MyInvocation.MyCommand.Path` + dot-source at the **top** of the file; arbitrary code directly under `Describe` | v4 → [v4-to-v5](references/v4-to-v5.md) |
48
| `Assert-MockCalled`, `Assert-VerifiableMock`, `Set-ItResult -Pending` | v4 / early-v5 (these are **removed in v6**) |
49
| `Invoke-Pester -Script … -OutputFile … -CodeCoverage …` (legacy params) | v4 invocation → map to config |
50
| `BeforeAll { . $PSScriptRoot/… }`, `New-PesterConfiguration`, `Should -Invoke` | already v5-style → [v5-to-v6](references/v5-to-v6.md) |
51
52
Install the target version when ready:
53
54
```powershell
55
# Latest stable v5 — pin the major to avoid installing Pester 6
56
Install-Module Pester -MaximumVersion 5.99.99 -Force
57
58
# Pester 6
59
Install-Module Pester -Force
60
```
61
62
> On **Windows PowerShell 5.1** the OS ships a Microsoft-signed built-in Pester 3 that PowerShellGet
63
> won't overwrite with the differently-signed newer Pester — add `-SkipPublisherCheck` there to
64
> install side-by-side. Not needed on PowerShell 7+. See
65
> https://pester.dev/docs/introduction/installation.
66
67
## Migration workflow
68
69
Run this loop for each major jump. **Do not jump two majors at once** — go v4→v5, then v5→v6.
70
71
1. **Baseline.** Run the suite on the **current** version first and record pass/fail. You need a
72
known-good (or known) starting point so you can tell migration regressions apart from
73
pre-existing failures.
74
```powershell
75
# Bare Invoke-Pester works on every major; exact parameters differ
76
# (v3/v4: -Script/-OutputFile; v5+/v6: -Path/-Output).
77
Invoke-Pester
78
```
79
2. **Read the reference** for this jump (table above) so you know the full scope before editing.
80
3. **Edit file by file.** Apply the mechanical changes (see per-jump cheat sheets below and in the
81
reference). Keep changes small and reviewable — one file or one concern at a time.
82
4. **Switch versions** with `Install-Module` (Step 0), then re-import: `Remove-Module Pester;
83
Import-Module Pester` (or start a fresh session).
84
5. **Run and fix.** Re-run with `-Output Detailed`; use `-Output Diagnostic` (v4→v5) or read the
85
explicit v6 error messages to locate problems. Match each failure to the **symptom → fix**
86
tables in the reference.
87
6. **Green, diff, commit.** Re-run until the result matches the baseline (or better). Review the
88
diff, then commit. Migrating in small commits makes regressions trivial to bisect.
89
90
## What actually changes (scope per jump)
91
92
| Jump | Difficulty | Nature |
93
|---|---|---|
94
| v3 → v4 | Low | Assertion-syntax rename (`Should -Be`). Largely script-automatable. |
95
| v4 → v5 | **High** | New two-phase runtime. Test **structure** changes: setup must move into `BeforeAll`, discovery-time code into `BeforeDiscovery`, file location via `$PSScriptRoot`. Not a pure find-replace. |
96
| v5 → v6 | Low–Medium | Backwards-compatible runtime; deprecated features now throw. Mostly small, targeted fixes. Your `Should -Be` assertions keep working unchanged. |
97
98
## Quick cheat sheets
99
100
### v4 → v5 (most common fixes)
101
```powershell
102
# 1. Move file import into BeforeAll, use $PSScriptRoot (NOT $MyInvocation.MyCommand.Path)
103
# BEFORE
104
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
105
. "$here\Get-Thing.ps1"
106
# AFTER
107
BeforeAll { . $PSScriptRoot/Get-Thing.ps1 }
108
109
# 2. Any code that DISCOVERS/generates tests must be in BeforeDiscovery
110
BeforeDiscovery { $cases = Get-Content $PSScriptRoot/cases.json | ConvertFrom-Json }
111
112
# 3. Should -Throw matches with -like wildcards, not .Contains
113
{ throw 'a long message' } | Should -Throw '*long*'
114
115
# 4. Invoke-Pester legacy params → New-PesterConfiguration (see reference for full map)
116
```
117
Full details, scoping rules, and the parameter→config table: [references/v4-to-v5.md](references/v4-to-v5.md).
118
119
### v5 → v6 (most common fixes)
120
```powershell
121
# 1. Mock assertions: removed verbs — rename (old -> new):
122
# Assert-MockCalled -> Should -Invoke
123
# Assert-VerifiableMock -> Should -InvokeVerifiable
124
Should -Invoke Get-Thing -Times 1 -Exactly
125
Should -InvokeVerifiable
126
127
# 2. Add a default mock — unmatched calls no longer run the real command
128
Mock Get-Thing { 'default' }
129
Mock Get-Thing -ParameterFilter { $Name -eq 'a' } -MockWith { 'a' }
130
131
# 3. Empty/$null -ForEach now throws; allow it only where empty is expected
132
Describe 'Optional' -ForEach $cases -AllowNullOrEmptyForEach { }
133
134
# 4. Combine duplicate BeforeAll/BeforeEach/AfterAll/AfterEach in the same block into one
135
```
136
Full breaking-change list with symptoms and fixes: [references/v5-to-v6.md](references/v5-to-v6.md).
137
138
## Safety rules
139
140
- **Tests are the spec.** Migration must not change what a test asserts — only how the suite is
141
structured and invoked. If a test starts passing/failing differently for any reason other than a
142
documented breaking change, investigate before accepting it.
143
- **Automated migration scripts produce false positives.** The community scripts (linked in the
144
references) help with `Should` syntax and dot-sourcing, but always review the diff and re-run the
145
suite afterward. Never bulk-edit and commit unchecked.
146
- **Mind file encoding** when scripting replacements over `*.Tests.ps1` — preserve the original
147
encoding (UTF-8 vs ASCII) so you don't mangle non-ASCII test names.
148
- **Work on a branch, commit per file/concern.** Small commits keep `git bisect` useful if a
149
migrated test goes red later.
150

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…