3

I tried run Jest unit tests on React-Native with Bugsnag, but I got error:

 The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/en/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.
    
    ReferenceError: window is not defined
      1 | import React from 'react';
    > 2 | import Bugsnag from '@bugsnag/react-native';

2 Answers2

2

Solution which is working for me:

we need to add mock bugsnag interceptor to jestSetupFile.js

jest.mock('@bugsnag/react-native', () => ({
  use(plugin) {
    const boundary = plugin.init();
    // we don't need the error boundary to swallow the errors, we want jest see them and fail the test
    delete boundary.prototype.componentDidCatch;
    return boundary;
  }
})); 
1

I fixed it with just mocking Bugsnag: jest.mock("@bugsnag/react-native", () => jest.fn());

Andrija
  • 937
  • 14
  • 33