sentrytest

package
v1.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sentrytest provides test fixtures for the Sentry Go SDK.

Fixture bundles a context, scope, client, and MockTransport with assertion helpers, eliminating boilerplate across integration and unit tests.

Isolated mode (default) creates a context and scope safe for parallel tests. Global mode (WithGlobal) calls sentry.Init for tests that use global state.

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 CheckRequestIsolation

func CheckRequestIsolation(t *testing.T, request func() (context.Context, context.Context, error))

CheckRequestIsolation exercises sequential reuse and concurrent requests. request returns the completed request context and, optionally, its outer middleware context. Both contexts must be canceled when the request ends.

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

	// Context carries the fixture's isolated scope and client.
	Context context.Context

	// Scope is the fixture's isolated scope.
	Scope *sentry.Scope

	// 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 context, scope, 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) 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).

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 a fresh fixture scope and client. 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 client instead of creating an isolated client context.

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