Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 9, Jasmine: "Error: unreachable" when doing unit test of a service

My problem is similar to Jasmine Angular 9 test failing because 'unreachable' stack trace at injectableDefOrInjectorDefFactory, but the solution there did not work for me.

Here is my .spec.ts file:

import { TestBed } from '@angular/core/testing';

import { DataService } from './data.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { DataServiceMock, FirestoreStub } from './mock.data.service';

describe('DataService', () => {

  let service: DataService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: DataService, useClass: DataServiceMock },
        { provide: AngularFirestore, useClass: FirestoreStub },
      ]
    }).compileComponents();
    service = TestBed.get(DataService);
    service.loadData();
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should get have 2 products in its data', () => {
    expect(service.getData()).toEqual([
      {
        name: 'Camomile',
        description: 'Camomile oil',
        uses: ['topical', 'ingest'],
        benefits: ['headache', 'anxiety'],
      },
      {
        name: 'Cardamom',
        description: 'oil from the cardamom seed',
        uses: ['surface cleaning'],
        benefits: ['joint pain', 'hygiene'],
      }
    ]);
  });
});

For the "DataService > should be created", I get

Error: unreachable
    at injectableDefOrInjectorDefFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:17298:1)
    at providerToFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:17398:1)

Any help with figuring this out would be appreciated. If you need to see other code, let me know. Thanks much!

like image 956
VictorNorman Avatar asked Oct 26 '25 06:10

VictorNorman


1 Answers

mistake: {provide: FeaturesToggleService, useClass: {}},

correct: {provide: FeaturesToggleService, useValue: {}}

like image 192
Baidiuk Yevhen Avatar answered Oct 29 '25 09:10

Baidiuk Yevhen