SystemJS and KarmaJS: TypeError: System.import is

2019-02-26 08:35发布

问题:

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

I keep getting the error below about System.import.

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.

Project structure

SystemJS configuration (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

Error

回答1:

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

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:

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

I see the problem. It's because you've got your bootstrapping code (eg. System.import() calls) inside your SystemJS config file - system.conf.js karma-systemjs expects just a simple System.config() call that it can then intercept to find out where your transpiler and polyfills are. It does this by evaluating your config file with a fake System object which simply records whatever is passed to System.config(). This fake object has no System.import() method, which causes your error.

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.