Greetings I am trying to do a unit test with jasmine + karma , in angular 7, but always get this error :

Failed: Illegal state: Could not load the summary for directive DataStorageService.

I know it has to do with the imports or the declarations but can't see wha tis wrong

My spec file :

import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, Component, NgModule } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { IonicModule } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
import { DataStorageService } from './post-data-service';
import { AppRoutingModule } from '../app-routing.module';
import { CommonModule } from '@angular/common';
import { AppComponent } from '../app.component';

describe('HomePage', () => {
  let component: DataStorageService;
  let fixture: ComponentFixture<DataStorageService>;
  let service: DataStorageService;

  beforeEach(async(() => {
    fixture = TestBed.createComponent(DataStorageService);
    component = fixture.debugElement.componentInstance;
    TestBed.configureTestingModule({
      declarations: [DataStorageService],
      schemas: [NO_ERRORS_SCHEMA],
      providers: [BarcodeScanner, DataStorageService],
      imports: [
        TranslateModule.forRoot(),
        IonicModule,
        FormsModule,
        RouterModule,
        AppRoutingModule,
        HttpClientModule,
        CommonModule]
    })
      .compileComponents();
  }));
  afterEach(() => {
    service = null;
    fixture = null;
    component = null;
  });
  it('should create', () => {
    expect(component).toBeDefined();
  });
});

my service

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

//endpoint
const endpoindURL = 'https://example.com/backend';

@Injectable({
  providedIn: 'root'
})

export class DataStorageService {
  result: any;
  constructor(private http: HttpClient) {
  }

  sendCodeToServer(data) {
    const headers = new HttpHeaders().set('Content-Type', 'application/json');
    return new Promise((resolve, reject) => {
      this.http.post(endpoindURL, data, { headers: headers })
        .toPromise().then((data) => {
          resolve(data);
        }).catch((err) => {
          console.log(err);
          reject(err);
        });
    });
  }
}

0 Answers