I am facing an issue with my Angular unit test using Jasmine-Karma. The test seems to finish prematurely without completing all the steps. Can someone please assist me with this problem?
Output:
LOG: '{"a":"b"}'
LOG: '1'
LOG: 'Finished!'
Error:
null: Unhandled Promise rejection: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out ; Zone: ProxyZone ; Task: Promise.then ; Value:
null: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out
Unit test:
import 'rxjs/add/observable/of';
import { } from 'jasmine';
import { TestBed, inject } from '@angular/core/testing';
import { async } from '@angular/core/testing';
import { Observable } from 'rxjs/Observable';
interface FileResponse {
data: Blob;
}
class BlobUtil {
public static asString(blob: Blob) {
return new Promise<string>((resolve, reject) => {
var reader = new FileReader();
reader.onload = (event: any) => resolve(event.target.result);
try {
reader.readAsText(blob);
}
catch (e) {
reject(e);
}
});
}
}
class Service {
public static async SafeCall(executor: Observable<FileResponse>,
success: (response: string) => void, failure: (error: string) => void) {
try {
var response = await executor.toPromise();
success(await this.Handle(response));
}
catch (e) {
failure(e.message);
}
}
private static async Handle(response: FileResponse) {
return await BlobUtil.asString(response.data);
}
}
describe('Tests', () => {
afterEach(() => {
console.log("Finished!");
});
it("test", async(inject([],
async () => {
var request = Observable.of({ data: new Blob(['{"a":"b"}'], { type: 'text/plain' }) });
await Service.SafeCall(request, response => console.log(response), error => console.log(error));
expect(true).toBeTruthy();
console.log("1");
await Service.SafeCall(request, response => console.log(response), error => console.log(error));
console.log("2");
expect(true).toBeTruthy();
console.log("3");
await Service.SafeCall(request, response => console.log(response), error => console.log(error));
console.log("4");
expect(true).toBeTruthy();
console.log("5");
})));
});