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.
// ./mySrc.js
var firebase = require('firebase');
var ref = firebase.database().ref('myRefUrl');
ref.on('value', function (snapshot) {
console.log(snapshot.val());
});
var proxyquire = require('proxyquire');
var mySrc = proxyquire('./mySrc', {
firebase: mocksdk
});
mocksdk.database().flush();
// data is logged
// ./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.js
var mySrc = proxyquire('./mySrc', {
firebase: mocksdk
});
mocksdk.firestore().flush();
// data is logged