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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

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

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay