Tutorial: Integrating with proxyquire

In Node/Browserify, you need to patch require itself to override firebase calls. The trio of proxyquire (Node), proxyquireify (Browserify), and proxyquire-universal (both) make this easy.

Examples

RTDB

Source

// ./mySrc.js
var firebase = require('firebase');
var ref = firebase.database().ref('myRefUrl');
ref.on('value', function (snapshot) {
  console.log(snapshot.val());
});

Test

var proxyquire   = require('proxyquire');

var mySrc = proxyquire('./mySrc', {
  firebase: mocksdk
});
mocksdk.database().flush();
// data is logged

Firestore

Source

// ./mySrc.js
var firebase = require('firebase');
var ref = firebase.firestore().doc('users/123');
ref.get().then(function(doc) {
  console.log(doc.data());
}).catch(function(err) {
  console.error(err);
});

Test

// ./test.js
var mySrc = proxyquire('./mySrc', {
  firebase: mocksdk
});
mocksdk.firestore().flush();
// data is logged