2016-05-26 19 views
0

Ich versuche, Karma auf einem Angular2-Projekt einzurichten, und ich versuche, den angular2-Schnellstart zu referenzieren, der Karma mit Jasmin erfolgreich ausführt.Ungültiger System.Register-Aufruf beim Einrichten von Karma mit angular2

Bis jetzt habe ich das Karma-Test-Shim importiert, meine karma.conf.js-Datei zu dem geändert, was ich glaube, dass ich brauche. Ich glaube, ich in den Tests bin ziehen erfolgreich, aber Karma klagt mit diesem Fehler

Uncaught TypeError: Invalid System.register call. Anonymous System.register calls can only be made by modules loaded by SystemJS.import and not via script tags. 

Hier sind die Shim und konfigurieren Datei

karma.conf.js

module.exports = function(config) { 
var client = './src/client/', 
    clientApp = client + 'app/'; 

config.set({ 
    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: './', 

    // frameworks to use 
    // some available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 

    plugins:[ 
     require('karma-jasmine'), 
     require('karma-phantomjs-launcher'), 
     require('karma-chrome-launcher'), 
     require('karma-htmlfile-reporter') 
    ], 

    // list of files/patterns to load in the browser 
    files: [ 
     'node_modules/systemjs/dist/system.src.js', 

     //Polyfills 
     'node_modules/es6-shim/es6-shim.min.js', 
     'node_modules/systemjs/dist/system-polyfills.js', 

     // Reflect and Zone.js dependencies 

     '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}, 

     //paths loaded by module loaders 
     {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}, 

     // transpiled application & spec code paths loaded via module imports 
     {pattern: clientApp + '**/**/*.js', included: true, watched: false}, 
     {pattern: clientApp + '**/**/*.spec.js', included: true, watched: true}, 

     // asset (HTML & CSS) paths loaded via Angular's component compiler 
     // (these paths need to be rewritten, see proxies section) 
     {pattern: clientApp + '**/**/*.html', included: false, watched: true}, 
     {pattern: clientApp + '**/**/*.css', included: false, watched: true}, 

     // paths for debugging with source maps in dev tools 
     {pattern: clientApp + '**/**/*.ts', included: false, watched: false}, 
     {pattern: clientApp + '**//***.js.map', included: false, watched: false} 
    ], 

    // list of files to exclude 
    exclude: [], 

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

    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: {}, 

    // test results reporter to use 
    // possible values: 'dots', 'progress', 'coverage' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress', 'html'], 

    htmlReporter: { 
     outputFile: 'reports/unitTests.html', 

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

    // web server port 
    port: 9876, 

    // enable/disable colors in the output (reporters and logs) 
    colors: true, 

    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || 
    // config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 

    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 

    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    //  browsers: ['Chrome', 'ChromeCanary', 'FirefoxAurora', 'Safari', 'PhantomJS'], 
    browsers: ['Chrome'], 

    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false 
}); 

};

und das Karma-Test-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 = '/src/client'; 
    return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath); 
} 

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

System.config({ 
    baseURL: ' ', 
    packageWithIndex: true // sadly, we can't use umd packages (yet?) 
}); 

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); 

Meine app Struktur wie so ist

./src 
    /client 
      /app/components 
       /directives 
       /etc... 
       main 

Bisher bin ich nur eine geistige Gesundheit Test ohne Importe versuchen zu laufen, wie so

Etwas, das ich bemerkt habe, ist, dass die Quelle auf Dev-Tools scheint nur das Typoskript der Spec-Dateien zu finden.

Antwort

1

Problem war, dass ich meine Datei SystemJs.config nicht korrekt konfiguriert hatte. Der Pfad für "app" wurde auf "src/client/app" geändert und Karma wird erfolgreich ausgeführt.

EDIT

Die Änderung muss wie so in Ihrer Map-Datei in systemjs.config gemacht werden:

var map = { 'app': 'src/client/app', // 'dist', 'rxjs': 'node_modules/rxjs', '@angular': 'node_modules/@angular',

Wir uns einen Build Ordner mit dem Namen dist in diesem Projekt mit.

+0

Wie führen Sie diese Änderung durch? Welche Datei wurde geändert? war die Datei 'systemjs.config.js'? – XtianGIS

+0

Siehe die überarbeitete Antwort –