Sentry Testkit | Sentry for Next.js
Source URL: https://docs.sentry.io/platforms/javascript/guides/nextjs/best-practices/sentry-testkit
Sentry Testkit | Sentry for Next.js
Section titled “Sentry Testkit | Sentry for Next.js”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 the Sentry servers. This way you won’t swamp Sentry with false reports during test runs or other CI operations.
Sentry Testkit is a community-maintained Sentry plugin that allows Sentry’s reports to be intercepted for further data inspection. It enables Sentry to work natively in your application, by overriding Sentry’s default transport mechanism, which makes it so that the report isn’t really sent, but rather logged locally into memory. This way, logged reports can be fetched later for your own usage, verification, or any other purpose you may have in your local developing or testing environment.
Sentry Testkit is community-maintained and not officially supported by Sentry. Please open an issue in the Sentry Testkit repository if you have any questions or feedback.
npm install sentry-testkit --save-devimport sentryTestkit from "sentry-testkit";
const { testkit, sentryTransport } = sentryTestkit();
// initialize your Sentry instance with sentryTransportSentry.init({ dsn: "___PUBLIC_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 eventstest("collect performance events", function () { // run any scenario that eventually calls Sentry.startTransaction(...) expect(testkit.transactions()).toHaveLength(1);});You may see more usage examples in the testing section of sentry-testkit repository as well.
Sentry Testkit consists of a very simple and straightforward API. See the full API description and documentation in Sentry Testkit Docs.