onsi/ginkgo/writing-specs
Claude Code and agent skill by onsi/ginkgo
- community
Checking that the SKILL.md file is still in the repository.
Author good Ginkgo specs — container nodes (Describe/Context/When), subject nodes (It/Specify), setup/cleanup nodes (BeforeEach, JustBeforeEach, AfterEach, DeferCleanup, BeforeSuite/AfterSuite), capturing failure diagnostics before teardown with DeferOnFailure, the "declare in container, initialize in BeforeEach" rule, separating creation from configuration, reusable test helpers with GinkgoHelper()/GinkgoHelperGo(), and By/GinkgoWriter output. Use when writing or reviewing specs or extracting…
Install
npx vskill@latest install onsi/ginkgo/writing-specsSKILL.md
The same SKILL.md was published earlier as onsi/ginkgo/writing-specs.
Writing Ginkgo specs
Assumes Ginkgo is wired into the suite (ginkgo:setup) and you know the two-phase model (ginkgo:overview). Docs: <https://onsi.github.io/ginkgo/#writing-specs>.
The shape of a spec
- Containers (
Describe/Context/When) organize; they're identical — pick the one that reads as a sentence. Subjects (It/Specify) hold the assertions; one spec runs perIt. - The spec's full name is the concatenation of every container text plus the
Ittext — write them to read as a phrase.
The rule that prevents most bugs: declare in container, initialize in setup
Container bodies run once, at tree-construction time (ginkgo:overview). So:
- Declare shared variables in the container body (
var book *books.Book). - Initialize them in
BeforeEachso each spec gets a clean copy.
The same trap explains: no assertions in container bodies (they fire at construction, with no spec active) and no expensive/stateful work in container bodies. If you see logic directly inside a Describe/Context/When body, it almost always belongs in a BeforeEach.
Setup and cleanup nodes — and their ordering
| Node | Runs | |
|---|---|---|
BeforeEach | Before each spec, outer→inner. The workhorse. | |
JustBeforeEach | After all BeforeEaches, just before the It. | |
JustAfterEach | Just after the It, before any AfterEach. | |
AfterEach | After each spec, inner→outer (reverse). | |
BeforeSuite/AfterSuite | Once, around the whole suite (top-level only). | |
BeforeAll/AfterAll | Once per Ordered container (→ ginkgo:ordering-and-flakes). |
`JustBeforeEach` separates creation from configuration. Let nested BeforeEaches configure inputs into declared variables, and do the single creation step in JustBeforeEach — so each context overrides just the inputs it cares about:
Use it deliberately — deeply nested JustBeforeEaches get hard to follow.
Cleanup: prefer DeferCleanup, and restore rather than clear
DeferCleanup registers teardown next to the setup that needs it, and runs in LIFO order (like defer). It works in any setup/subject node and adapts to scope (called in BeforeSuite, it cleans up after the suite; in BeforeEach, after the spec):
DeferCleanupacceptsfunc(),func() error(a non-nil error fails the spec), captured arguments, and afunc(ctx SpecContext)form (→ginkgo:timeouts-and-async).- Restore original state; don't blindly clear it.
os.Unsetenvafter the test wrongly assumes the var started unset — capture and restore instead (as above). DeferCleanupis a function call, not a node — it's the one cleanup mechanism you may use inside setup/subject closures. You may not define nodes (It,BeforeEach, …) inside a running closure.
Capturing failure state: DeferOnFailure
DeferOnFailure registers diagnostics that run only if the spec fails, as soon as the failing node returns and before any teardown nodes — so they see the state that AfterEach/DeferCleanup are about to destroy (but not what a Go defer in the failing node already undid):
- Same signature and scope as `DeferCleanup` (spec,
Orderedcontainer fromBeforeAll, or whole suite fromBeforeSuite). Also onGinkgoT(), so helpers and libraries can register it. - Fires once per attempt on the first failure (assertion, panic, any timeout,
AbortSuite, first^C) — including a failure in teardown, before the rest of teardown runs. Not triggered byReportAfterEachfailures. Callbacks run LIFO, default toNodeTimeout(--grace-period), and ignore the spec's expiredSpecTimeout. - Prefer it to
JustAfterEach+if CurrentSpecReport().Failed().
Output: GinkgoWriter and By
- `GinkgoWriter` buffers logs and only prints them when a spec fails (or always under
-v) — so passing specs stay quiet. UseGinkgoWriter.Printf(...), orGinkgoWriter.TeeTo(w)to also stream live. →ginkgo:debugging-failures. - `By("...")` annotates steps in a long spec; the annotations surface on failure (and under
-v) to show how far the spec got. It records into the spec's timeline.
Failures, in brief
A failed Gomega assertion calls Fail, which panics; Ginkgo recovers it, marks the spec failed, and still runs cleanup. So code after a failed assertion in the same closure does not run. Use Skip("reason") to skip a spec at runtime (→ ginkgo:filtering). For failures inside goroutines and async polling, see ginkgo:timeouts-and-async.
Test helpers — keep failure locations honest with GinkgoHelper()
Extract repeated setup or assertions into plain Go functions. The catch: a `Fail` (or failed Gomega assertion) inside a helper reports the helper's own line — useless for knowing which call failed. Mark the helper with `GinkgoHelper()` and Ginkgo skips that frame when computing the failure location, pointing at the spec that called it instead:
- `GinkgoHelper()` composes. Mark every helper in a chain (
expectValidBook→expectStorable→ …) and the reported location is always the spec that kicked it off. - Prefer it over the older manual frame-counting:
Fail(msg, offset)or theOffset(n)decorator (→ginkgo:decorators). Those break the moment helpers call helpers (you'd have to bump every offset).
A helper that fails from a goroutine uses GinkgoHelperGo — it runs your code on a new goroutine, already implies `defer GinkgoRecover()` (→ ginkgo:timeouts-and-async), and gives you a helperFail to use for the helper's own failures (so they still report at the call site); caller-supplied assertions report inline:
(With Gomega, a helper's own assertions can run through g := gomega.NewGomega(helperFail); g.Expect(...).)
Where to go next
- Parameterize or generate specs →
ginkgo:tables-and-dynamic-specs - Decorate nodes (
Serial,Label, timeouts, …) →ginkgo:decorators - Run and filter what you wrote →
ginkgo:running,ginkgo:filtering
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingplugins/ginkgo/skills/writing-specs/SKILL.md
Popularity
- Repository stars
- 9,076
- Repository forks
- 710
- 7-day trend score
- 46
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/onsi/ginkgo/writing-specs)