DEV Community

Cover image for How to Mock Bugsnag in jest.setup.js
Lem Dulfo
Lem Dulfo

Posted on

2

How to Mock Bugsnag in jest.setup.js

If you're testing with Jest, perhaps you already know to add manual mocks in your __mocks__ directory

├── src
│   └── index.js
├── __mocks__
│   └── @bugsnag
│       └── react-native.js
├── node_modules
├── jest.config.js
├── jest.setup.js
└── package.json
Enter fullscreen mode Exit fullscreen mode

With __mocks__/@bugsnag/react-native.js looking like:

module.exports = {
  leaveBreadcrumb: jest.fn(),
  notify: jest.fn(),
  start: jest.fn(),
  // other functions you call in your code
};
Enter fullscreen mode Exit fullscreen mode

However, if you already have jest.setup.js, you can mock Bugsnag like this:

// inside jest.setup.js
jest.mock("@bugsnag/react-native", () => ({
  leaveBreadcrumb: jest.fn(),
  notify: jest.fn(),
  start: jest.fn(),
  // other functions you call in your code
}));

// other mocked modules
Enter fullscreen mode Exit fullscreen mode

The two approaches will work, so choose one or the other depending on you or your team's preference or need.

Sources:
Mocking Node modules
jest.mock

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more