TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at <Jasmine>

Viewed 1240

Hi I ahve added this piece of code in ngonit, for polling and after adding this all my test cases are getting failed. Please help how to resolve?

  ngOnInit(): void {
this.timeInterval = interval(5000)
.pipe(
  startWith(0),
  switchMap(() => this.deviceService.getDeviceList())
)
.pipe(
  takeUntil(this.deviceService.flag$),
  repeatWhen(() => this.deviceService.flag$)
)
.subscribe((success: any) => {
  this.gridApi?.setRowData(this.updatedData);
  console.log("hello")
  this.updatedData = success;
}, retry(2));

This is the complete error

HeadlessChrome 88.0.4324 (Windows 10.0.0) DeviceComponent testing columnresize of the grid for other scenario FAILED
    TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
        at <Jasmine>
        at subscribeTo (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/util/subscribeTo.js:27:1)
        at innerSubscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/innerSubscribe.js:69:23)
        at TakeUntilOperator.call (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/operators/takeUntil.js:11:52)
        at Observable.subscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Observable.js:23:1)
        at RepeatWhenOperator.call (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/operators/repeatWhen.js:11:1)
        at Observable.subscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Observable.js:23:1)
        at DeviceComponent.ngOnInit (http://localhost:9876/_karma_webpack_/src/app/device/device.component.ts:99:4)
        at callHook (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:3937:1)
        at callHooks (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:3901:1)
        at executeInitAndCheckHooks (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:3842:1)
    Failed: Cannot read property 'unsubscribe' of undefined
        at <Jasmine>
        at DeviceComponent.ngOnDestroy (http://localhost:9876/_karma_webpack_/main.js:28373:23)
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/device/device.component.spec.ts:196:15)
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)
        at AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:763:1)
        at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:302:1)
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
        at Zone.runGuarded (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:134:1)
        at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:885:1)
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:823:1)
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)

HeadlessChrome 88.0.4324 (Windows 10.0.0): Executed 1 of 385 (1 FAILED) (0 secs / 1.732 secs) HeadlessChrome 88.0.4324 (Windows 10.0.0) DeviceComponent testing columnresize of the grid for other scenario FAILED TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at at subscribeTo (http://localhost:9876/karma_webpack/node_modules/rxjs/_esm2015/internal/util/subscribeTo.js:27:1) at innerSubscribe (http://localhost:9876/karma_webpack/node_modules/rxjs/_esm2015/internal/innerSubscribe.js:69:23) at TakeUntilOperator.call (http://localhost:9876/karma_webpack/node_modules/rxjs/_esm2015/internal/operators/takeUntil.js:11:52) at Observable.subscribe (http://localhost:9876/karma_webpack/node_modules/rxjs/_esm2015/internal/Observable.js:23:1) at RepeatWhenOperator.call (http://localhost:9876/karma_webpack/node_modules/rxjs/_esm2015/internal/operators/repeatWhen.js:11:1) at Observable.subscribe (http://localhost:9876/karma_webpack/node_modules/rxjs/_esm2015/internal/Observable.js:23:1) at DeviceComponent.ngOnInit (http://localhost:9876/karma_webpack/src/app/device/device.component.ts:99:4) at callHook (http://localhost:9876/karma_webpack/node_modules/@angular/core/ivy_ngcc/fesm2015/core.js:3937:1) at callHooks (http://localhost:9876/karma_webpack/node_modules/@angular/core/ivy_ngcc/fesm2015/core.js:3901:1) at executeInitAndCheckHooks (http://localhost:9876/karma_webpack/node_modules/@angular/core/ivy_ngcc/fesm2015/core.js:3842:1) Failed: Cannot read property 'unsubscribe' of undefined at at DeviceComponent.ngOnDestroy (http://localhost:9876/karma_webpack/main.js:28373:23) at UserContext. (http://localhost:9876/karma_webpack/src/app/device/device.component.spec.ts:196:15) at ZoneDelegate.invoke (http://localhost:9876/karma_webpack/node_modules/zone.js/dist/zone-evergreen.js:365:1)

3 Answers

Try this to debug it:

  ngOnInit(): void {
  console.log('this.deviceService.getDeviceList()', this.deviceService.getDeviceList());
  console.log('this.deviceService.flag$', this.device.getDeviceList());

  // make sure the above logs are not defined. 
 // If they are, you're not mocking them correctly when `ngOnInit` is called. 
 // `ngOnInit` is called after the first `fixture.detectChanges()` after 
 // the `TestBed.configureTestingModule({}).compileComponents();`.

this.timeInterval = interval(5000)
.pipe(
  startWith(0),
  switchMap(() => this.deviceService.getDeviceList())
)
.pipe(
  takeUntil(this.deviceService.flag$),
  repeatWhen(() => this.deviceService.flag$)
)
.subscribe((success: any) => {
  this.gridApi?.setRowData(this.updatedData);
  console.log("hello")
  this.updatedData = success;
}, retry(2));
  1. First disable all of your tests with xdescribe

  2. enable one by one until you find problematic unittest It should be the one that uses providers that mock methods and return observable instead of value, for example

    { provide: TranslateService, useValue: { get: () => of(''), instant: () => of('') } }, { provide: SomeService, useValue: { getInfos: () => of() , getList: () => [] },

  • instant should return value, not observable
  • getInfos should return some result in observable, not undefined
  • getList should return array in observable, not concrete empty array...

for me, any of these cases did cause inconsistent true negatives and false positives...

  1. when you detect it, fix providers and enable the rest of the tests.

Best of luck, this one was a meanace...

I had a similar issue.

I suggest to check that these methods does returns Observables, in your 2 switchMap operator calls:

this.deviceService.getDeviceList()
this.deviceService.flag$

This was the cause in my case. The switchMap operator must return an Observable. So it could be happening that there is no Observable to subscribe to at the of your code.

If you do not need to return Observables in those method try with of(), like this:

// DO NOT forget to import 'of' from rxjs :)
  ngOnInit(): void {
this.timeInterval = interval(5000)
.pipe(
  startWith(0),
  switchMap(() => of(this.deviceService.getDeviceList()))
)
.pipe(
  takeUntil(this.deviceService.flag$),
  repeatWhen(() => of(this.deviceService.flag$))
)
.subscribe((success: any) => {
  this.gridApi?.setRowData(this.updatedData);
  console.log("hello")
  this.updatedData = success;
}, retry(2));
Related