Skip to main content

Network Interception

Sentry Testkit supports using network interception libraries, without the need to change your application's Sentry.init function. The testkit is agnostic to the interception library you use.



Sentry Testkit exports a initNetworkInterceptor callback function, where you can init your interceptor. The interceptor should intercept requests from baseUrl and pass the request body (as json) to the handleRequestBody function. For envelope requests (such as performance events), we use the emitted request event, since in a reply function nock automatically tries to parse the body as json, and fails since Sentry are using a non-standard json with a json request header.


See for more information on envelope requests.

Example using nock

const nock = require('nock')
const sentryTestkit = require('sentry-testkit')
const { testkit, initNetworkInterceptor } = sentryTestkit()

beforeAll(() => {
const myAppDSN = '<your DSN goes here>'
initNetworkInterceptor(myAppDSN, (baseUrl, handleRequestBody, handleEnvelopeRequestBody) => {
.reply(200, (_, requestBody) => {
.on('request', (_, interceptor, body) => {
if (interceptor.uri.test('/api/000000/envelope')) {

test('findReport example', async function() {
const err = new Error('error to look for')

// Some faulty scenario that will report err

const report = testkit.findReport(err)