3

Trying to run a simple effects test with an existing and recently migrated Angular 7 project. But I get error as below.

Error: No test scheduler initialized at getTestScheduler (node_modules/jasmine-marbles/es6/src/scheduler.js:11:1) at new TestHotObservable (node_modules/jasmine-marbles/es6/src/test-observables.js:21:39) at Module.hot (node_modules/jasmine-marbles/es6/index.js:7:1)

My code in effects spec file is basic standard check with 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);

Has anyone seen and resolved this error before?

mruanova
  • 6,351
  • 6
  • 37
  • 55
Anand
  • 245
  • 4
  • 15

4 Answers4

9

Although changing to ES5 fixed the problem, my colleague has come up with a better solution. Solution is to add the following lines in src/test.ts file. I like it better as it allows to continue testing in 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();
});
Anand
  • 245
  • 4
  • 15
  • 2
    After upgrading `jasmine-marbles to 0.6.0` this solution with break tests using it so you should remove it after upgrading – michalSolarz Jan 07 '20 at 14:11
7

Upgrading jasmine-marbles to 0.6.0 resolved this problem for me.

andrewpm
  • 534
  • 6
  • 12
0

After further research figured out that it was due to compiler options in tsconfig.spec.json. Originally it was setup as "target": "es6", changing it to es5 fixed this issue and specs are successfully running now.

Anand
  • 245
  • 4
  • 15
0

In my case the reason for the No test scheduler initialized error was using cold outside the beforeEach block.

Creating the test observables inside the beforeEach block solved the problem.

Eneko
  • 1,709
  • 1
  • 16
  • 25