Currently, I'm facing some challenges while testing my Angular 2 service. Even though my tests are passing, I keep encountering this error in the console:
context.js:243 Unhandled Promise rejection: 'expect' was used when there was no current spec; this could be due to an asynchronous test timing out ; Zone: ProxyZone ; Task: Promise.then ; Value: Error: 'expect' was used when there was no current spec; this could be because an asynchronous test timed out
The service I am working on utilizes PouchDB and returns a promise.
Here is a snippet of my service code:
import { Injectable } from '@angular/core';
import { Project } from './project';
declare var PouchDB:any;
@Injectable()
export class ProjectService {
db: any;
constructor() {
if(navigator.vendor && navigator.vendor.indexOf('Apple') > -1){
this.db = new PouchDB('projects', {adapter: 'fruitdown'});
}else{
this.db = new PouchDB('projects');
}
}
saveProject(project:Project): Promise<any>{
return this.db.put(project);
}
getProjects(limit:number,skip:number): Promise<any> {
return this.db.allDocs({
include_docs: true,
attachments: false,
descending: true,
limit: limit,
skip: skip
});
}
}
Below is an excerpt from my test specification:
import { TestBed, inject, async } from '@angular/core/testing';
import { Project, ProjectService } from './index';
describe('ProjectService', () => {
let project: Project;
let service: ProjectService;
let createFakeProject = function() {
let project = new Project;
project._id = 'iwhxu27i';
project.name = 'foo bar';
project.email = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a1a8a887a5e9a6b5">[email protected]</a>';
return project;
}
beforeEach(() => {
const injector = TestBed.configureTestingModule({
providers: [ProjectService]
});
service = injector.get(ProjectService);
project = createFakeProject();
});
it('should be able to CREATE a new project (async)',
async( (done) => {
service.saveProject(project).then(
response => {
expect(response).toEqual(project);
done();
} );
}));
});
I have been struggling with this issue for quite some time now. Perhaps I need to utilize fakeAsync and tick()? Though, using fakeAsync doesn't feel like the ideal solution. It appears that calling done() within a .finally() block might be necessary, however, .finally() seems to not be a method. As I am relatively new to testing Promises with Jasmine, it's possible that I am overlooking something obvious. If you know of any code snippets (or example codes) incorporating Angular 2, Jasmine, and promises, I would greatly appreciate your assistance.
I prefer not to mock PouchDB and concoct my custom stubbed Promise.
Despite the fact that this test should technically fail since the response != project, it passes without any issues. However, the error message persists in my console. Any guidance or advice would be highly valuable. Thank you!