How to Mock Bugsnag in jest.setup.js

Lem - Jul 15 '23 - - Dev Community

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

. . . . . .
Terabox Video Player