Angular 2 - Karma错误 - 模块解析失败'您可能需要一个合适的加载程序来处理此文件类型'

时间:2016-11-11 09:22:28

标签: testing angular typescript karma-jasmine

我正在为我的角度2应用程序设置Karma。我收到错误的插件错误..你能帮忙吗? 我正在使用https://github.com/AngularClass/angular2-webpack-starter 以供参考。 这是我对业力的依赖性。

"istanbul": "^0.4.5",
    "jasmine-core": "^2.4.1",
    "json-loader": "^0.5.4",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-coverage": "^1.1.1",
    "karma-istanbul-reporter": "0.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-mocha-reporter": "^2.2.0",
    "karma-remap-coverage": "^0.1.2",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^1.8.0",
    "lodash-webpack-plugin": "^0.10.2",
    "npm-scripts-info": "^0.3.6",
    "raw-loader": "0.5.1",
    "rimraf": "^2.5.2",
    "source-map": "^0.5.6",
    "source-map-loader": "^0.1.5",

这是我的karma.conf.js

var webpackConfig = require('./webpack.test');

module.exports = function (config) {
  var _config = {
    basePath: '',

    frameworks: ['jasmine'],

    files: [
      {pattern: './config/karma-test-shim.js', watched: false}
    ],

    preprocessors: {
      './config/karma-test-shim.js': ['coverage','webpack', 'sourcemap'],
      './src/**/!(*.spec).(ts|js)': ['sourcemap']
    },

    webpack: webpackConfig,
    coverageReporter: {
      type: 'in-memory'
    },

    remapCoverageReporter: {
      'text-summary': null,
      json: './coverage/coverage.json',
      html: './coverage/html'
    },
    webpackMiddleware: {
      stats: 'errors-only'
    },

    webpackServer: {
      noInfo: true
    },

    //reporters: ['progress'],
     /*
     * test results reporter to use
     *
     * possible values: 'dots', 'progress'
     * available reporters: https://npmjs.org/browse/keyword/karma-reporter
     */
    reporters: [ 'mocha', 'coverage', 'remap-coverage' ],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['Chrome'],
    singleRun: true
  };

  config.set(_config);
};

我在运行时收到此错误:

Chrome 54.0.2840 (Windows 7 0.0.0) ERROR
  Uncaught Error: Module parse failed: {location}\src\app\app.component.spec.ts Unexpected t
oken (13:57)
  You may need an appropriate loader to handle this file type.
  |     ]}));
  |
  |   it('should have a state', inject([ AppComponent ], (app: AppComponent) => {
  |     expect(app.state).toEqual('blank');
  |   }));

在config / karma-test-shim.js:69437

1 个答案:

答案 0 :(得分:0)

我认为这类似于question

您需要在webpack.test.js(webpack配置文件)中添加以下更改:

尝试像这样更新加载器“ awesome-typescript-loader”:

{
   loader: 'awesome-typescript-loader?presets[]=es2017',
   options: { configFileName: helpers.root('src', 'tsconfig.json') }
}