title: Test Firebase RealtimeDatabase using Jest
body: <p>My purpose is simply to test one function. I cannot figure out how to mock firebase properly. I try to keep the example with axios mocking from Jest docs. I have the following code:</p>
  
  <p><strong>MusicService.js</strong></p>
  
  <pre><code>import { initializeApp } from "firebase/app";
  import "firebase/database";
  
  const firebase = initializeApp({
    apiKey: "&lt;API_KEY&gt;",
    authDomain: "&lt;PROJECT_ID&gt;.firebaseapp.com",
    databaseURL: "https://&lt;DATABASE_NAME&gt;.firebaseio.com",
    projectId: "&lt;PROJECT_ID&gt;",
    storageBucket: "&lt;BUCKET&gt;.appspot.com",
    messagingSenderId: "&lt;SENDER_ID&gt;",
  });
  
  export class MusicService {
    static getAlbums() {
      return firebase.database().ref("albums").once("value")
      .then(snapshot =&gt; Object.values(snapshot.val()));
    }
  }
  </code></pre>
  
  <p><strong>MusicService.test.js</strong></p>
  
  <pre><code>import firebase from 'firebase/app';
  import 'firebase/database';
  import { MusicService } from './MusicService';
  
  jest.mock('firebase/app');
  jest.mock('firebase/database');
  
  test("test", () =&gt; {
    firebase.initializeApp.mockImplementation(() =&gt; {
      database: jest.fn(() =&gt; {
        return {
          ref: jest.fn()
        }
      })
    });
  
    MusicService.getAlbums();
  });
  </code></pre>
  
  <p>The problem is that I get the following error:
  <a href="https://i.stack.imgur.com/2TN3v.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2TN3v.png" alt="enter image description here"></a></p>
  
  <p>I tried to mock firebase.database.</p>
  
  <pre><code>test("test", () =&gt; {
    firebase.mockImplementation(() =&gt; {
      return {
        database: {
  
        }
      }
    });
    MusicService.getAlbums();
  });
  </code></pre>
  
  <p>But in this case I get the error that says:</p>
  
  <blockquote>
    <p>TypeError: _app.default.mockImplementation is not a function.</p>
  </blockquote>
  
  <p>I don't expect the working example will be given, but rather could you tell please, what exactly should I mock? The whole firebase library or maybe the part where my function starts -  <code>return firebase.database()</code>.</p>