I am currently facing a challenge with implementing a unit test for an Observable in order to signal the completion of a process. While there is no asynchronous code in the logout function yet, I plan to include it once the full logic is implemented. The main objective here is to have the observable trigger the subscriber upon completion of the logout process without returning any data.
However, my unit tests have highlighted that the subscriber is not being invoked as expected. Typically, I work with async/await due to issues with Observables like this one, but I am trying to improve my skills in front-end development which often favors Observables.
This issue has occurred in a new Angular 9 application that I am setting up. The unit tests are conducted using Jest and run through Wallaby.
Here's what I have tried so far:
To test my unit test, I intentionally introduced a flaw into the function.
https://i.stack.imgur.com/0ZUV6.png
On line 63 of my tests, Wallaby displays a grey box indicating that the test did not execute that particular line. Consequently, the test passed without verifying the assertion made.
If I modify the observable to return a value, I get the line to execute... partially.
https://i.stack.imgur.com/QJI4M.png
Despite this change, I do not receive the expected failure message from my mock. It should indicate "expected clearSession() to be called 1 times, was 0".
When running Jest to gain further insights, I encountered the following error:
FAIL src/app/auth/auth.service.spec.ts
● AuthService › logout › should destroy the session
1 timer(s) still in the queue.
at node_modules/zone.js/dist/fake-async-test.js:621:31
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:386:30)
at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:117:43)
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:385:36)
at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:143:47)
In an odd turn of events, fixing the function resolves all issues. However, this only works when the Observable returns a value, even though its purpose is to simply notify the process completion. Despite this, I have not been able to trigger a failing assertion directly related to the logout process; instead, the failure seems unrelated (e.g., the timer in queue).
https://i.stack.imgur.com/jM2QM.png
The verify
function utilized is from ts-mockito
, which functions similarly to
expect(someSpy).toHaveBeenCalledTimes()
.
Are there any nuances regarding Observables that I might be overlooking?