使用Karma与SystemJS,gulp和TypeScript时出错

时间:2016-02-11 14:06:05

标签: typescript karma-runner systemjs gulp-karma karma-qunit

我是node.js堆栈的新用户,如npmgulp等。我之前编写过一些JavaScript单元测试用例,现在想用Karma作为测试运行器。然而,经过几次尝试后,我现在完全迷失了。

首先,我有以下项目配置:

  • SystemJS作为模块加载器。
  • 文件夹结构(为简洁起见,省略了一些):

    project
    |
    |--build //contains all gulp tasks
    |
    |--dist //contains the output (*.html, *.js etc.)
    |
    |--jspm_packages 
    |
    |--node_modules
    |
    |--src //contains the source .html, and *.ts
    |
    |--tests //contains unit tests in *.ts. I also have a gulp task to build tests/**/*.ts to *.js in the same folder.
    |
    |--typings //contains type definitions
    |
    |--config.js //contains SystemJS configuration
    |
    |--karma.conf.js
    
  • karma.conf.js

    // Karma configuration        
    
    module.exports = function (config) {
        config.set({
            basePath: '',
            frameworks: [ 'qunit'],
            files: [
                'jspm_packages/system.js',
                //'config.js',
                'dist/custom/*.js' //,
                //'tests/**/*.js'
            ],
            exclude: [        ],
            preprocessors: {        },
            reporters: ['progress'],
            port: 9876,
            colors: true,
            logLevel: config.LOG_INFO,
            autoWatch: true,
            browsers: ['Chrome'],
            singleRun: false,
            concurrency: Infinity
        });
    }
    

但是,每当我karma start时,我都会收到错误Uncaught TypeError: Unexpected anonymous System.register call.。无论如何要解决这个问题吗?

其他尝试:

  1. 根据https://github.com/karma-runner/gulp-karma,尝试创建一个gulp任务。但是有同样的问题。
  2. 试图使用karma-systemjskarma.conf.js的不同之处如下:

    // Karma configuration        
    
    module.exports = function (config) {
        config.set({
            ...
            frameworks: [ 'systemjs', 'qunit'],
            plugins: ['karma-systemjs', 'karma-chrome-launcher', 'karma-firefox-launcher'],
            systemjs: {
                configFile: 'config.js',
                serveFiles: ['jspm_packages/**/*.js'],
                config: {
                    transpiler: 'babel'
                }
            },
            ...
        });
    }
    

    在这种情况下,我遇到了以下错误,但我安装了SystemJsBabel

    [WARNING] Looking up paths with require.resolve() is deprecated.
    Please add "systemjs" to your SystemJS config paths.
    Cannot find "systemjs".
      Did you forget to install it ?
      npm install systemjs --save-dev
    [WARNING] Looking up paths with require.resolve() is deprecated.
    Please add "babel" to your SystemJS config paths.
    Cannot find "babel-core".
      Did you forget to install it ?
      npm install babel-core --save-dev
    
  3. 这方面的任何帮助都会很棒。

0 个答案:

没有答案