2016-08-21 82 views
5

我正在使用Angular 2和TypeScript。我使用Angular CLI工具創建了這個項目,並嘗試使用命令「karma start」在內部運行測試用例 - 「app.component.spec.ts」。這個測試用例由Angular CLI本身生成,並且沒有變化。 當我運行它給了我下面的錯誤測試用例 -Angular CLI:使用karma測試執行時出現「意外的令牌導入」

C:\Projects\Angular2\TestProject>karma start 
21 08 2016 16:18:34.167:WARN [karma]: No captured browser, open http://localhost:9876/ 
21 08 2016 16:18:34.178:INFO [karma]: Karma v0.13.22 server started at http://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!\'', 
    inject([AppComponent], (app: AppComponent) => { 
     expect(app.title).toEqual('Test Project Title'); 
    })); 
}); 
+0

您正在使用哪個IDE? – TypeScripter

+1

Visual Studio代碼 – Raj

+2

無論哪個IDE或終端,我都面臨同樣的問題。第一個導入始終引發「未捕獲的SyntaxError:意外的令牌導入」。 – Guntram

回答

2

我有同樣的錯誤。我的問題是我的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', watched: false } 
], 
preprocessors: { 
    './web/spec/test.ts': ['angular-cli'] 
}, 

然後導入正確解釋和.ts文件可以通過編譯再次使用ng test