salemdar / angular2-cookie

Implementation of Angular 1.x $cookies service to Angular 2
MIT License
109 stars 29 forks source link

CookieService not found when used with TestBed #28

Closed andreivirtosu closed 7 years ago

andreivirtosu commented 7 years ago

Not sure what I'm doing wrong, but this test fails to compile with the angular2/systemjs/karma setup:

class mockCookieService {
}
describe('AppComponent with TCB', function () {
  beforeEach(() => {
    TestBed.configureTestingModule(
      {
        declarations: [AppComponent],
        providers: [
          { provide: CookieService, useClass: mockCookieService }
        ]
      });
  });

The error I get: 26 10 2016 11:35:57.473:WARN [web-server]: 404: /base/node_modules/angular2-cookie/services/cookies.service.js

The setup was based on angular2 quick-start project: https://github.com/angular/quickstart

andreivirtosu commented 7 years ago

The issue was with karma configuration. So what's needed is: Add to karma.conf.js:

files: [
      // angular2-cookie
      { pattern: 'node_modules/angular2-cookie/**/*.js', included: false, watched: false },
      { pattern: 'node_modules/angular2-cookie/**/*.js.map', included: false, watched: false }
]

Add to karma-test-shim.js:

System.config({
     packages: {
        'angular2-cookie': {
                  main: './core.js',
                  defaultExtension: 'js'
         }
     },
     map: {
             'angular2-cookie': 'npm:angular2-cookie'
     }
});