This is the code where i am writing unit test in jest:
import { Connector, DbConnector } from "@custom/connector"; // package contains mongodb operations.
export class DBService {
private connector: Connector;
constructor() {
this.connector = DbConnector.getInstance();
this.connector.connect();
}
public async saveData() {
return this.connector.update(collName, condition, update, options).then(() => {
// logger
});
}
}
unit test:
import { Connector, DbConnector } from "@custom/connector";
import DBService from "service.ts";
it("success", async () => {
const db = new DBService ();
const records = { ok: 1 };
jest.spyOn(DbConnector, "getInstance").mockImplementation((): any => {
return {
connect() { return Promise.resolve(); },
update() { return Promise.resolve(records); },
};
});
expect(await db.saveData()).resolves.toEqual(records); // Not sure what to do here
});
When i run, i am getting below error:
Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:
Can somebody help me where i am missing? Any help would be really appreciated.
Thanks in advance.