Random: An error was thrown in afterAll TypeError: You provided 'undefined' where a stream was expected

Viewed 2281

From time to time karma for unexplained reasons, returns a difficult to debug error:

An error was thrown in afterAll
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/_esm5/internal/util/subscribeTo.js:28:1)
    at subscribeToResult (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm5/internal/util/subscribeToResult.js:15:23)
    at MergeMapSubscriber._innerSub (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm5/internal/operators/mergeMap.js:74:50)
    at MergeMapSubscriber._tryNext (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm5/internal/operators/mergeMap.js:68:1)
    at MergeMapSubscriber._next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm5/internal/operators/mergeMap.js:51:1)
    at MergeMapSubscriber.Subscriber.next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm5/internal/Subscriber.js:53:1)
    at FilterSubscriber._next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm5/internal/operators/filter.js:38:1)
    at FilterSubscriber.Subscriber.next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm5/internal/Subscriber.js:53:1)
    at SafeSubscriber.__tryOrUnsub (http://localhost:9876/_karma_webpack_/node_modules/rxjs/internal/Subscriber.js:205:1)
    at SafeSubscriber.next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/internal/Subscriber.js:143:1)

Any of you have an idea how to track what it concerns?

Use:

Angular 9
    "jasmine-core": "^3.5.0",
    "jasmine-marbles": "^0.6.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^4.4.1",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.1",
    "karma-jasmine": "~3.1.1",
    "karma-jasmine-html-reporter": "^1.5.2",
    "karma-scss-preprocessor": "^4.0.0",
1 Answers

In karma.conf.js in your plugins add require('karma-spec-reporter') and in your reporters add spec. This should list every test as it succeeds / fails along with the error messages.

This should help you identify which of the tests throws this error.

In my case I saw the error even though no afterAll statements were in my unit tests file.

In my specific case, the error would occur while testing a method that was using mergeMap to combine, in sequence, two observables (output of one observable became the input of another observable) and later it all would .pipe(map(...),map(...)). The unit tests relied on a mock of a service which didn't handle the pipe()

Previously in my service.mock.ts I had:

get: jasmine.createSpy('get').and.returnValue({
  pipe: jasmine.createSpy('pipe'),
})

The error went away with:

get: jasmine.createSpy('get').and.returnValue({
  pipe: jasmine.createSpy('pipe').and.returnValue(of({})),
})
Related