Skip to content
A Sentry plugin to allow Sentry report interception and further inspection of the data being sent
JavaScript
Branch: master
Clone or download
zivl Merge pull request #35 from wix/docsify-alerts-plugin
adoption of docsify-plugin-flexible-alerts
Latest commit edb371e Oct 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
src
test
.babelrc
.eslintignore Don't run eslint on the dist folder Aug 7, 2019
.eslintrc.json
.gitignore enable package-lock file for npm ci Jan 6, 2019
.npmignore renovate and update documentation Jan 8, 2019
.npmrc enable package-lock file for npm ci Jan 6, 2019
.nvmrc #10 add travis CI configuration Jan 6, 2019
.prettierrc
.travis.yml
LEGACY_API.md create legacy api Jan 8, 2019
LICENSE
README.md
index.d.ts Add a new network interception testing capability Aug 7, 2019
jest.config.json Don't transpile JS test files Aug 5, 2019
package-lock.json
package.json update package.json description Aug 8, 2019
wallaby.js Remove semicolons Aug 5, 2019

README.md

sentry-teskit

npm version GitHub Hackage-Deps Build Status

Sentry is an open-source JavaScript SDK published by Sentry to enable error tracking that helps developers monitor and fix crashes in real time.
However, when building tests for your application, you want to assert that the right flow-tracking or error is being sent to Sentry, but without really sending it to Sentry servers. This way you won't swamp Sentry with false reports during test running and other CI operations.

Sentry Testkit - to the rescue

Sentry Testkit enables Sentry to work natively in your application, and by overriding the default Sentry transport mechanism, the report is not really sent but rather logged locally into memory. In this way, the logged reports can be fetched later for your own usage, verification, or any other use you may have in your local developing/testing environment.

Usage

Installation

npm install sentry-testkit --save-dev

Using in tests

const sentryTestkit = require('sentry-testkit')

const {testkit, sentryTransport} = sentryTestkit()
const DUMMY_DSN = 'https://acacaeaccacacacabcaacdacdacadaca@sentry.io/000001';

// initialize your Sentry instance with sentryTransport
Sentry.init({
    dsn: DUMMY_DSN,
    transport: sentryTransport,
    //... other configurations
})

// then run any scenario that should call Sentry.catchException(...)

expect(testkit.reports()).toHaveLength(1)
const report = testkit.reports()[0]
expect(report).toHaveProperty(...)

Network interception support

Instead of modifying your application code, you can use network interception libraries in conjunction with the testkit.
Example with 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) => {
      // This callback is where we init our interceptor.
      // The interceptor should intercept requests from `baseUrl` and pass the
      // request body (as json) to the `handleRequestBody` function.
      nock(baseUrl)
        .persist()
        .post(/.*/)
        .reply(200, (_, requestBody) => {
          handleRequestBody(requestBody)
        })
    })
})

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)
    expect(report).toBeDefined()
})

Yes! We Love Puppeteer

const sentryTestkit = require('sentry-testkit')

const {testkit} = sentryTestkit()

testkit.puppeteer.startListening(page);

// Run any scenario that will call Sentry.captureException(...), for example:
await page.addScriptTag({ content: `throw new Error('An error');` });

expect(testKit.reports()).toHaveLength(1)
const report = testKit.reports()[0]
expect(report).toHaveProperty(...)

testkit.puppeteer.stopListening(page);

You may see more usage examples in the testing section of this repository as well.

Test Kit API

See full API description and documentation here: https://wix.github.io/sentry-testkit/

What About Nodejs?

Of Course! sentry-testkit have full support in both @sentry/browser and @sentry/node since they have the same API and lifecycle under the hood.

Raven-Testkit

The good old legacy raven-testkit documentation can be found here. It it still there to serve Raven which is the old legacy SDK of Sentry for JavaScript/Node.js platforms

You can’t perform that action at this time.