How to fix jest test TypeError: Cannot set property selectStoreEntity$ of [object Object] which has only a getter?

Viewed 117

How to test this readonly observable variable in jest?

I tried following the directions via this article --> https://medium.com/angular-in-depth/how-to-test-observables-a00038c7faad

I get this error when it's ran though: Cannot set property selectStoreEntity$ of [object Object] which has only a getter

voice-uc.service.spec.ts

 // whether or not the account has uc-voice
  readonly hasVoiceUc$ = this.inventory$.pipe(
    map(inventory => inventory?.data?.data?.length > 0)
  );

voice-uc.service.spec.ts

it('should call hasVoiceUc$', async () => {
    // Testing using marbles (see https://medium.com/angular-in-depth/how-to-test-observables-a00038c7faad)
    const scheduler = new TestScheduler((actual, expected) => {
      expect(actual).toEqual(expected);
    });

    scheduler.run(({ cold, expectObservable }) => {
      const values = {
        a: null,
        b: {
          data: {},
        },
        c: {
          data: {
            data: [],
          },
        },
        d: {
          data: {
            data: ['value'],
          },
        },
      };
      const source$ = cold('abcd|', values);
      const expectedMarble = 'abcd|';
      const expectedValues = { a: false, b: false, c: false, d: true };

      (selectStoreEntity$ as any) = () => source$;

      service = new VoiceUcService({} as any, loggerService);

      const result$ = service.hasVoiceUc$;
      expectObservable(result$).toBe(expectedMarble, expectedValues);
    });
  });

ERROR: TypeError: Cannot set property selectStoreEntity$ of [object Object] which has only a getter

● VoiceUcService › should call hasVoiceUc$

    TypeError: Cannot set property selectStoreEntity$ of [object Object] which has only a getter

       99 |       const expectedValues = { a: false, b: false, c: false, d: true };
      100 | 
    > 101 |       (selectStoreEntity$ as any) = () => source$;
          |                                  ^
      102 | 
      103 |       service = new VoiceUcService({} as any, loggerService, {} as any);
      104 | 

0 Answers
Related