NAME

Sentry::Test - Captures what an application sends to Sentry, for its tests

SYNOPSIS

use Test::More;
use Sentry::Test;

my $sentry = Sentry::Test->start;

My::App->new->run;    # however it initializes the SDK

is $sentry->last_event->{message}, 'Payment failed';
is_deeply $sentry->discarded_events, [];
$sentry->clear;

DESCRIPTION

Like the test helpers of sentry-python and sentry-ruby: from "start" on, every client sends to memory instead of Sentry, whenever and however the application initializes the SDK, before or after "start": with Sentry::SDK::Auto, its own "init" in Sentry::SDK, the options of Mojolicious::Plugin::SentrySDK, Plack::Middleware::SentrySDK or CGI::Application::Plugin::Sentry, or in a hook running only when a server starts. Even without DSN, as in most test environments.

Nothing is sent over the network, and nothing in the background: what the SDK sends is there as soon as the code reporting it returned. Events are kept as they would be sent: after sampling, "before_send" in Sentry::SDK, the event processors and scrubbing. What was dropped instead shows up in "discarded_events".

Only the process calling "start" captures: events of forked children, e.g. workers of a preforking server, are lost. Test the application in the test's process, as Test::Mojo and Plack::Test do by default.

Don't replace the client or mock Sentry::SDK methods instead: a mock returning nothing where the SDK expects an event id, or lacking methods the SDK calls, breaks in ways the real client never does.

EXAMPLES

Test::Mojo

use Test::More;
use Test::Mojo;
use Sentry::Test;

my $sentry = Sentry::Test->start({ traces_sample_rate => 1 });
my $t      = Test::Mojo->new('MyApp');

$t->get_ok('/fails')->status_is(500);

# die "Something failed\n" in the action
is_deeply [map { $_->{exception}{values}[-1]{value} } $sentry->events->@*],
  ['Something failed'];
is_deeply [map { $_->{transaction} } $sentry->transactions->@*],
  ['GET /fails'];

Plack::Test

use Test::More;
use Plack::Test;
use Plack::Util;
use HTTP::Request::Common qw(GET);
use Sentry::Test;

my $sentry = Sentry::Test->start;
my $test   = Plack::Test->create(Plack::Util::load_psgi('app.psgi'));

is $test->request(GET '/fails')->code, 500;
is scalar $sentry->events->@*, 1;

Test::More with several cases

my $sentry = Sentry::Test->start;

subtest 'reports a failed payment' => sub {
  $sentry->clear;
  My::Shop->new->pay(-1);
  is $sentry->last_event->{level}, 'error';
};

subtest 'reports nothing for a valid payment' => sub {
  $sentry->clear;
  My::Shop->new->pay(1);
  is_deeply $sentry->envelopes, [];
};

The scopes are not reset between cases: tags, the user or breadcrumbs the application set outside of a request scope stay, as they would in the application.

ATTRIBUTES

transport

The Sentry::Transport::Memory the clients send to.

METHODS

start

my $sentry = Sentry::Test->start;
my $sentry = Sentry::Test->start({ traces_sample_rate => 1 });

Starts capturing: initializes the SDK again with the options of the last "init" in Sentry::SDK (none if it was not initialized yet), and makes this and every later init send to "transport". The options given win over those of every init, e.g. to sample all transactions; "transport" wins over all.

Calling it again starts capturing afresh, into a new transport.

Initializing again closes the client the application initialized before, which sends what that client still holds (see "close" in Sentry::SDK): with a DSN in the environment, e.g. through Sentry::SDK::Auto, to Sentry.

stop

Sentry::Test->stop;

Stops capturing: initializes the SDK again with the options of the last "init" in Sentry::SDK, without those of "start".

events

my $events = $sentry->events;

The errors and messages sent, oldest first, as hashes like the JSON Sentry receives.

transactions

my $transactions = $sentry->transactions;

The transactions sent, oldest first.

feedback

my $feedback = $sentry->feedback;

The feedback sent (see "capture_feedback" in Sentry::SDK), oldest first, as events with the feedback in contexts->{feedback}.

logs

Sentry::SDK->logger->info('Order %s placed', [42]);
is $sentry->logs->[-1]{body}, 'Order 42 placed';

The logs sent (see "LOGS" in Sentry::SDK), oldest first, with their attributes typed as sent, e.g. { type => 'integer', value => 42 }. Sends the logs the client still holds in its batch first.

metrics

my $metrics = $sentry->metrics;

The metric values sent (see "METRICS" in Sentry::SDK), like "logs".

attachments

my $attachments = $sentry->attachments($sentry->last_event);
is $attachments->[0]{filename}, 'payload.json';

The attachments sent with an event of "events" or "feedback": their filename, content_type, attachment_type (if set) and bytes.

last_event

my $event = $sentry->last_event;

The error or message sent last, undef if none was.

envelopes

my $envelopes = $sentry->envelopes;

All Sentry::Envelope objects sent, oldest first, e.g. to check items other than events and transactions.

discarded_events

my $discarded = $sentry->discarded_events;
# [{ reason => 'before_send', category => 'error', quantity => 1 }]

What the SDK discarded instead of sending (see "discarded_events" in Sentry::Transport::Memory).

clear

$sentry->clear;

Forgets what was captured, e.g. between the cases of a test.

SEE ALSO

Sentry::SDK, Sentry::Transport::Memory