[英]Getting TimeOut Error while running asynchronous 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
});
}
}
單元測試:
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
});
當我運行時,出現以下錯誤:
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:
有人可以幫我在哪里失蹤嗎? 任何幫助將非常感激。
提前致謝。
這是單元測試解決方案:
dbService.ts
:
import { Connector, DbConnector } from './dbConnector';
export class DBService {
private connector: Connector;
constructor() {
this.connector = DbConnector.getInstance();
this.connector.connect();
}
public async saveData() {
const collName = 'collName';
const condition = 'condition';
const update = {};
const options = {};
return this.connector.update(collName, condition, update, options).then((reconds) => {
return reconds;
});
}
}
dbConnector.ts
:
export interface Connector {
connect(): void;
update(collName, condition, update, options): Promise<any>;
}
export class DbConnector implements Connector {
public static connector: Connector;
public static getInstance() {
if (this.connector) {
return this.connector;
}
this.connector = new DbConnector();
return this.connector;
}
private constructor() {}
public connect() {
console.log('connect to db');
}
public async update(collName, condition, update, options) {
return 'real update';
}
}
dbService.test.ts
:
import { DBService } from './dbService';
import { DbConnector } from './dbConnector';
describe('61815803', () => {
it('should pass', async () => {
const records = { ok: 1 };
const dbConnectorMock = {
connect: jest.fn(),
update: jest.fn().mockResolvedValueOnce(records),
};
jest.spyOn(DbConnector, 'getInstance').mockReturnValueOnce(dbConnectorMock);
const dbService = new DBService();
const actual = await dbService.saveData();
expect(actual).toEqual({ ok: 1 });
expect(DbConnector.getInstance).toBeCalledTimes(1);
expect(dbConnectorMock.connect).toBeCalledTimes(1);
expect(dbConnectorMock.update).toBeCalledWith('collName', 'condition', {}, {});
});
});
帶有覆蓋率報告的單元測試結果:
PASS stackoverflow/61815803/dbService.test.ts (10.698s)
61815803
✓ should pass (6ms)
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 76 | 0 | 55.56 | 73.91 |
dbConnector.ts | 50 | 0 | 20 | 45.45 | 9-13,17,20
dbService.ts | 100 | 100 | 100 | 100 |
----------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 12.495s
您可能希望為 jestjs 全局設置timeout
配置,例如: jest.setup.js
:
jest.setTimeout(10 * 1000);
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node',
setupFilesAfterEnv: [
'./jest.setup.js',
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.