How to fix Expected $.length = 2 to equal 1
Asked Answered
C

1

11

I want to test ngLogger function with jasmine marble but a got error

Expected $.length = 2 to equal 1.

Expected $.length = 2 to equal 1.

Expected $[0].frame = 0 to equal 10.

Expected $[0].notification.value to be a kind of Observable, but was Object({ type: 'TECHNICAL', level: 'ERROR', msg: 'test' }).

Expected $[1] = Object({ frame: 0, notification: Notification({ kind: 'C', value: undefined, error: undefined, hasValue: false }) }) to equal undefined.

Test:

    export namespace GlobalUtils {
       export function ngLogger(error: string): 
                                   Observable<Log> {
        return of({ type: LogEnum.TECHNICAL,
          level: LevelEnum.ERROR,
          msg: error } as Log
        );
      }
    }



    import { GlobalUtils } from './global.utils';

    it('ngLogger should be return an Observable', () => {
        const expected = of({
          type: LogEnum.TECHNICAL,
          level: LevelEnum.ERROR,
          msg: 'test'
        });

        const expected$ = hot('-a', { a: expected });
        const result$ = GlobalUtils.ngLogger('test');

        expect(result$).toBeObservable(expected$);
      });

const expected$ = hot('a', { a: expected }); don't do any difference. const expected$ = hot('a|', { a: expected }); give the error :

    Expected $[0].notification.value to be a kind of Observable, but was Object({ type: 'TECHNICAL', level: 'ERROR', msg: 'test' }).
    Expected $[1].frame = 0 to equal 10

then I changed

const expected = of({
      type: LogEnum.TECHNICAL,
      level: LevelEnum.ERROR,
      msg: 'test'
    });` to `const expected = of({
      type: LogEnum.TECHNICAL,
      level: LevelEnum.ERROR,
      msg: 'test'
    });

I get the error Expected $[1].frame = 0 to equal 10. what does it means ?

Changchangaris answered 19/6, 2019 at 15:39 Comment(3)
const expected$ = hot('a', { a: expected }); or even const expected$ = hot('a|', { a: expected });?Illusive
const expected$ = hot('a', { a: expected }); don't do any difference.Changchangaris
now i see you have 2 issues first is that marble should be (a|) as this is how you describe simultaneous emit and end observable which is done when using of second issue is that you have your expected defined as observable and it should be only the data insideIllusive
I
15

You have 2 issues first is that marble should be (a|) as this is how you describe simultaneous emit and end observable which is done when using of. The second issue is that you have your expected defined as observable and it should be only the data inside. And thanks to this I have learnt how to use marbles:

const msg = 'test';
const expected = {
  type: LogEnum.TECHNICAL,
  level: LevelEnum.ERROR,
  msg,
}; // notice that this value should not be observable

const expected$ = hot('(a|)', { a: expected }); // also you are returning of which is ending immediately

const result$ = GlobalUtils.ngLogger(msg);

expect(result$).toBeObservable(expected$);

Also in addition here is working example

Illusive answered 19/6, 2019 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.