It seems that due to the delay
, the assertion within your subscribe
function is not being executed.
To confirm that the assertions inside the subscribe block are indeed running, you can try the following:
it('should test the download file', () => {
const fakeurl = 'http://fakeurl';
service.downloadFile(fakeurl).subscribe((resp: IDownload) => {
expect(1).toBe(2); // This test should fail
expect(resp).toBe(mockDownloadedFile);
});
const req = httpMock.expectOne(request => request.url.includes(fakeurl), 'call api');
expect(req.request.method).toBe('GET');
req.flush(new Blob());
subscripton.unsubscribe(); // Unsubscribe from the previous subscription
});
Execute the above test and ensure that it fails to confirm that it is entering the subscribe block for assertions. If it passes, then it's likely not reaching that block.
In your specific scenario, utilizing fakeAsync
could help in simulating time progression in a controlled manner:
import { fakeAsync, tick } from '@angular/core/testing';
....
it('should test the download file', fakeAsync(() => { // Include fakeAsync here
const fakeurl = 'http://fakeurl';
let response: IDownload;
const subscription = service.downloadFile(fakeurl).subscribe((resp: IDownload) => {
expect(1).toBe(2); // Ensure this fails and remove once failure is confirmed
response = resp; // Assign response value here
});
const req = httpMock.expectOne(request => request.url.includes(fakeurl), 'call api');
expect(req.request.method).toBe('GET');
req.flush(new Blob());
tick(3500); // Progress timer by 3500ms to bypass delay
expect(response).toBe(mockDownloadedFile);
}));
Since you're using jest
, there might be compatibility issues with using fakeAsync
. Switching from test
to it
could potentially resolve this problem.