Jasmine spy state persistence across it blocks with jasmine.createSpy
27.5K reputation · 24 Sept 2023, 15:09 UTC
The goal is to clarify how Jasmine manages spy lifecycle and test isolation when the same spy instance is reused across multiple it blocks within a describe suite.
Spies created with jasmine.createSpy and jasmine.createSpyObj track calls, return values and call order, and the framework does not enforce automatic suite-level isolation for shared references. State persistence can lead to call history accumulation unless cleanup is applied explicitly, and the exact boundaries for automatic reset versus manual cleanup are not clearly documented.
This matters for designing reliable specs that avoid leakage between tests, especially when spies are created at describe scope or shared across files. Behavior is version sensitive and the expected defaults for isolation are uncertain.
Does Jasmine automatically reset calls history for a spy between it blocks? Which creation scope for jasmine.createSpy or jasmine.createSpyObj guarantees isolation without manual reset? When is explicit cleanup required to prevent state leakage across specs?