2014-04-07 60 views
4

我試圖讓茉莉花測試在Karma中運行。這是針對使用RequireJS的AngularJS應用程序,但目前,測試是一個簡單的expect(true).toBe(true);測試,沒有Angular依賴關係。當我禁用Karma的requirejs模塊並註釋掉"client/app/require-shared.js""test/require-test.js",files並直接加載testSpec.js文件時,我發現它運行。「Uncaught TypeError:無法讀取屬性'...'的空」使用RequireJS與Karma

然而,當我重新啓用requirejs和背部添加RequireJS配置文件來karma.conf.js的files --keeping的testSpec.js文件中有被「靜態」裝 - 我看到了錯誤

Chrome 33.0.1750 (Windows 7) ERROR 
    Uncaught TypeError: Cannot read property 'failedCount' of null 
    at C:/.../project/node_modules/karma-jasmine/lib/adapter.js:126 
Chrome 33.0.1750 (Windows 7) ERROR 
    Uncaught TypeError: Cannot read property 'length' of null 
    at C:/.../project/node_modules/karma-jasmine/lib/jasmine.js:2518 
Chrome 33.0.1750 (Windows 7): Executed 1 of 2 ERROR (0.009 secs/0.001 secs) 

如果我嘗試全角測試,我得到的錯誤有關$injector是空的,但我覺得這一切都植根於這個requirejs模塊問題。我在設置中缺少什麼來完成這項工作?

測試/ karma.conf.js

module.exports = function(config) { 

    config.set({ 
     basePath: "../", // resolves to "project/" 

     frameworks: ["jasmine", "requirejs"], 

     files: [ 
      // load the RequireJS config files first 
      "client/app/require-shared.js", 
      "test/require-test.js", 

      // set included to false for files to be loaded via RequireJS 
//   {pattern: "bower_components/**/*.js", included: false}, 
//   {pattern: "client/**/*.js",   included: false}, 
//   {pattern: "test/unit/*Spec.js",  included: false} 
      {pattern: "test/unit/*Spec.js"} 
     ], 


     // list of files to exclude 
     exclude: [ 
//   "client/app/app.js", 
//   "client/app/bootstrap.js", 
//   "client/app/require-main.js", 
      "*.conf.js" 
     ], 

     reporters: ["progress"], 
     port: 9876, 
     colors: true, 
     logLevel: config.LOG_INFO, 
     autoWatch: false, 
     browsers: ["Chrome"], 
     singleRun: true 
    }); 
}; 

客戶端/應用程序/需要-shared.js

requirejs.config({ 
    paths: { 
     "app":   "../app/app", 
     "angular":  "../extlibs/angular-1.2.14", 
     "jquery":   "../extlibs/jquery-2.1.0.min", 
     "ng-cookies":  "../extlibs/AngularPlugins/angular-cookies-1.2.13", 
     "ng-resource": "../extlibs/AngularPlugins/angular-resource-1.2.13", 
     "ng-route":  "../extlibs/AngularPlugins/angular-route-1.2.14", 
    } 

    ,shim: { 
     angular: { 
      exports: "angular" 
     } 
     ,"ng-resource": { 
      deps: [ "angular" ], 
      exports: "ng-resource" 
     }, 
     ,"ng-cookies":  ["angular"] 
     ,"ng-route":  ["angular"] 
     ,"ngui-bootstrap": ["angular"] 
    } 
}); 

測試/需要-test.js

var allTestFiles = []; 
var TEST_REGEXP = /(spec|test)\.js$/i; 

var pathToModule = function(path) { 
    return path.replace(/^\/base\//, "").replace(/\.js$/, ""); 
}; 

Object.keys(window.__karma__.files).forEach(function(file) { 
    if (TEST_REGEXP.test(file)) { 
     // Normalize paths to RequireJS module names. 
     var normalPath = pathToModule(file); 
     console.log("adding " + file + " as " + normalPath + " to allTestFiles array"); 
     allTestFiles.push(normalPath); 
    } 
}); 

requirejs.config({ 
    baseUrl: "/base/client/components/" 

    /* 
    * "path" is defined in require-shared.js and shared with 
    * require-main. Add paths for the testing files. 
    */ 
    ,paths: { 
     "test":   "../../test", 
     "bower":   "../../bower_components", 
     "angular-mocks": "bower/angular-mocks/angular-mocks" 
    } 

    ,shim: { 
     'angular-mocks': { 
      deps: [ 'ng-resource' ], 
      exports: 'angular.mock' 
     } 
    } 

    ,deps: allTestFiles 
    ,callback: window.__karma__.start 
}); 

測試/單元/ testSpec.js

describe('a basic test of version', function() { 
    it('true should be true', function() { 
     expect(true).toBe(true); 
    }); 
}); 

回答

2

的問題是,由karma init產生的TEST_REGEXP尋找任spec或在文件名test。這導致require-test.js文件作爲測試規範被引入。

該溶液是

  • 重命名需要-test.js一個
  • 變化(spec|test)在RE
  • 變化的RE到/^(?:(?!require-test\.js$).)*(spec|test)\.js$/i(還參見,this answer
只是 spec
0

這也可能是你的問題的原因,因爲它在其他情況下報告相同的錯誤(缺少package.json):https://github.com/tkellen/js-matchdep/issues/11。但是,如果您看到此結果的錯誤消息,那麼您運行的是舊版本的npm。升級會給你一個不同的錯誤信息(根據線程)。

相關問題