writing-specs
What it does
This skill shows how to write well-organized Ginkgo tests in Go. It is for developers who write or review Go tests with Ginkgo.
When to use it
- ✓When you write new Ginkgo tests for a Go package.
- ✓When your Ginkgo tests share data and start to interfere with each other.
- ✓When you want to review or tidy existing Ginkgo test code.
Skill link
https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/writing-specs/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Write Ginkgo tests for my Go bookshelf code, with clean setup for each test.”
5 new skills a week. 1 minute to read.
How it works
It explains the parts of a test: groups that organize tests, the tests themselves, and setup code that runs fresh before each one.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
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
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))
})
})
})
})
- 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.
// 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.
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:
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.
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):
BeforeEach(func() {
original := os.Getenv("MODE")
os.Setenv("MODE", "test")
DeferCleanup(os.Setenv, "MODE", original) // captured args passed at cleanup time
})
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 t
FAQ
Where should I put variables for my tests?
Declare them in the group and set their values in a setup step so each test starts clean.
What is DeferCleanup?
It schedules a cleanup step that runs after a test, even if the test fails.
Can I make helper functions for tests?
Yes, the skill covers reusable helper functions that report failures at the right line.
Details
Original description by the author
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 a test helper. Covers the tree-construction-time pitfalls (no assertions/init/loop-capture in contain
repo: onsi/ginkgo
Popularity
- GitHub stars
- 9.1k
- Ranking position
- #12310
- License
- MIT
- Updated
- 10/9/2026
Comments
- …
More skills from this repository
ci
9.1kHelps your agent set up Ginkgo tests for continuous integration, with recommended flags that catch leftover focus or pending markers and flaky tests.
debugging-failures
9.1kHelps your agent find out why Ginkgo tests failed by reading the summary line and pulling the exact failing tests and messages from a JSON report.
decorators
9.1kHelps your agent use Ginkgo test decorators correctly, such as Serial, Ordered, Label, Focus and timeouts, by explaining what each one does.
filtering
9.1kHelps your agent run only some Ginkgo tests, using pending, skip, focus, labels, version constraints and file filters in the right order of precedence.
Similar skills
test-driven-development
311kTeaches your agent to write a failing test first, watch it fail, then write the smallest code that passes, before building any feature or fix.
verification-before-completion
311kTeaches your agent to run real checks and read their output before claiming that a task, fix, or build is complete or passing.
migrate-to-shoehorn
310kReplaces risky type casts in your test files with the shoehorn library, so tests can use partial data and still pass type checks.