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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay