2016-03-15 62 views
4

如果我的應用程序代碼使用ng2-boostrap模塊(取決於moment.js庫),我無法在Karma中運行jasmine測試。如何在Karma中配置System.js以測試依賴於moment.js的代碼

該應用程序在瀏覽器中正常工作,而茉莉測試可以在瀏覽器中通過添加映射時刻系統運行:

-- unit-test.html --- 
... 
<script src="node_modules/ng2-bootstrap/bundles/ng2-bootstrap.min.js"></script> 
... 
<body> 
<script> 
    // #2. Configure SystemJS to use the .js extension 
    //  for imports from the app folder 
    System.config({ 
     packages: { 
      'app': {defaultExtension: 'js'} 
     }, 
     map: { 
      'moment': 'node_modules/moment/moment.js' 
     } 
    }); 

我試圖同裏面karma.shim但它不」工作。我越來越:

Error: XHR error (404 Not Found) loading http://localhost:9876/node_modules/moment/moment.js 
Error loading http://localhost:9876/node_modules/moment/moment.js as "moment" from http://localhost:9876/ng2-bootstrap/components/datepicker/date-formatter 
at addToError (D:/Projects/Angular/karma-ngbootstrap/node_modules/systemjs/dist/system.src.js:41:18) 

在我karma.conf我有

files: [ 
    ... 
    {pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true},  
    .... 
    {pattern: 'node_modules/moment/moment.js',included: true, watched: true}, 
    {pattern: 'node_modules/ng2-bootstrap/bundles/ng2-bootstrap.min.js', included: true, watched: true}, 

    {pattern: 'karma-test-shim.js', included: true, watched: true}, 

    {pattern: 'app/**/*.js', included: false, watched: true}, 
    .... 

而在因果報應 - 測試 - 墊片:

System.config({ 
    packages: { 
     'base/app': { 
      defaultExtension: false, 
      format: 'register', 
      map: Object.keys(window.__karma__.files). 
      filter(onlyAppFiles). 
      reduce(function createPathRecords(pathsMapping, appPath) { 
       // creates local module name mapping to global path with karma's fingerprint in path, e.g.: 
       // './hero.service': '/base/src/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e' 
       var moduleName = appPath.replace(/^\/base\/app\//, './').replace(/\.js$/, ''); 
       pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath] 
       return pathsMapping; 
      }, {}) 
     } 
    }, 
    map: { 
     'moment': 'node_modules/moment/moment.js' 
    } 
}); 

映射部件種類的作品,根據病因我在moment下放了什麼,後來在XHR錯誤路徑中找到它。

完整的文件可以在簡單的測試項目,請訪問: https://github.com/tzielins/angular-start-project

業力的配置是基於http://twofuckingdevelopers.com/2016/01/testing-angular-2-with-karma-and-jasmine/

我很高興用系統自帶的一個簡單的配置,但我運氣經驗。 js/Karma從頭開始,並且這個工作沒有ng2-boostrap依賴性(被測試的代碼只能導入形式爲ng2-boostrap的腳本,這足以啓動業力,它可以被註釋掉以便通過測試)。

我必須從System.js配置丟失一些明顯的東西。

回答

0

我使用新的「ng2-bootstrap」 - 「ngx-bootstrap」 與angular-seed並且也遇到了同樣的問題。

解決了該問題 - 添加以下代碼爲 「karma.conf.js」,下文件部分:

{pattern: 'node_modules/ngx-bootstrap/bundles/ngx-bootstrap.umd.min.js', included: false, watched: false} 
相關問題