Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha unit tests for Firebase app

I use firebase 3.3.0 and I want to use signInWithEmailAndPassword function in my mocha unit test, but I get error auth/network-request-failed

Unhandled rejection Error: A network error (such as timeout, interrupted connection or unreachable host) has occurred. 

test.js

const FIREBASE_CONFIG = {
    apiKey: "AIzaSyDdA1POUWy9eid1AdBYuMdxch_k8ob7Qrg",
    authDomain: "my-app.firebaseapp.com",
    databaseURL: "https://my-app.firebaseio.com",
    storageBucket: "my-app.appspot.com",
};

const FIREBASE_API_REF = firebase.initializeApp(FIREBASE_CONFIG);

before(function (done) {

        promise = new Promise(function (resolve, reject) {
            return FIREBASE_API_REF.auth().signInWithEmailAndPassword(firstUserEmail, firstUserPassword)
            .then(function (userData) {
                firstUserId = userData.uid;
                resolve(userData);
                done();
            }, function (error) {
                return reject(error);
            })
        });

    });

package.json

"scripts": {
    "test": "mocha --grep ./e2e.js --invert --compilers js:babel-register -R spec --ui bdd --timeout 7000"
  }
like image 950
Matt Avatar asked Aug 21 '16 22:08

Matt


People also ask

What is Mocha in unit testing?

Mocha is a widely used JavaScript test framework running on NodeJS and browsers. It supports asynchronous testing running the tests serially, allowing for more flexible and accurate reporting. It is a highly customizable framework that supports different assertions and libraries.

What is the difference between jest and mocha?

Jest is also faster than Mocha. It has built-in support for snapshot testing, which means tests are run automatically on each change to the code. This makes it easy to keep your tests up to date as you work. Mocha has more features out of the box since it is a more mature tool with a larger community of contributors.

Is Mocha A test runner?

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.


1 Answers

When you say, "I want to use signInWithEmailAndPassword function in my mocha unit test" then I would say to you, "why"?

Are you trying to help out the Firebase team by testing that their authentication service works? That's nice of you, but if you want to test YOUR app then you should NOT be calling out to firebase at all in a unit test. What you really want to check is that a response similar to what Firebase responds with is handled correctly by your application in the code that is run once the response comes back.

If I were tasked with writing a test for this I would use the sinon library with mocha and create a stub that calls a different function which returns some data instead of actually calling to Firebase:

This illustrates the syntax for a sinon stub:

var stub = sinon.stub(object, "method", func);

This is what I would do in your example:

var stub = sinon.stub(FIREBASE_API_REF.auth(), "signInWithEmailAndPassword"
,  () => { 

  // Simply return a JSON object that is similar to the normal response from Firebase
  return {
    name: "Jim",
    data: {
      something: "some stuff"
    }
});
like image 154
Jim Avatar answered Sep 21 '22 07:09

Jim