SystemJS和KarmaJS:TypeError:System.import不是函数

时间:2016-02-25 19:43:44

标签: karma-runner systemjs

我正在努力让我的项目与Karma和SystemJS合作。我正在使用带有业力的karma-systemjs插件。

我一直收到有关System.import的错误。

相信这是因为在karma-systemjs插件运行时没有加载SystemJS。请告诉我我做错了什么。

项目结构

enter image description here

SystemJS配置(system.conf.js)

System.config({
    baseUrl: '',
    defaultJSExtensions: true,
    map: {
        'jquery': 'vendor/kendo/js/jquery.min.js',
        'angular': 'vendor/kendo/js/angular.js',
        'kendo': 'vendor/kendo/js/kendo.all.min.js',
        'angular-mocks': 'vendor/bower_components/angular-mocks/angular-mocks.js',
        'angular-ui-router': 'vendor/bower_components/angular-ui-router/release/angular-ui-router.min.js',
        'angular-toastr': 'vendor/bower_components/angular-toastr/dist/angular-toastr.tpls.min.js',
        'angular-local-storage': 'vendor/bower_components/angular-local-storage/dist/angular-local-storage.min.js'
    },
    paths: {
        'systemjs': 'vendor/bower_components/system.js/dist/system.js',
        'system-polyfills': 'vendor/bower_components/system.js/dist/system-polyfills.js'  
    },
    meta: {
        'vendor/kendo/js/jquery.min.js': {
            format: 'global',
            exports: '$'
        },
        'vendor/kendo/js/angular.js': {
            format: 'global',
            deps: [
                'vendor/kendo/js/jquery.min.js'
            ],
            exports: 'angular'
        },
        'vendor/kendo/js/kendo.all.min.js': {
            format: 'global',
            deps: [
                'vendor/kendo/js/angular.js'
            ],
            exports: 'kendo'
        },
        'vendor/bower_components/angular-ui-router/release/angular-ui-router.min.js': {
            format: 'global',
            deps: [
                'vendor/kendo/js/angular.js'
            ]
        },
        'vendor/bower_components/angular-mocks/angular-mocks.js': {
            format: 'global',
            deps: [
                'vendor/kendo/js/angular.js'
            ],
            exports: 'angular.mock'
        },
        'vendor/bower_components/angular-toastr/dist/angular-toastr.tpls.min.js': {
            format: 'global',
            deps: [
                'vendor/kendo/js/angular.js'
            ]
        },
        'vendor/bower_components/angular-local-storage/dist/angular-local-storage.min': {
            format: 'global',
            deps: [
                'vendor/kendo/js/angular.js'
            ]
        }
    }
});
Promise.all([
    System.import('kendo'),
    System.import('angular-mocks'),
    System.import('angular-ui-router'),
    System.import('angular-toastr'),
    System.import('angular-local-storage')
]).then(function () {
    System.import('angular')
        .then(function (angular) {
        System.import('ng/app/app.module')
            .then(function (app) {
            angular.bootstrap(document, ['s9.app']);
        }, function (err) {
            console.log(err);
        });
    }, function (err) {
        console.log(err);
    });
});

//# sourceMappingURL=system.conf.js.map

karma.conf.js

// Karma configuration
var configure = function (config) {
    config.set({
        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '.',
        transpiler: null,
        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['systemjs', 'jasmine'],
        systemjs: {
            // Path to your SystemJS configuration file
            configFile: 'system.conf.js',
            // Patterns for files that you want Karma to make available, but not loaded until a module
            //     requests them. eg. Third-party libraries.
            serveFiles: [
                'vendor/kendo/js/**/*.js',
                'vendor/bower_components/**/*.js',
                'ng/**/*.js',
                'test/**/*Spec.js'
            ],
            config: {
                paths: {
                    'systemjs': 'vendor/bower_components/system.js/dist/system.js',
                    'system-polyfills': 'vendor/bower_components/system.js/dist/system-polyfills.js'
                }
            }
        },
        // list of files / patterns to load in the browser
        files: [
            {pattern: 'vendor/kendo/js/**/*.js', included: false},
            {pattern: 'vendor/bower_components/**/*.js', included: false},
            {pattern: 'ng/**/*.js', included: false},
            {pattern: 'test/**/*Spec.js', included: false}
        ],
        // list of files to exclude
        exclude: [],
        // 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'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress'],
        // 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_DEBUG,
        // 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: ['PhantomJS'],
        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false,
        // Concurrency level
        // how many browser should be started simultaneous
        concurrency: Infinity
    });
};
module.exports = configure;

//# sourceMappingURL=karma.conf.js.map

错误

enter image description here

1 个答案:

答案 0 :(得分:2)

我通过从配置代码中移出引导代码来修复此问题。显然当使用karma-systemjs插件时,System.import在调用它时仍然不可用(尽管它在正常运行时)。

我做了什么:我将引导代码(即Promise.all([....)移动到一个名为bootstrap.js的单独文件中(名称并不重要)然后在我的index.html我按顺序添加了它们:

enter image description here

也可以从这个链接(karma系统js插件作者说):https://github.com/rolaveric/karma-systemjs/issues/71

  

我看到了问题。这是因为你有自己的引导代码   (例如System.import()调用)在SystemJS配置文件中 -   system.conf.js karma-systemjs只需要一个简单的System.config()   打电话说它可以拦截找出你的发布者和   polyfills是。它通过使用假的评估您的配置文件来做到这一点   简单记录传递给的内容的系统对象   System.config()。这个假对象没有System.import()方法   导致你的错误。

     

我建议将你的引导代码移动到一个单独的文件中(我   亲自将它放在我的HTML脚本标签中。否则你会跑   如果您尝试使用systemjs-builder,那么就会遇到类似的问题   可能不希望在开始时调用angular.bootstrap()   你的单元测试。