如果使用 RxJS 的 shareReplay 运算符,则在 mocha 中测试未完成

Test in mocha not completing if shareReplay operator of RxJS is used

我有一个简单的 Javascript 函数,它 returns 一个可观察对象,我应用了带参数 1 的 shareReplay 运算符。

[![export function doStuffWithShareReplay() {
    return interval(100).pipe(
        shareReplay(1),
        tap(d => console.log('do stuff 1', d)),
        take(5)
    );
}

如果我将这样的函数放在 mocha 测试中,并从 VSCode 中 运行 测试,似乎测试的执行永远不会完成并且我必须手动停止测试执行。更准确地说,测试按预期通过了,但是 VScode 顶部中心的小控制面板没有关闭,我必须单击红色按钮将其关闭,如下图所示。如果我删除 shareReplay ,执行将按预期结束。我想知道这种行为的原因是什么。

使用publishReplay(1)refCount()代替shareReplay(1)

return interval(100).pipe(
  publishReplay(1),
  refCount(),
  ...

自 RxJS 5.5(RxJS 6.1 中仍然存在)以来 shareReplay(1) 中存在一个错误,阻止它取消订阅其来源。

更多详情见本期:https://github.com/ReactiveX/rxjs/issues/3336