0

I am trying to write a test case of a service which depends on httpclient and Router.

I tried Karma + Jasmine: Cannot read property 'getComponentFromError'. Still getting the same error. Maybe some other issue. I need help here

When I run that test case I face an error saying "Failed: Cannot read property 'getComponentFromError' of null"

I already tried some solution like

import {TestBed} from '@angular/core/testing';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {NavigationEnd, Router} from '@angular/router';

import {AddaService} from './adda.service';
import {RequestUrl} from './request.url';
import {countryList} from './countries';

describe('AddaService', () => {
    let addaService: AddaService;
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [RequestUrl, countryList, HttpClient, HttpHeaders, HttpParams, NavigationEnd, Router],
            providers: [AddaService]
        });
        addaService = TestBed.get(AddaService);
    });

    it('should be createable', () => {
        expect(addaService).toBeTruthy();
    });

});

beforeEach(() => TestBed.configureTestingModule({
        providers: [AddaService]
    }));

    it('should be created', inject([HttpTestingController, AddaService],
        (httpMock: HttpTestingController, service: AddaService) => {
            // We call the service
            service.getcountries();
            expect(service.countries).toBeTruthy();
        })
    );

expect result should have been a success. But I am below error.


Failed: Cannot read property 'getComponentFromError' of null
TypeError: Cannot read property 'getComponentFromError' of null
    at TestBedViewEngine._initIfNeeded (/Users/manoj_adda/packages/core/testing/src/test_bed.ts:393:46)
    at TestBedViewEngine.get (/Users/manoj_adda/packages/core/testing/src/test_bed.ts:473:10)
    at Function.TestBedViewEngine.get (/Users/manoj_adda/packages/core/testing/src/test_bed.ts:243:36)
    at UserContext.<anonymous> (/Users/manoj_adda/Sites/addaio/src/app/adda.service.spec.ts:16:25)
    at /Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:112:25
    at new ManagedPromise (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:1067:7)
    at ControlFlow.promise (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2396:12)
    at schedulerExecute (/Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:95:18)
    at TaskQueue.execute_ (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2970:14)
    at TaskQueue.executeNext_ (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2953:27)
From: Task: Run beforeEach in control flow
    at UserContext.<anonymous> (/Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:94:19)
    at attempt (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4486:26)
    at QueueRunner.run (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4402:20)
    at runNext (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4446:20)
    at /Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4453:13
    at /Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4356:12
    at /Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:64:48
    at ControlFlow.emit (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/events.js:62:21)
    at ControlFlow.shutdown_ (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2565:10)
    at shutdownTask_.MicroTask (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2490:53)
From asynchronous test: 
Error
    at Suite.<anonymous> (/Users/manoj_adda/Sites/addaio/src/app/adda.service.spec.ts:11:2)
    at addSpecsToSuite (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1181:25)
    at Env.describe (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1145:7)
    at describe (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4593:18)
    at Object.<anonymous> (/Users/manoj_adda/Sites/addaio/src/app/adda.service.spec.ts:9:1)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module.m._compile (/Users/manoj_adda/Sites/addaio/node_modules/ts-node/src/index.ts:422:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/manoj_adda/Sites/addaio/node_modules/ts-node/src/index.ts:425:12)
Biswajit Patra
  • 101
  • 1
  • 2
  • 11
  • Possible duplicate of [Karma + Jasmine: Cannot read property 'getComponentFromError'](https://stackoverflow.com/questions/49068013/karma-jasmine-cannot-read-property-getcomponentfromerror) – paulsm4 Jun 05 '19 at 16:16
  • @paulsm4 I tried that solution still getting the same error. – Biswajit Patra Jun 05 '19 at 16:28
  • 3
    `imports` is supposed to be an array of angular **modules** to import, not an array of services. If your service depends on Router and HttpClient, you should only have HttpClientTestingModule and RouterTestingModule in the imports. – JB Nizet Jun 05 '19 at 16:33
  • @JBNizet Same result with that too. And other imports are not services exactly service. They are not even required to import. I was just trying to include the dependencies. Those files contain some constant values that's all. About HttpClientTestingModule and RouterTestingModule tried already got the same error. – Biswajit Patra Jun 05 '19 at 16:40
  • 1
    When in doubt ... DELETE everything in "modules", and reload from scratch :) – paulsm4 Jun 05 '19 at 17:43
  • why even add things that are not modules into imports halve of this things should be in providers just start from HttpClientTestingModule and RouterTestingModule else this question is wast of time – Xesenix Jun 19 '19 at 18:39
  • @Xesenix Thanks for the reply. Yes, I figured that out. My only issue even without those imports is not working. – Biswajit Patra Jun 21 '19 at 09:07
  • Were you able to figure this one out? What was your solution? – nmrichards Nov 25 '20 at 13:16
  • @nmrichards No, I am not unable to, so I stashed this. – Biswajit Patra Nov 26 '20 at 04:58

0 Answers0