sentrytest

package
v0.46.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package sentrytest provides test fixtures for the Sentry Go SDK.

Fixture bundles a Hub, Client, and MockTransport with assertion helpers, eliminating boilerplate across integration and unit tests.

Isolated mode (default) creates a cloned hub safe for parallel tests. Global mode (WithGlobal) calls sentry.Init for middleware tests that read from sentry.CurrentHub.

Index

Constants

This section is empty.

Variables

View Source
var DefaultEventCmpOpts = cmp.Options{
	cmpopts.IgnoreFields(sentry.Event{},
		"Contexts",
		"EventID",
		"Modules",
		"Platform",
		"Release",
		"Sdk",
		"ServerName",
		"Timestamp",
	),
	cmpopts.IgnoreFields(sentry.Request{}, "Env"),
	cmpopts.IgnoreUnexported(sentry.Event{}),
	cmpopts.EquateEmpty(),
}

DefaultEventCmpOpts are cmp.Options for comparing sentry.Event values. They ignore fields that vary between runs (IDs, timestamps, server metadata) and all unexported fields.

Functions

func NewContext

func NewContext(parent context.Context, t testing.TB, opts ...Option) context.Context

NewContext creates a context backed by a new Fixture without a synctest bubble. If parent is nil, context.Background is used.

func Run

func Run(t *testing.T, fn func(t *testing.T, f *Fixture), opts ...Option)

Run creates a Fixture inside a synctest.Test bubble and calls fn with it. All background goroutines (batch processors) use fake time, so Fixture.Flush completes instantly. This is the preferred way to create fixtures.

For the rare case where synctest is incompatible (e.g. third-party code that leaks goroutines), use NewFixture directly.

Types

type Fixture

type Fixture struct {
	// T is the test context.
	T testing.TB

	// Hub is the fixture's hub. In global mode this is [sentry.CurrentHub].
	// In isolated mode this is a clone with its own client.
	Hub *sentry.Hub

	// Client is the fixture's client, configured with the MockTransport.
	Client *sentry.Client

	// Transport captures all events sent through the client.
	Transport *sentry.MockTransport
	// contains filtered or unexported fields
}

Fixture provides an isolated Sentry environment for testing. It bundles a Hub, Client, and MockTransport with assertion helpers.

func NewFixture

func NewFixture(t testing.TB, opts ...Option) *Fixture

NewFixture creates a new test fixture without a synctest bubble. Prefer Run for most tests; use this only when synctest is incompatible.

func (*Fixture) AssertEventCount

func (f *Fixture) AssertEventCount(want int)

AssertEventCount flushes and asserts the number of captured events.

func (*Fixture) AssertHubIsolation

func (f *Fixture) AssertHubIsolation(requestHub *sentry.Hub)

AssertHubIsolation verifies that requestHub is a distinct clone from the fixture's hub, confirming the middleware properly cloned the hub per request.

func (*Fixture) DiffEvents

func (f *Fixture) DiffEvents(want []*sentry.Event, opts ...cmp.Option) string

DiffEvents flushes and returns a cmp.Diff of captured events against want. Uses DefaultEventCmpOpts merged with any additional opts. Returns "" when events match.

func (*Fixture) Events

func (f *Fixture) Events() []*sentry.Event

Events returns all captured events, including transactions.

TODO: Add typed helper views (errors, transactions, logs, metrics, check-ins) when the telemetry processor path is enabled in tests.

func (*Fixture) Flush

func (f *Fixture) Flush()

Flush flushes the fixture's client.

Inside a Run bubble it calls synctest.Wait first to let all background goroutines settle, then flushes under fake time (completing instantly). Outside a bubble it awaits for a real Flush.

func (*Fixture) NewContext

func (f *Fixture) NewContext(parent context.Context) context.Context

NewContext returns parent with the fixture's hub attached. If parent is nil, context.Background is used.

type Option

type Option func(*config)

Option configures a Fixture.

func WithClientOptions

func WithClientOptions(opts sentry.ClientOptions) Option

WithClientOptions sets the sentry.ClientOptions for the fixture. Transport is always overridden to use the fixture's mock transport. If Dsn is empty, the fixture sets a placeholder test DSN.

func WithGlobal

func WithGlobal() Option

WithGlobal makes the fixture call sentry.Init to set the global hub instead of creating an isolated hub. Use this for middleware tests where the middleware reads from sentry.CurrentHub.

Tests using global mode must not run in parallel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL