4

My component subscribes to an Observable in a Service, which is populated via an Ngrx selector, generalized here for brevity:

export class MyService {
  signInFalied$: Observable<boolean>;

  constructor(
    private store: Store<MyAppState>,
  ) {
    this.signInFailed$ = this.store.select(mySelectors.signInFailed);
  }
}

My component has conditional content based on this state value, and I would like to test that the correct content is displayed. In my test, I'm providing a mock for the service as such:

describe('My Test', () => {
  let spectator: SpectatorHost<MyComponent>;

  const createHost = createHostComponentFactory({
    component: MyComponent,
    declarations: [MyComponent],
    providers: [
      ...,
      mockProvider(MyService, {
        signInFailed$: cold('x', { x: null }),
        ...
      }),
    ],
    imports: [...]
  });
});

When I run tests, I get:

Error: No test scheduler initialized

Through searches I have tried setting my compile target to ES5

I'm also using the latest version of jasmine-marbles at this time: 0.6.0

What am I doing wrong?

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144

3 Answers3

3

cold needs to be in an async scope. So you need to add a beforeEach and call this in an async scope:

import { async } from '@angular/core/testing';

describe('My Test', () => {
   beforeEach(async(() => {

       TestBed.configureTestingModule({
           providers: [
              ...,
              mockProvider(MyService, {
                signInFailed$: cold('x', { x: null }),
                ...
              }),
            ],
        })
        .compileComponents()
   });


});
Liam
  • 27,717
  • 28
  • 128
  • 190
  • 1
    this answer was almost useful but and helped me to reach my answer below +1 but this answer does not specify what the mockProvider function is – danday74 Mar 19 '21 at 12:56
  • I don't know what the `mockProvider` is. That's just taken from the OPs question and they don't give this detail. I'm guessing it's a mocked provider... – Liam Aug 23 '21 at 07:52
0

I think I have faced this issue before. I am not sure about angular-spectator but for jasmine on the first beforeEach I call initTestScheduler and addMatchers.

Something like this:

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

describe('MyComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({ 
     ....
    }).compileComponents();

    initTestScheduler();
    addMatchers();
  }));
});
AliF50
  • 16,947
  • 1
  • 21
  • 37
  • Hmm. That's not working for me in this case using Spectator. I've just about given up on using Spectator for this particular test. I was able to get a standard Angular unit test working and setting state as expected. Thank you for the suggestion though, I appreciate it. – Brandon Taylor Apr 02 '20 at 02:40
0

This worked for me, the relevant bit being the providers array (have left the rest of the code for context only):

beforeEach(async () => {
  await TestBed.configureTestingModule({
    imports: [BusinessModule, RouterTestingModule, HttpClientTestingModule, ToastrModule.forRoot()],
    providers: [
      {
        provide: DataSourcesService,
        useValue: {
          activeBusinessDataSources$: cold('--x|', { x: activeBusinessDataSources })
        }
      }
    ]
  }).compileComponents();
});
danday74
  • 52,471
  • 49
  • 232
  • 283