由于无法解释的原因,不时会返回一个难以调试的错误:
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)
你们中有人知道如何跟踪它所涉及的内容吗?
用途:
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",
在karma.conf.js
中的插件中添加require('karma-spec-reporter')
,在报告程序添加spec
。这应该列出每个成功/失败的测试以及错误消息。
这将有助于您确定哪个测试会引发此错误。
在我的案例中,即使我的单元测试文件中没有afterAll
语句,我也看到了错误。
在我的特定情况下,当测试一种方法时,错误会发生,该方法使用mergeMap
按顺序组合两个可观测值(一个可观测的输出变成另一个可观察的输入(,然后全部组合为.pipe(map(...),map(...))
。单元测试依赖于一个没有处理pipe()
的服务的模拟
之前在我的service.mock.ts
中,我有:
get: jasmine.createSpy('get').and.returnValue({
pipe: jasmine.createSpy('pipe'),
})
错误消失了:
get: jasmine.createSpy('get').and.returnValue({
pipe: jasmine.createSpy('pipe').and.returnValue(of({})),
})
我遇到了同样的问题。我发现这是由switchMap引起的,该switchMap在所有情况下都没有返回可观察的结果。
一个简单的默认返回EMPTY就足以修复错误。