Angular 2.0.0使用保留字' import'在karma-test-shim.js - 包括JS输出中的Angular TypeScript文件?

时间:2016-09-21 14:56:06

标签: unit-testing angular webpack karma-runner karma-jasmine

升级到Angular 2.0.0后,我从PhantomJS收到此错误:

START:
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
    SyntaxError: Use of reserved word 'import'
    at karma-test-shim.js:4115
Chrome 53.0.2785 (Mac OS X 10.11.6) ERROR
    Uncaught SyntaxError: Unexpected token import
    at http://localhost:9876/context.html:8

当我在Chrome DevTools中的第4115行查看karma-test-shim.js时,我发现它正在尝试包含@ angular / core / testing的ts:

    eval("/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Injector, NgModule, NgZone, OpaqueToken } from '@angular/core';
import { AsyncTestCompleter } from './async_test_completer';
import { ComponentFixture } from './component_fixture';
import { ListWrapper } from './facade/collection';
import { FunctionWrapper, stringify } from './facade/lang';
import { TestingCompilerFactory } from './test_compiler';
var UNDEFINED = new Object();
/**
* An abstract class for inserting the root test component element in a platform independent way.
*
* @experimental
*/
export var TestComponentRenderer = (function () {
    function TestComponentRenderer() {
    }
    TestComponentRenderer.prototype.insertRootElement = function (rootElementId) { };
    return TestComponentRenderer;
}());
var _nextRootElementId = 0;
/**
* @experimental
*/
export var ComponentFixtureAutoDetect = new OpaqueToken('ComponentFixtureAutoDetect');
/**
* @experimental
*/
export var ComponentFixtureNoNgZone = new OpaqueToken('ComponentFixtureNoNgZone');
/**
* @experimental
*/
export var TestBed = (function () {
    ... 

我的webpack.test.config(此包只有测试)

process.env.npm_lifecycle_event = 'test';
var webpack = require('webpack'),
basicConf = {},
path = require('path');

var NODE_MODULES = root("node_modules");

// Helper functions
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}

basicConf.entry = {};

basicConf.devtool = "eval";
basicConf.debug = true;

basicConf.resolve = {
cache: true,
fallback: [ NODE_MODULES ],
modulesDirectories: [ NODE_MODULES ],
root: [ root("") ],
extensions: ["", ".ts", ".js", ".json", ".css", ".scss", ".html"]
};

basicConf.module = {
loaders: [
    {
    test: /\.ts$/,
    loader: "ts",
    query: {
        "ignoreDiagnostics": [
        2403, // 2403 -> Subsequent variable declarations
        2300, // 2300 -> Duplicate identifier
        2374, // 2374 -> Duplicate number index signature
        2375, // 2375 -> Duplicate string index signature
        2502  // 2502 -> Referenced directly or indirectly
        ]
    }
    },

    {test: /\.scss$/, loader: "null"},
    {test: /\.css$/, loader: "null"},
    {test: /\.html$/, loader: "raw"},
    {test: /\.json$/, loader: "json"}
],
postLoaders: [
    {
    test: /\.(js|ts)$/,
    include: path.resolve("."),
    loader: "istanbul-instrumenter-loader",
    exclude: [/.+-spec\.ts$/, /\.e2e\.ts$/, NODE_MODULES]
    }
]
};

module.exports = basicConf;

什么会导致webpack / karma在karma-test-shim.js输出中包含ts文件?此代码可以由另一个项目利用并正确执行。我似乎无法进行测试。

我的实际karam.test.shim.js:

Error.stackTraceLimit = Infinity;

require('core-js/es6');
require('core-js/es7/reflect');
require('ts-helpers');
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/proxy');
require('zone.js/dist/sync-test');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('rxjs/Rx');
require("material-design-lite/dist/material.min.js");

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.TestBed.initTestEnvironment(
    browser.BrowserDynamicTestingModule,
    browser.platformBrowserDynamicTesting()
);

var testContext = require.context('.', true, /.+-spec\.ts/);
function requireAll(requireContext) {
    return requireContext.keys().map(requireContext);
}
var modules = requireAll(testContext);

1 个答案:

答案 0 :(得分:0)

简单地说,必须迁移到webpack 2.0.0-beta.25