代码之家  ›  专栏  ›  技术社区  ›  Manu Chadha

无法在Karma中指定javascript文件的路径

  •  0
  • Manu Chadha  · 技术社区  · 7 年前

    我的JS文件(我需要测试)是/JasmineTest/src/mySource。js。它有 myObj

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

    我的Jasmine规范文件是/JasmineTest/spec/mySpec。js。It测试 myObj公司

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

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

    当我在/JasmineTest中启动Karma时,测试会出错

    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') require is not defined

    如何使myObj在specs文件中可见?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Duncan Thacker    7 年前

    Karma不知道如何执行模块请求,除非您专门配置它来执行捆绑。总的来说,我希望在Karma中使用与你的web应用程序相同的捆绑方式,比如Webpack、Browserify或类似的捆绑方式。