'Firebase initializeApp does not work in jest

I created my custom react project from sratch (I setup all needed configuration like webpack etc.). In this project I also use firebase. I created a file called fireConfig.ts in src folder and i imported it in my index.tsx file so that firebase.initializeApp() can be run. With this config everything works fine while developing. Unfortunately there is a problem while testing with jest. When I test some component, jest doesn't see my firebase.initializeApp (I understand it because it is imported in index.tsx, not in tested file so it is a different scope). I can obviously solve it by importing fireConfig in each test file, but it doesn't seem like a perfect solution for me. So here is my question:

is there a way to run this fireConfig.ts (for each file) without importing it anywhere? (with usage of webpack or something like this).

I know that in create-react-app I don't need import anything anywhere, i just create fireConfig file and that's all, create-react-app runs it for me. Someone knows how does it work in create-react-app or knows any other way?

// fireConfig.ts

import firebase from 'firebase/compat/app';
import 'firebase/auth';

const firebaseConfig = {...};

export const fire = firebase.initializeApp(firebaseConfig);


// index.tsx

import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import './fireConfig';

import GlobalContexts from './context/globalContexts';
import App from './App';

ReactDOM.render(
  <BrowserRouter>
    <GlobalContexts>
      <App />
    </GlobalContexts>
  </BrowserRouter>,
  document.getElementById('root')
);




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source