0

I am writing a basic test to test the initialization of the component. Here it goes,

import { NO_ERRORS_SCHEMA } from '@angular/core';
import {
  inject,
  async,
  TestBed,
  ComponentFixture,
} from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { AppInsightsModule, AppInsightsService } from 'ng2-appinsights';
import { FredAuthAdalModule, AuthService } from '@MyPackage';

/**
 * Load the implementations that should be tested
 */
import { AppComponent } from './app.component';
import { AppState } from './app.service';

class MockAuthService extends AuthService {
}

describe(`App`, () => {
  let comp: AppComponent;
  let fixture: ComponentFixture<AppComponent>;

  /**
   * async beforeEach
   */
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule, AppInsightsModule, FredAuthAdalModule],
      declarations: [AppComponent],
      schemas: [NO_ERRORS_SCHEMA],
      providers: [AppState, AppInsightsService, { provide: AuthService, useClass: MockAuthService }]
    })
      /**
       * Compile template and css
       */
      .compileComponents();
  }));

  /**
   * Synchronous beforeEach
   */
  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    comp = fixture.componentInstance;

    /**
     * Trigger initial data binding
     */
    fixture.detectChanges();
  });

  it(`should be initialized`, () => {
    expect(fixture).toBeDefined();
    expect(comp).toBeDefined();
  });

});

I have mocked the AuthService because it has multi-level dependencies. Providing the AuthService to the test bed results in an error "Provider for AuthInternalDependency not found".

[at-loader] Ok, 0.118 sec. 07 08 2017 15:33:56.115:WARN [web-server]: 404: /env.json Chrome 60.0.3112 (Windows 10 0.0.0) ERROR Uncaught SyntaxError: Unexpected token N in JSON at position 0 at webpack:///node_modules/zone.js/dist/zone.js:195:0 <- config/spec-bundle.js:78354 Chrome 60.0.3112 (Windows 10 0.0.0) ERROR Uncaught SyntaxError: Unexpected token N in JSON at position 0 at webpack:///node_modules/zone.js/dist/zone.js:195:0 <- config/spec-bundle.js:78354

I tried importing the complete MypackageModule into the test bed but I get the same error. Any ideas on what it could be?

0mpurdy
  • 3,198
  • 1
  • 19
  • 28
lohiarahul
  • 1,432
  • 3
  • 22
  • 35

1 Answers1

0

Can you please try by removing expect(fixture).toBeDefined();

Developer
  • 279
  • 6
  • 22