2017-08-31 22 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等,所以尝试了第二种方法。 –