2016-07-06 37 views
0

我試圖安裝Karma與茉莉花來測試我的Angular2應用程序。 Karma無法在規範文件中追加導入擴展名。例如,我的規格文件有import {TileComponent} from './tiles.component'; 當我運行Karma時,出現以下錯誤:http://localhost:9876/base/wwwroot/app/tiles/tiles.component 404 (Not Found) 如果我嘗試訪問此鏈接並在最後手動添加js,它將加載我的文件。Angular 2的Karma安裝程序。

karma.conf.js:

module.exports = function (config) { 
 

 
    var appBase = 'wwwroot/app/'; 
 
    var appAssets = '/base/app/'; 
 

 
    config.set({ 
 
     basePath: '', 
 
     frameworks: ['jasmine'], 
 
     plugins: [ 
 
      require('karma-jasmine'), 
 
      require('karma-chrome-launcher'), 
 
      require('karma-htmlfile-reporter') 
 
     ], 
 

 
     customLaunchers: { 
 
      Chrome_travis_ci: { 
 
       base: 'Chrome', 
 
       flags: ['--no-sandbox'] 
 
      } 
 
     }, 
 
     files: [    'node_modules/systemjs/dist/system.src.js', 
 

 
      'node_modules/core-js/client/shim.js', 
 

 
      'node_modules/reflect-metadata/Reflect.js', 
 
      'node_modules/zone.js/dist/zone.js', 
 
      'node_modules/zone.js/dist/jasmine-patch.js', 
 
      'node_modules/zone.js/dist/async-test.js', 
 
      'node_modules/zone.js/dist/fake-async-test.js', 
 

 
      // RxJs. 
 
      { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false }, 
 
      { pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false }, 
 

 
      { pattern: 'node_modules/@angular/**/*.js', included: false, watched: false }, 
 
      { pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false }, 
 

 
      { pattern: 'systemjs.config.js', included: false, watched: false }, 
 
      //{ pattern: 'karma-test-shim.js', included: false, watched: false }, 
 
      //'systemjs.config.js', 
 
      'karma-test-shim.js', 
 
      { pattern: appBase + '**/*.js', included: false, watched: true }, 
 

 
      { pattern: appBase + '**/*.html', included: false, watched: true }, 
 
      { pattern: 'wwwroot/styles/**/*.css', included: false, watched: true }, 
 

 
      { pattern: 'app/**/*.ts', included: false, watched: false }, 
 
      { pattern: appBase + '**/*.js.map', included: false, watched: false } 
 
     ], 
 

 
     proxies: { 
 
      "/app/": appAssets 
 
     }, 
 

 
     exclude: [], 
 
     preprocessors: {}, 
 
     reporters: ['progress', 'html'], 
 

 
     htmlReporter: { 
 
      outputFile: '_test-output/tests.html', 
 

 
      pageTitle: 'Unit Tests', 
 
      subPageTitle: __dirname 
 
     }, 
 

 
     port: 9876, 
 
     colors: true, 
 
     logLevel: config.LOG_INFO, 
 
     autoWatch: true, 
 
     browsers: ['Chrome'], 
 
     singleRun: false 
 
    }) 
 
}

卡瑪測試shim.js

// /*global jasmine, __karma__, window*/ 
 
Error.stackTraceLimit = Infinity; 
 
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; 
 

 
__karma__.loaded = function() { 
 
}; 
 

 
function isJsFile(path) { 
 
    return path.slice(-3) === '.js'; 
 
} 
 

 
function isSpecFile(path) { 
 
    return /\.spec\.js$/.test(path); 
 
} 
 

 
function isBuiltFile(path) { 
 
    var builtPath = '/base/wwwroot/'; 
 
    return isJsFile(path) && (path.substr(0, builtPath.length) === builtPath); 
 
} 
 

 
var allSpecFiles = Object.keys(window.__karma__.files) 
 
    .filter(isSpecFile) 
 
    .filter(isBuiltFile); 
 

 
System.config({ 
 
    baseURL: '/base', 
 
    packageWithIndex: true, // sadly, we can't use umd packages (yet?) 
 
    map: { 
 
     'http://localhost:9876/base/wwwroot/app/app.component': 'http://localhost:9876/base/wwwroot/app/app.component.js' 
 
    } 
 
}); 
 

 
System.import('systemjs.config.js') 
 
    .then(function() { 
 
     return Promise.all([ 
 
     System.import('@angular/core/testing'), 
 
     System.import('@angular/platform-browser-dynamic/testing') 
 
     ]) 
 
    }) 
 
    .then(function (providers) { 
 
     var testing = providers[0]; 
 
     var testingBrowser = providers[1]; 
 

 
     testing.setBaseTestProviders(
 
     testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, 
 
     testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); 
 

 
    }) 
 
    .then(function() { 
 
     // Finally, load all spec files. 
 
     // This will run the tests directly. 
 
     return Promise.all(
 
     allSpecFiles.map(function (moduleName) { 
 
      return System.import(moduleName); 
 
     })); 
 
    }) 
 
    .then(__karma__.start, __karma__.error);

回答

0

的問題是,我是進口system.config.j s兩次。我從karma.conf.js文件中刪除了導入。 我也做了一些小的路徑更改。

+0

我也收到了這個。我沒有包含兩次systemjs.config.js。你做了什麼其他的小路徑改變?我在這裏拉我的頭髮! – Brian

+0

我記不清我究竟發生了什麼變化,因爲它已經快一年了。我建議檢查並確保所有路徑都指向正確的目錄。 – Yhlas