angular 2 - TestBed.configureTestingModule - providers:[] throws使用保留字' import'错误

时间:2016-09-14 16:12:37

标签: unit-testing testing angular typescript

我刚刚组建了一个新的快速入门项目,名为angular webpack quickstart(angular 2 webpack quickstart

我已经添加了我的第一个test.spec.ts,就像这样

describe('first test', ()=>{
  beforeEach(()=>{
    TestBed.configureTestingModule({
      providers:[MockBackend]
    })
  )
}

它抛出了我

  

SyntaxError:保留字的用户' import'

如果我删除了MockBackend,它可以正常工作。

  

EDIT ------------------------

     

我发现如果我提供例如MockBackend到了   app.component.spec.ts(也来自快速启动),everthing工作正常。

     

所以唯一的区别是我的测试文件的位置是

app
--app.component.spec
--sdk (folder)
----tests (folder)
------mytest.spec

这是我的karma.conf,webpack.test和webpack.common(它们基本上是快速入门指南中的默认值

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': ['webpack', 'sourcemap']
    },

    webpack: webpackConfig,

    webpackMiddleware: {
      stats: 'errors-only'
    },

    webpackServer: {
      noInfo: true
    },

    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['PhantomJS'],
    singleRun: true
  };

  config.set(_config);
};

Webpack.test

var helpers = require('./helpers');

module.exports = {
  devtool: 'inline-source-map',

  resolve: {
    extensions: ['', '.ts', '.js']
  },

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loaders: ['ts', 'angular2-template-loader']
      },
      {
        test: /\.html$/,
        loader: 'html'

      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'null'
      },
      {
        test: /\.css$/,
        exclude: helpers.root('src', 'app'),
        loader: 'null'
      },
      {
        test: /\.css$/,
        include: helpers.root('src', 'app'),
        loader: 'raw'
      }
    ]
  }
}

Webpack.common

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
  entry: {
    'polyfills': './src/polyfills.ts',
    'vendor': './src/vendor.ts',
    'app': './src/main.ts'
  },

  resolve: {
    extensions: ['', '.js', '.ts']
  },

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loaders: ['ts', 'angular2-template-loader']
      },
      {
        test: /\.html$/,
        loader: 'html'
      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'file?name=assets/[name].[hash].[ext]'
      },
      {
        test: /\.css$/,
        exclude: helpers.root('src', 'app'),
        loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
      },
      {
        test: /\.css$/,
        include: helpers.root('src', 'app'),
        loader: 'raw'
      }
    ]
  },

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: ['app', 'vendor', 'polyfills']
    }),

    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
};

1 个答案:

答案 0 :(得分:0)

解决了....我正在使用的测试文件是从R​​C4规范粘贴的派对副本,并且有一个我认为已被弃用的导入...