From gomega
Guides writing custom Gomega matchers in Go tests using the GomegaMatcher interface or gcustom.MakeMatcher with templates, format helpers, and StopTrying for Eventually/Consistently. Use when built-in or composed matchers can't express a domain assertion.
How this skill is triggered — by the user, by Claude, or both
Slash command
/gomega:custom-matchersThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
When no built-in matcher fits and composition (`And`/`Or`/`WithTransform`/`SatisfyAll` — see `gomega:composing-matchers`) can't express it cleanly, write a custom matcher. **Reach for `gcustom.MakeMatcher` first** — it's the modern, low-boilerplate path. Drop to a hand-written type only when you need full control. Docs: <https://onsi.github.io/gomega/#adding-your-own-matchers>.
When no built-in matcher fits and composition (And/Or/WithTransform/SatisfyAll — see gomega:composing-matchers) can't express it cleanly, write a custom matcher. Reach for gcustom.MakeMatcher first — it's the modern, low-boilerplate path. Drop to a hand-written type only when you need full control. Docs: https://onsi.github.io/gomega/#adding-your-own-matchers.
gcustom.MakeMatcher(matchFunc, ...) builds a full types.GomegaMatcher from one function. The match function must be func(actual T) (bool, error):
import (
"github.com/onsi/gomega/gcustom"
"github.com/onsi/gomega/types"
"encoding/json"
"fmt"
"net/http"
"reflect"
)
func RepresentJSONifiedObject(expected any) types.GomegaMatcher {
return gcustom.MakeMatcher(func(response *http.Response) (bool, error) {
ptr := reflect.New(reflect.TypeOf(expected)).Interface()
if err := json.NewDecoder(response.Body).Decode(ptr); err != nil {
return false, fmt.Errorf("failed to decode JSON: %w", err)
}
decoded := reflect.ValueOf(ptr).Elem().Interface()
return reflect.DeepEqual(decoded, expected), nil
}).WithTemplate("Expected:\n{{.FormattedActual}}\n{{.To}} contain the JSON representation of\n{{format .Data 1}}").WithTemplateData(expected)
}
Then Expect(resp).To(RepresentJSONifiedObject(book)) just works.
Typed match funcs get free type-checking. Because the func takes *http.Response, gcustom rejects any other actual with a clear error before calling your code. Use func(actual any) (bool, error) only if you want to handle multiple types or do your own type checks.
Return (false, err) for bad input, not a panic. A non-nil error fails the assertion in both the To and NotTo directions — you can't accidentally pass a negated assertion by feeding garbage.
.WithMessage("contain the JSON representation") — simplest. Renders Expected:\n<actual>\nto contain the JSON representation (and not to when negated)..WithTemplate(tmpl, optionalData) — full control. WithTemplate(tmpl, data) is shorthand for .WithTemplate(tmpl).WithTemplateData(data).Custom matcher failed for:\n<actual> message.Template variables and functions:
| token | meaning |
|---|---|
{{.Actual}} | the raw actual value |
{{.FormattedActual}} | actual pretty-printed (indent 1) |
{{.To}} | "to" on positive failure, "not to" on negated failure |
{{.Failure}} / {{.NegatedFailure}} | bools for the two directions |
{{.Data}} | whatever you passed to WithTemplateData |
{{format <obj> <indent>}} | render any object with Gomega's formatter |
Avoid recompiling templates in hot paths. WithTemplate parses on every constructor call. Precompile once with gcustom.ParseTemplate(str) (use that, not raw text/template, so the format func is registered) and pass it to MakeMatcher(matchFunc, tmpl) or .WithPrecompiledTemplate(tmpl).
MakeMatcher returns this; implement it directly only when you need behavior gcustom doesn't expose:
type GomegaMatcher interface {
Match(actual any) (success bool, err error)
FailureMessage(actual any) (message string)
NegatedFailureMessage(actual any) (message string)
}
A hand-rolled version of the matcher above:
func RepresentJSONifiedObject(expected any) types.GomegaMatcher {
return &representJSONMatcher{expected: expected}
}
type representJSONMatcher struct{ expected any }
func (m *representJSONMatcher) Match(actual any) (bool, error) {
response, ok := actual.(*http.Response)
if !ok {
return false, fmt.Errorf("RepresentJSONifiedObject matcher expects an *http.Response")
}
ptr := reflect.New(reflect.TypeOf(m.expected)).Interface()
if err := json.NewDecoder(response.Body).Decode(ptr); err != nil {
return false, fmt.Errorf("failed to decode JSON: %w", err)
}
decoded := reflect.ValueOf(ptr).Elem().Interface()
return reflect.DeepEqual(decoded, m.expected), nil
}
func (m *representJSONMatcher) FailureMessage(actual any) string {
return format.Message(actual, "to contain the JSON representation of", m.expected)
}
func (m *representJSONMatcher) NegatedFailureMessage(actual any) string {
return format.Message(actual, "not to contain the JSON representation of", m.expected)
}
Conventions:
RepresentJSONifiedObject) returns the matcher; the struct itself stays unexported. Take concrete types in the constructor where you can; use any only when you genuinely must.FailureMessage/NegatedFailureMessage are always called after Match — stash anything you computed in Match on the struct and reuse it in the messages.reflect to interpret generic actual/expected inputs.Use these for messages instead of hand-rolling fmt.Sprintf so output matches every other Gomega matcher:
format.Message(actual, "to <verb>", expected) → Expected\n<actual>\n<message>\n<expected>. Omit expected for a one-sided message.format.Object(obj, indent) → the pretty-printed block Gomega uses everywhere (indent is a uint).format.MessageWithDiff(actual, "to equal", expected) for string-diff highlighting.Eventually/Consistently (see gomega:async) re-invoke your matcher each poll. If a match result can become permanently fixed (e.g. a closed channel, an exited process), implement the optional OracleMatcher method so polling can short-circuit:
func (m *exitMatcher) MatchMayChangeInTheFuture(actual any) bool {
session := actual.(*gexec.Session)
return session.ExitCode() == -1 // true while still running; false once exited
}
This is not part of GomegaMatcher — most matchers don't need it. When present, Gomega calls it after each Match; returning false stops polling and fails/passes as appropriate (so Eventually(session).Should(Exit(0)) fails the instant a wrong exit code lands instead of waiting out the timeout). gcustom matchers don't expose this; hand-write the type if you need it.
Only consulted for bare values. If Eventually is polling a function, Gomega can't assume the result is stable, so MatchMayChangeInTheFuture is skipped.
A more direct, modern alternative: have Match return a StopTrying(...) error (or call StopTrying(...).Now()) to abort Eventually/Consistently immediately and fail with your message.
func (m *thing) Match(actual any) (bool, error) {
if irrecoverable {
return false, StopTrying("the resource was deleted; it will never appear")
}
return check(actual), nil
}
You can .Wrap(err) and .Attach("desc", obj) onto the signal. Full polling-signal semantics (including TryAgainAfter) live in gomega:async.
Drive the matcher under test with ordinary assertions, and for the error paths call Match directly (an erroring matcher would otherwise just fail your assertion):
var _ = Describe("RepresentJSONifiedObject", func() {
var book Book
var response *http.Response
BeforeEach(func() {
book = Book{Title: "Les Miserables", Author: "Victor Hugo"}
j, err := json.Marshal(book)
Expect(err).NotTo(HaveOccurred())
response = &http.Response{Body: io.NopCloser(bytes.NewBuffer(j))}
})
It("matches an http.Response carrying the object's JSON", func() {
Expect(response).To(RepresentJSONifiedObject(book))
})
It("does not match a different payload", func() {
response.Body = io.NopCloser(strings.NewReader(`{}`))
Expect(response).NotTo(RepresentJSONifiedObject(book))
})
It("errors on the wrong actual type", func() {
_, err := RepresentJSONifiedObject(book).Match("not a response")
Expect(err).To(HaveOccurred())
})
})
Assert on FailureMessage(actual) / NegatedFailureMessage(actual) when the wording matters.
format package, put the matcher and its tests in the matchers package, and add the constructor to matchers.go in the top-level package. Issues and PRs welcome at https://github.com/onsi/gomega.You can replace gomega.Default with a type implementing both gomega.Gomega and an Inner() Gomega method that returns the real default, delegating every call through. Useful for cross-suite logging, call counting, or injecting delays to surface timing dependencies — rarely needed, but the seam exists.
See also: gomega:matchers (catalog — check before building), gomega:composing-matchers (build matchers without code), gomega:async (polling signals).
npx claudepluginhub onsi/gomega --plugin gomegaCombines Gomega matchers via And/Or/Not, maps values with WithTransform, uses Satisfy for ad-hoc predicates, and nests matchers inside ContainElement/ConsistOf/HaveKeyWithValue.
Provides guidance on testify for Go testing: assert vs require, core assertions, mocks, and suites. Activates when code imports github.com/stretchr/testify.
Guides writing Ginkgo specs with container/subject/setup nodes, the 'declare in container, initialize in BeforeEach' rule, and reusable test helpers. Use when writing or reviewing Ginkgo tests.