Is there a way to completely isolate a SignalStore test from the SignalStoreFeatures it...

Discussão em 'Angular' iniciado por Steven van Schagen, Janeiro 14, 2025.

  1. I have some questions regarding the testing of SignalStore and SignalStoreFeature.

    I have a SignalStore that injects a number of SignalStoreFeatures. All of these features are responsible for performing service calls and storing some data, exposing computed signals to the SignalStore. The SignalStore operates with all this data from the different features and exposes computed signals and methods to a component.

    I use Spectator for all my tests, I have a spec for the SignalStore and a spec for each SignalStoreFeature. I initialize Spectator with SpectatorService and I create my API service mocks by using createSpyObj from Jasmine. This SignalStore uses SignalStoreFeatures and I know of no way to mock these Features in my test. So, in every test I need to handle the implementation of all these features, and mock all service calls that they perform. I would like to mock these features, so I can test my SignalStore in complete isolation of these features.

    Question: Is there a way to mock SignalStoreFeatures when testing a SignalStore that implements those?

    Additionally, when I run the SignalStore and SignalStoreFeature tests all in one go, I get an error from one of the SignalStoreFeatures stating that an includes operation is performed on undefined. It comes from a computed signal that does an includes on an array. In this SignalStoreFeature, this array is initially an empty array and is loaded with data with the response of a service call. It appears that a test different than the test of this specific SignalStoreFeature leaks data into a test that accesses this computed signal with an undefined array, which results in the error given.

    Question: How is it possible that objects defined in tests are leaking into other tests, even though they have their own Spectator setup, Spy objects, etc.?

    My test setup for the SignalStore is as follows:


    let spectator: SpectatorService<InstanceType<typeof MySignalStore>>

    const apiServiceSpy = createSpyObj(‘ApiService', ['getFeatures', 'getConfig']);

    const createStore = createServiceFactory({
    service: MySignalStore,
    providers: [
    {
    provide: ApiService,
    useValue: apiServiceSpy
    },
    MySignalStore
    ]
    });


    And for the SignalStoreFeatures:

    const testMySignalStoreFeature = signalStore({ providedIn: 'root' }, withMySignalStoreFeature());

    const apiServiceSpy = createSpyObj(‘ApiService', [‘getFeatures’]);


    Regarding the leaking test data, I have tried in all of these different spec files to mock the ApiService endpoints in the global beforeEach(), so that if in some case the endpoint is called there would be no undefined array given back by the service. Apart from that, it feels like a mystery why this happens. It might be that the Angular testing framework is a bit behind on supporting SignalStore tests, and that underwater between tests signals are leaking data.

    Continue reading...

Compartilhe esta Página