Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI: "Unexpected token import" while test execution using karma

I am using Angular 2 with TypeScript. I created this project using Angular CLI tool, and trying to run the testcases inside - "app.component.spec.ts" using the command - "karma start". This testcase is generated by Angular CLI itself, and there is not change in it. When I run the testcase it gives me following error -

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

Could someone please help how to resolve this issue.

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');
    }));
});
like image 338
Raj Avatar asked Nov 20 '22 21:11

Raj


1 Answers

I had the same error. My issue was that all .ts files in my spec directory wasnt detected by angular-cli correctly. I had some configuration issues in my karma.conf.js. I changed the following:

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

to

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

Afterwards the import was interpreted correctly and the .ts files could compile without errors again using ng test.

like image 63
Tim Avatar answered Nov 22 '22 11:11

Tim