/ginkgo:writing-specsfrom ginkgo
Guides writing Ginkgo specs with correct container/subject/setup node patterns, the declare-in-container-initialize-in-BeforeEach rule, and common pitfalls like tree-construction-time assertions.
Ships in ginkgo. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
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.
var _ = Describe("Books", func() {
var book *books.Book // declare here...
BeforeEach(func() {
book = &books.Book{ // ...initialize here, fresh per spec
Title: "Les Miserables",
Author: "Victor Hugo",
Pages: 2783,
}
Expect(book.IsValid()).To(BeTrue()) // assertions in setup are fine
})
Describe("categorizing", func() {
Context("with more than 300 pages", func() {
It("is a novel", func() {
Expect(book.Category()).To(Equal(books.CategoryNovel))
})
})
})
})
Describe/Context/When) organize; they're identical — pick the one that reads as a sentence. Subjects (It/Specify) hold the assertions; one spec runs per It.It text — write them to read as a phrase.Container bodies run once, at tree-construction time (ginkgo:overview). So:
var book *books.Book).BeforeEach so each spec gets a clean copy.// WRONG — runs once at construction; every spec shares & pollutes one book
var _ = Describe("Books", func() {
book := &books.Book{Pages: 2783} // initialized at construction time
It("mutates", func() { book.Pages = 0 })
It("expects 2783", func() { Expect(book.Pages).To(Equal(2783)) }) // flaky under randomization
})
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.
| 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:
var jsonString string
BeforeEach(func() { jsonString = `{"id":1,"name":"Sally"}` }) // base config
JustBeforeEach(func() { user, err = NewUser(jsonString) }) // creation, runs last
Context("with malformed JSON", func() {
BeforeEach(func() { jsonString = `{"oops"` }) // override one input
It("errors", func() { Expect(err).To(HaveOccurred()) })
})
Use it deliberately — deeply nested JustBeforeEaches get hard to follow.
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):
BeforeEach(func() {
original := os.Getenv("MODE")
os.Setenv("MODE", "test")
DeferCleanup(os.Setenv, "MODE", original) // captured args passed at cleanup time
})
DeferCleanup accepts func(), func() error (a non-nil error fails the spec), captured arguments, and a func(ctx SpecContext) form (→ ginkgo:timeouts-and-async).os.Unsetenv after the test wrongly assumes the var started unset — capture and restore instead (as above).DeferCleanup is 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.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):
BeforeEach(func(ctx SpecContext) {
dbClient = db.NewClient(ctx)
DeferOnFailure(func(ctx SpecContext) {
AddReportEntry("db-dump", dbClient.Dump(ctx), ReportEntryVisibilityFailureOrVerbose)
}, NodeTimeout(10*time.Second))
DeferCleanup(dbClient.Clear) // runs after the dump
})
DeferCleanup (spec, Ordered container from BeforeAll, or whole suite from BeforeSuite). Also on GinkgoT(), so helpers and libraries can register it.AbortSuite, first ^C) — including a failure in teardown, before the rest of teardown runs. Not triggered by ReportAfterEach failures. Callbacks run LIFO, default to NodeTimeout(--grace-period), and ignore the spec's expired SpecTimeout.JustAfterEach + if CurrentSpecReport().Failed().GinkgoWriter buffers logs and only prints them when a spec fails (or always under -v) — so passing specs stay quiet. Use GinkgoWriter.Printf(...), or GinkgoWriter.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.It("processes an order", func() {
By("submitting the cart")
// ...
By("charging the card")
// ...
})
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.
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:
func expectValidBook(b *books.Book) {
GinkgoHelper() // this frame is ignored in failure locations
Expect(b).NotTo(BeNil())
Expect(b.IsValid()).To(BeTrue()) // a failure here is reported at the CALLER
}
It("accepts a good book", func() {
expectValidBook(book) // ← failures point here, not inside the helper
})
GinkgoHelper() composes. Mark every helper in a chain (expectValidBook → expectStorable → …) and the reported location is always the spec that kicked it off.Fail(msg, offset) or the Offset(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:
func EnsureSprockets(n int, fn func(int)) {
GinkgoHelper()
GinkgoHelperGo(func(helperFail func(string, ...int)) { // implies defer GinkgoRecover()
if n == 0 {
helperFail("sprockets must not be zero") // reported at the EnsureSprockets call site
}
fn(n) // caller's assertions report inline
})
}
(With Gomega, a helper's own assertions can run through g := gomega.NewGomega(helperFail); g.Expect(...).)
ginkgo:tables-and-dynamic-specsSerial, Label, timeouts, …) → ginkgo:decoratorsginkgo:running, ginkgo:filteringWhat SKILL.md sets; muted values are Claude Code's defaults
13 skills in this plugin
/ciConfigure Ginkgo for continuous integration — the recommended CLI flag set and the rationale for each flag (-r -p --randomize-all --randomize-suites --fail-on-pending --fail-on-empty --keep-going --cover --race --trace --json-report --timeout --poll-progress-after/-interval), invoking via go run to pin the CLI to go.mod, the exit-code safeguards that catch committed Focus/Pending and empty filters, collecting report and coverage artifacts with --output-dir, CI-friendly output (--github-output/--force-newlines/--no-color), and the fixed per-suite cost --race adds (GORACE=atexit_sleep_ms=0). Use when setting up or hardening a CI pipeline for a Ginkgo suite./debugging-failuresDiagnose a failing Ginkgo suite as an agent — always run with --json-report into a predictable temp/gitignored location, read the terminal verdict line, then use jq to extract structured failure details (name, message, file:line, panic value, captured logs). Covers the panicked-vs-failed trap, panic locations pointing into the Go runtime, parallel output interleaving, reproducing with --seed, and progress reports for hangs. Use when a suite failed and you need to know why. Also invokable as /ginkgo:debugging-failures./decoratorsOne-line reference for every Ginkgo decorator, grouped by what it does, with the node types each can decorate — Serial, Ordered, ContinueOnFailure, OncePerOrdered, Label, Focus, Pending, FlakeAttempts, MustPassRepeatedly, NodeTimeout, SpecTimeout, GracePeriod, PollProgressAfter/Interval, SuppressProgressReporting, SpecPriority, SemVerConstraint, AroundNode, Offset/CodeLocation, EntryDescription. Use to look up a decorator's exact name, semantics, and where it's legal.npx claudepluginhub onsi/ginkgo --plugin ginkgoOther plugins' skills for the same job