2017-08-31 20 views
0

我的JS文件(我需要測試)是/JasmineTest/src/mySource.js。它有myObj對象無法指定Karma中的JavaScript文件路徑

myObj={ 
    setA:function(value){ 
     a=value; 
    }, 
    getA:function(){ 
     return a; 
    }, 
}; 

我的茉莉花規範文件是/JasmineTest/spec/mySpec.js。它測試myObj

describe("Jasmine sample suite",function(){ 
    it("tracks that spy was called",function(){ 
     expect(myObj.getA).toHaveBeenCalled(); 
    }); 

因果報應,我指定的規範文件位置爲

files: [ 
     'spec/*.js' 
    ], 

,當我在/ JasmineTest開始噶,這個測試給錯誤

Chrome 60.0.3112 (Windows 10 0.0.0) Jasmine sample suite tracks that spy was called FAILED 
     ReferenceError: myObj is not defined 
      at UserContext.<anonymous> (spec/mySpec.js:4:9) 

我試圖出口myObj module.exports = myObj;並使用require('../src/mySource.js')將其導入到spec文件中,但出現錯誤require is not defined

如何使myObj在規格文件中可見?

回答

0

Karma不知道如何做模塊要求,除非你專門配置它來做捆綁。一般而言,我希望在Karma中使用與您的Web應用程序相同的綁定類型,例如Webpack或Browserify或類似的綁定。

另一種方法是將mySource.js列入karma.conf.js中的「files」字段中,該字段將執行它並將myObj作爲全局變量使用,但不能很好地擴展。

+0

我不知道如何使用webpack等,所以嘗試了第二種方法。 –