如何使用karma-systemjs导入打字稿文件?

时间:2017-04-13 14:12:15

标签: angularjs typescript karma-runner systemjs

背景

我正在尝试为Angular 1x应用程序设置测试运行器,但我无法让Karma使用Systemjs和Typescript。

测试用Typescript编写,我们使用Systemjs导入使用ES6模块的文件。大多数文件不使用ES6模块。

我没有像karma-typescript一样使用业力预处理器,而是试图使用Systemjs来转换我的Typescript。这是错误的方式吗?似乎没有发生任何转变!

错误

当我运行karma start时,脚本在导入打字稿namspace时失败:

import core = myApp.core;

Chrome 52.0.2743 (Windows 7 0.0.0) ERROR
  Error: (SystemJS) SyntaxError: Unexpected token import
        Evaluating app/scripts/site/core/components/settingsMenu/settingsMenu.spec.ts
        Error loading app/scripts/site/core/components/settingsMenu/settingsMenu.spec.ts

这是我的karma.conf.js文件,基于this example

// Karma configuration
// Generated on Thurs Apr 13 2017

module.exports = function(config) {
    config.set({

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

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

        systemjs: {
            configFile: 'config.js',
            config: {
                paths: {
                    'systemjs': 'app/vendor/systemjs/system.src.js',
                    'system-polyfills': 'app/vendor/systemjs/system-polyfills.src.js',
                    'typescript': '/node_modules/typescript/lib/typescript.js'
                },
                packages: {
                    'app': {
                        defaultExtension: 'ts'
                    }
                },
                transpiler: 'typescript'
            },
            serveFiles: [
                'app/scripts/**/*!(spec).ts'
            ]
        },

        // list of files / patterns to load in the browser
        files: [
            { pattern: 'app/scripts/**/*.spec.ts' }
        ],


        // 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_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'],


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

systemjs config:

System.config({
    baseURL: './',
    defaultJSExtensions: true
});

1 个答案:

答案 0 :(得分:1)

尝试在systemjs config(config.js)中添加transpiler属性:

System.config({
   baseURL: './',
   defaultJSExtensions: true,
   transpiler: 'typescript'
});