title: Testing Firebase rules in typescript
body: <p>I am setting up integration tests for my model layer and Firebase rules. All written in TypeScript. A basic test example is something like this:</p>
  
  <pre><code>it('should not be able to write to his own public profile with occupied user name', (done) =&gt; {
    const id = new UserID(testUser.uid);
    const validPublicProfile = new UserPublicProfile(id, 'occupied_user_name', new Timestamp(), 'testUser');
    const ref = UserPublicProfile.reference(id);
    firebaseModule.writeToRef(ref, validPublicProfile)
      .then((snap) =&gt; {
        expect(true).to.be.false;
        done();
      })
      .catch((err) =&gt; {
        expect(err.code).to.equal(PERMISSION_DENIED_ERROR_CODE);
        done();
      });
  }).timeout(TEST_TIMEOUT);
  </code></pre>
  
  <p>This creates a public profile and gives it the user name <code>occupied_user_name</code>.</p>
  
  <p>When I set up my tests I would like to use the admin SDK to set up a predefined database, that e.g. has a user in it with the user name <code>occupied_user_name</code>.</p>
  
  <p>So, I need the admin SDK to setup the database, and the client SDK to test the rules. When I am using both the admin SDK and the client SDK I get this error:
  <code>
  FIREBASE FATAL ERROR: Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.
  </code></p>
  
  <p>I simply found where the error was being thrown in the firebase package (<code>node_modues/@firebase/database/dist/cjs/src/core/RepoManager.js -&gt; line 116</code>) and commented it out. Now I can run my tests and they all work just as expected.</p>
  
  <p>However, I am setting up CircleCI on my database now, and it can not run the tests because of the <code>Datanase initialized multiple times.</code> error.</p>
  
  <p>I have a file called <code>firebaseClient.ts</code> that looks like this:</p>
  
  <pre><code>import * as firebase from 'firebase'
  import * as firebaseProjectManager from './../../functions/firebaseProjectManager'
  
  const config = firebaseProjectManager.getClientConfig();
  firebase.initializeApp(config)
  const database = firebase.database();
  
  export function deleteFromRef(ref) {
    return database
      .ref(ref)
      .remove();
  }
  .
  .
  .
  </code></pre>
  
  <p>And a <code>firebaseAdmin.ts</code> file that looks something like this:</p>
  
  <pre><code>import * as admin from 'firebase-admin'
  import * as firebaseProjectManager from './../../functions/firebaseProjectManager'
  const databaseExample = require('./../database_example.json');
  
  let rootID: string
  
  admin.initializeApp({
    credential: admin.credential.cert(firebaseProjectManager.getAdminCert()),
    databaseURL: firebaseProjectManager.getDatabaseURL(),
  });
  
  const adminDb = admin.database();
  
  export default admin
  
  export function setUpTestRoot() {
    return adminDb.ref().push(databaseExample)
      .then((snap) =&gt; {
        rootID = snap.key;
        return rootID;
      });
  }
  .
  .
  .
  </code></pre>
  
  <p>And in my tests, I set up the database with the <code>firebaseAdmin.ts</code> file and then modify the database with the <code>firebaseClient.ts</code> file.</p>
  
  <p>Am I going about this the wrong way? or am I forced to fork the firebase repo, uncomment the line that throws the error, and use that instead? </p>