本文介绍了SystemJS和KarmaJS:TypeError:System.import不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的项目与Karma和SystemJS一起使用.我正在将karma-systemjs插件与karma一起使用.

I am trying to get my project working with Karma and SystemJS. I am using the karma-systemjs plugin with karma.

我不断收到有关System.import的错误.

I keep getting the error below about System.import.

相信这是因为在运行karma-systemjs插件时尚未加载SystemJS.请告诉我我在做什么错.

I believe it is because SystemJS is not being loaded by the time the karma-systemjs plugin runs. Please tell me what I am doing wrong.

项目结构

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

错误

推荐答案

我通过将引导程序代码移出配置代码来解决此问题.显然,在使用karma-systemjs插件时,调用它时System.import尚不可用(尽管它在正常运行时).

I fixed this by moving the bootstrap code out of the config code. Apparently when using the karma-systemjs plugin System.import is not available yet when this is called (although it is at normal runtime).

我的工作:我将引导程序代码(即Promise.all([....))移动到了一个名为bootstrap.js的单独文件中(名称不重要),然后将其index.html我按以下顺序添加了它们:

What I did: I moved the bootstrap code (i.e. Promise.all([....) into into a separate file called bootstrap.js (name is not important) and then in my index.html I added them in this order:

也通过此链接(因果系统js插件作者说): https://github.com/rolaveric/karma-systemjs/issues/71

Also from this link (the karma system js plugin author says): https://github.com/rolaveric/karma-systemjs/issues/71

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

I'd recommend moving your bootstrapping code into a separate file (I personally put it in a script tag with my HTML). Otherwise you'll run into similar problems if you try to use systemjs-builder, and you probably don't want angular.bootstrap() to be called at the start of your unit tests.

这篇关于SystemJS和KarmaJS:TypeError:System.import不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 19:32