Getting Started
Installation
npm:
npm install sentry-testkit --save-dev
yarn:
yarn add sentry-testkit --dev
Usage
// some.spec.js
const sentryTestkit = require('sentry-testkit')
const {testkit, sentryTransport} = sentryTestkit()
// initialize your Sentry instance with sentryTransport
Sentry.init({
dsn: 'some_dummy_dsn',
transport: sentryTransport,
//... other configurations
})
test('collect error events', function () {
// run any scenario that eventually calls Sentry.captureException(...)
expect(testkit.reports()).toHaveLength(1)
const report = testkit.reports()[0]
expect(report).toHaveProperty(...)
});
// Similarly for performance events
test('collect performance events', function () {
// run any scenario that eventually calls Sentry.startTransaction(...)
expect(testkit.transactions()).toHaveLength(1)
});
Using with 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);
startListening
has an optional baseUrl
as second parameter (it defaults to 'https://sentry.io'), so you can pass the URL of your server:
testkit.puppeteer.startListening(page, 'https://my-self-hosted-sentry.com');
Reset between tests
As you might run more than one test with Sentry and Sentry-Testkit, you might want to use the reset
function in between tests.
Usually, your testing framework will have a hook for that kind of action. In the following example, We're using Jest's hooks: beforeEach
, beforeAll
beforeEach(function(){
testkit.reset()
})
beforeAll(function(){
testkit.reset()
})
You may see more usage examples in the testing section of sentry-testkit
repository as well
Pay attention that Sentry reports the events asynchronously.
Even though captureException
is synchronous function and you can get the eventId
right away, the reporting itself still goes to an asynchronous flow.
Hence, it depends what you are testing and the chain of events caused by your test case scenario,
you will may need to use async/await
and tools like wix-eventually
or waitForExpect