Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular CLI:使用业力测试执行时的“意外的令牌导入”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用带有TypeScript的Angular 2.我使用Angular CLI工具创建了这个项目,并尝试使用命令 – “karma start”运行里面的测试用例 – “app.component.spec.ts”.此测试用例由Angular CLI本身生成,并且没有变化.
当我运行测试用例时,它会给我以错误

C:\Projects\Angular2\TestProject>karma start
21 08 2016 16:18:34.167:WARN [karma]: No captured browser,open @R_674_10107@://localhost:9876/
21 08 2016 16:18:34.178:INFO [karma]: Karma v0.13.22 server started at @R_674_10107@://localhost:9876/
21 08 2016 16:18:34.205:INFO [launcher]: StarTing browser Chrome
21 08 2016 16:18:36.677:INFO [Chrome 52.0.2743 (Windows 7 0.0.0)]: Connected on socket /#u3qiejjLaOGa5HdGAAAA with id 54026183
Chrome 52.0.2743 (Windows 7 0.0.0) ERROR
  Uncaught SyntaxError: Unexpected token import
  at C:/Projects/Angular2/TestProject/src/app/app.component.spec.ts:5

请问某人如何解决此问题.

app.component.spec.ts –

/* tslint:disable:no-unused-variable */
/// <reference path="../../typings/globals/jasmine/index.d.ts" />

import { addProviders,async,inject } from '@angular/core/tesTing';
import { AppComponent } from './app.component';

describe('App: TestProject',() => {
  beforeEach(() => {
    addProviders([AppComponent]);
  });

  it('should create the app',inject([AppComponent],(app: AppComponent) => {
      expect(app).toBeTruthy(); 
    }));

  it('should have as title \'app works!\'',(app: AppComponent) => {
      expect(app.titlE).toEqual('Test Project title');
    }));
});

解决方法

我有同样的错误.我的问题是我的spec目录中的所有.ts文件都没有被angular-cli正确检测到.我的karma.conf.js中有一些配置问题.我改变了以下内容

files: [
  { pattern: './web/spec/test.ts',watched: false }
],preprocessors: {
  './invalid-path/to/test.ts': ['angular-cli']
},

files: [
  { pattern: './web/spec/test.ts',preprocessors: {
  './web/spec/test.ts': ['angular-cli']
},

之后导入被正确解释,.ts文件可以使用Ng测试再次编译而不会出错.

大佬总结

以上是大佬教程为你收集整理的Angular CLI:使用业力测试执行时的“意外的令牌导入”全部内容,希望文章能够帮你解决Angular CLI:使用业力测试执行时的“意外的令牌导入”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。