2016-11-24 48 views
0

我正在爲Angular 2應用程序設置Karma。我的karma-test-shim.js中的System.import正在拋出一個錯誤,我相信這是因爲我錯誤地導入了我在karma.conf.js中放置的內容。System.import未在karma-test-shim.js中處理

我karma.conf.js(下調):

'use strict'; 

var argv = require('yargs').argv; 

module.exports = function (config) { 
    config.set({ 
    basePath: './', 
    frameworks: ['jasmine'], 
    files: [ 
     'node_modules/core-js/client/shim.min.js', 
     'node_modules/traceur/bin/traceur.js', 
    // System.js for module loading 
    'node_modules/systemjs/dist/system.src.js', 

    // Zone.js dependencies 
    'node_modules/zone.js/dist/zone.js', 
    'node_modules/zone.js/dist/long-stack-trace-zone.js', 
    'node_modules/zone.js/dist/async-test.js', 
    'node_modules/zone.js/dist/fake-async-test.js', 
    'node_modules/zone.js/dist/sync-test.js', 
    'node_modules/zone.js/dist/proxy.js', 
    'node_modules/zone.js/dist/jasmine-patch.js', 

    // Paths loaded via module imports 
    { pattern: 'node_modules/@angular/**/*.js', included: false, watched: true }, 
    { pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false }, 

    { pattern: 'app/**/*.js', included: false, watched: true }, 
    { pattern: 'app/**/*.html', included: false, watched: true, served: true }, 
    { pattern: 'app/**/*.css', included: false, watched: true, served: true }, 
    { pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: false, watched: false }, 
    { pattern: 'karma-test-shim.js', included: true, watched: true}, 
], 
// Proxied base paths 
proxies: {}, 
exclude: [ 
    'node_modules/**/*spec.js' 
], 
reporters: ['mocha'], 
port: 9876, 
colors: true, 
logLevel: config.LOG_DEBUG, 
autoWatch: false, 
// TODO: Remove sandbox. This is just work around for Chrome issue 
// https://github.com/karma-runner/karma-chrome-launcher/issues/73#issuecomment-236597429 
browsers: ['ChromeNoSandbox'], 
customLaunchers: { 
    ChromeNoSandbox: { 
    base: 'Chrome', 
    flags: ['--no-sandbox'] 
    } 
}, 
singleRun: true, 

// Command line args for tests 
client: { 
    files: argv.files 
    } 
    }); 
}; 

我的人緣試驗shim.js:

// Turn on full stack traces in errors to help debugging 
Error.stackTraceLimit=Infinity; 


jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; 

// // Cancel Karma's synchronous start, 
// // we will call `__karma__.start()` later, once all the specs are loaded. 
__karma__.loaded = function() {}; 


System.config({ 
    packages: { 
    'base/app': { 
     defaultExtension: false, 
     format: 'register', 
     map: Object.keys(window.__karma__.files). 
      filter(onlyAppFiles). 
      reduce(function createPathRecords(pathsMapping, appPath) { 
       var moduleName = appPath.replace(/^\/base\/app\//, './').replace(/\.js$/, ''); 
       pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath] 
       return pathsMapping; 
      }, {}) 

     } 
    } 
}); 

Promise.all([ 
    System.import('@angular/core/testing'), 
    System.import('@angular/platform-browser-dynamic/testing') 
]) 
    // First, initialize the Angular testing environment. 
    .then(([testing, testingBrowser]) => { 
    // testing.getTestBed().initTestEnvironment(
    // testingBrowser.BrowserDynamicTestingModule, 
    // testingBrowser.platformBrowserDynamicTesting() 
    //); 
    testing.setBaseTestProviders(testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, 
     testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); 
    }) 
    // Then we find all the tests. 
    .then(() => require.context('./', true, /\.spec\.ts/)) 
    // And load the modules. 
    .then(context => context.keys().map(context)) 
    // Finally, start Karma to run the tests. 
    .then(__karma__.start, __karma__.error); 


function filePath2moduleName(filePath) { 
    return filePath. 
      replace(/^\//, '').    // remove/prefix 
      replace(/\.\w+$/, '');   // remove suffix 
} 


function onlyAppFiles(filePath) { 
    return /\/base\/app\/(?!.*\.spec\.js$).*\.js$/.test(filePath); 
} 


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

我得到的錯誤:

24 11 2016 18:19:31.954:DEBUG [middleware:source-files]: Requesting /@angular/core/testing/
24 11 2016 18:19:31.954:DEBUG [middleware:source-files]: Fetching /@angular/core/testing 
24 11 2016 18:19:31.956:WARN [web-server]: 404: /@angular/core/testing 
24 11 2016 18:19:31.958:DEBUG [middleware:source-files]: Requesting /@angular/platform-browser-dynamic/testing/
24 11 2016 18:19:31.959:DEBUG [middleware:source-files]: Fetching /@angular/platform-browser-dynamic/testing 
24 11 2016 18:19:31.962:WARN [web-server]: 404: /@angular/platform-browser-dynamic/testing 
Chrome 54.0.2840 (Windows 7 0.0.0) ERROR 
    { 
    "originalErr": {} 
    } 

回答

0

您的karma-test-shim.js是prob錯誤,您需要更改地圖

System.config({ 
    baseURL: '/base', 
    // Extend usual application package list with test folder 
    packages: {'testing': {main: 'index.js', defaultExtension: 'js'}}, 

    // Assume npm: is set in `paths` in systemjs.config 
    // Map the angular testing umd bundles 
    map: { 
    '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js', 
    '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js', 
    '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js', 
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js', 
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js', 
    '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js', 
    '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js', 
    '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js', 
    }, 
}); 

強大的文本

+0

是啊,這是錯的映射。 –