Ngrx Effects 规范抛出错误 "No test scheduler initialized"

Ngrx Effects spec throws Error as "No test scheduler initialized"

尝试 运行 对现有和最近迁移的 Angular 7 项目进行简单效果测试。但是我得到如下错误。

错误:未初始化测试调度程序 在 getTestScheduler (node_modules/jasmine-marbles/es6/src/scheduler.js:11:1) 在新的 TestHotObservable (node_modules/jasmine-marbles/es6/src/test-observables.js:21:39) 在 Module.hot (node_modules/jasmine-marbles/es6/index.js:7:1)

我在效果规范文件中的代码是使用 jasmine-marbles 的基本标准检查。

const action = new Load(request);
const completion = new LoadSuccess(result);

actions$ = hot('-a-', { a: action});
const response = cold('-a|', {a: result});
const expected = cold('--b', {b: completion});
service.getSomething.and.returnValue(result);
expect(effects.load$).toBeObservable(expected);

有没有人见过并解决过这个错误?

经过进一步研究发现这是由于 tsconfig.spec.json 中的编译器选项造成的。最初它被设置为 "target":"es6",将其更改为 es5 解决了这个问题并且规格现在成功 运行。

虽然改用 ES5 解决了问题,但我的同事提出了更好的解决方案。解决方案是在 src/test.ts 文件中添加以下行。我更喜欢它,因为它允许在 ES6 中继续测试。

import { addMatchers, getTestScheduler, initTestScheduler, resetTestScheduler } from 'jasmine-marbles';

// configure matchers for jasmine-marbles
jasmine.getEnv().beforeAll(() => {
  return addMatchers();
});
jasmine.getEnv().beforeEach(() => {
 initTestScheduler();
});
jasmine.getEnv().afterEach(() => {
 getTestScheduler().flush();
 resetTestScheduler();
});

将 jasmine-marbles 升级到 0.6.0 为我解决了这个问题。

在我的例子中,No test scheduler initialized 错误的原因是在 beforeEach 块之外使用了 cold

beforeEach 块内创建测试可观察对象解决了问题。