Npm测试Firebase错误

时间:2017-12-14 00:54:57

标签: unit-testing firebase circleci

我正在尝试对我的代码运行一些测试。但每次我尝试运行它们都会返回

> stockmarketsimulator@0.0.0 test /home/ubuntu/workspace
 > mocha


home/ubuntu/workspace/node_modules/@firebase/app/dist/cjs/src       /firebaseApp.js:343
throw appErrors.create(code, args);
^
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
at error (/home/ubuntu/workspace/node_modules/@firebase/app/dist/cjs/src/firebaseApp.js:343:21)
at app (/home/ubuntu/workspace/node_modules/@firebase/app/dist/cjs/src/firebaseApp.js:227:13)
at Object.serviceNamespace [as database] (/home/ubuntu/workspace/node_modules/@firebase/app/dist/cjs/src/firebaseApp.js:283:47)
at Object.<anonymous> (/home/ubuntu/workspace/routes/news.js:7:25)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/ubuntu/workspace/test/test.js:8:12)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at /home/ubuntu/workspace/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (native)
at Mocha.loadFiles (/home/ubuntu/workspace/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/home/ubuntu/workspace/node_modules/mocha/lib/mocha.js:514:10)
at Object.<anonymous> (/home/ubuntu/workspace/node_modules/mocha/bin/_mocha:484:18)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3

但是当我在heroku上预览应用程序时,应用程序运行正常。

test.js

this.jsdom = require('jsdom-global')()
global.$ = global.jQuery = require('jquery');

 var expect = require("chai").expect;
 var test = require("../public/javascripts/testing");
 var getStock = require("../public/javascripts/getStock.js");
 var index = require("../routes/index.js");
var news = require("../routes/news.js");
var express = require('express');
var firebase = require('firebase');
var router = express.Router();
var yahooStocks = require("yahoo-stocks");


before(function(done) {
var config = {
    apiKey: process.env.firebase,
    authDomain: "xxxxxxxx",
    databaseURL: "xxxxxxxx",
    projectId: "xxxxx",
    storageBucket: "",
    messagingSenderId: "xxxxxxxxxxxxxx"
};
firebase.initializeApp(config);

firebase.auth().signInWithEmailAndPassword("default", "default");

done();
})


describe("Home page tests", function() {

it("Should Callback AAPL",function(done) {
    this.timeout(3000);
    news.get_stock_news("AAPL",function(data){
        console.log(data);
        expect(data).to.be.above(0);
        done();
    })
 })
 });

news.js

var express = require('express');
var firebase = require("firebase");
var router = express.Router();
var https = require("https");

// Get a reference to the database service
var database = firebase.database();
var googleFinance = require('google-finance');
var userUid;
var handle;
var signedIn = false;
var  names = [];
var info = [];
var urls = [];
var namesReturned = [];

var auth = "Basic " + new Buffer(username + ':' +         password).toString('base64');

   function get_stock_news(symbol,callback){
          var request = https.request({
            method: "GET",
            host: "api.intrinio.com",
            path:"/news?ticker="+symbol+"&page_size=1",
            headers: {
                "Authorization": auth
            }
            } , function(response) {
                  var json = "";
                  response.on('data', function (chunk) {
                      json += chunk;
                  });
                  response.on('end', function() {
                      var company = JSON.parse(json);
                      callback(company.data[0].ticker);

                  });
                });

 });

 module.exports = router;
 module.exports.get_stock_news = get_stock_news;

以上是我正在使用的代码。代码有效,所以如果有一些遗漏的半冒号或其他东西,它来自我的格式。问题是当我尝试运行一些测试时,我得到了firebase尚未初始化的错误

1 个答案:

答案 0 :(得分:0)

var database = firebase.database();中致电news.js之前,您无法在firebase.initializeApp()中致电test.js

您好像没有使用database,因此请删除news.js中的第7行并尝试一下。如果您需要进行其他测试(上面没有包含这些测试),请在database.js中初始化test.js并将其作为依赖项传递给news.get_stock_news函数或添加initDatabase(firebaseApp)news.js中运行并在每次测试前调用它以正确设置database