hirezio / auto-spies

Create automatic spies from classes
MIT License
181 stars 30 forks source link

Unable to mock a signal in a property of a mocked class. #85

Open jaytonic opened 2 months ago

jaytonic commented 2 months ago

Describe the bug One dependency of a tested class has a getter of a signal. I need to mock it. When following the guideline, it doesn't seems to compile because its a getter of a signal, which is a method.

To Reproduce Steps to reproduce the behavior:

  1. Create a class with a signal property:
export class DemoClass {
  public get someSignal() {
    return signal(10);
  }
  public get someOtherSignal() {
    return computed(() => this.someSignal() * 2);
  }
}
  1. Create some tests for it, like described in the doc
import { computed, signal } from '@angular/core';
import { createSpyFromClass } from 'jest-auto-spies';

describe('permissions tests', () => {
  it('should compile when trying to mock signal', () => {
    const classSpy = createSpyFromClass(DemoClass, { gettersToSpyOn: ['someSignal'] });
    classSpy.accessorSpies.getters.someSignal.mockReturnValue(10);
  });
  it('should compile when trying to mock computed signal', () => {
    const classSpy = createSpyFromClass(DemoClass, { gettersToSpyOn: ['someOtherSignal'] });
    classSpy.accessorSpies.getters.someOtherSignal.mockReturnValue(20);
  });
});

Expected behavior It should compiles and the signal should return the mocked value

Desktop (please complete the following information):

Additional context The whole point is to be able to mock signal and/or computed signals. Ideally, we should have something similar than for the observable properties.