Jest在throw new Error();
行抱怨。它只是在new关键字下面显示一个错误,而没有提供任何更多信息。为什么我的测试由于抛出错误而失败?我认为我正在处理代码中的错误。
测试
import { QueueService } from '../Queue.service';
import { PersistenceDatabaseRepository } from '../..';
describe('Queue.service', () => {
let queueService: QueueService;
let persistenceRepositoryMock: Partial<PersistenceDatabaseRepository>;
describe('when persistence layer throws errors', () => {
describe('when get throws error', () => {
it('should reject promises and empty queue', async () => {
persistenceRepositoryMock = {
get: jest.fn().mockImplementation(() => {
throw new Error();
}),
};
queueService = new QueueService(
persistenceRepositoryMock as PersistenceDatabaseRepository,
);
const promise1 = queueService.enqueue('1', 'delete');
const promise2 = queueService.enqueue('1', 'delete');
await Promise.all([promise1, promise2]);
expect(promise1).toThrow();
});
});
});
});
已捕获错误的部分经过测试的代码。请参见try/catch
块。
private async commit(id: Id, requests: Array<IChangeRequest<any>>) {
let entity: IEntity;
try {
entity = await this.persistenceRepository.get(id);
} catch (err) {
console.log('get throw');
for (const r of requests) {
r.reject(err);
}
return;
}
const { updatesToResolve, deleteToResolve, changesToReject } = this.groupQueueByCommitCase(
requests,
);
if (updatesToResolve) {
entity = await this.resolveUpdates(entity, updatesToResolve);
}
if (deleteToResolve) {
await this.resolveDelete(entity._id, entity._rev, deleteToResolve);
}
if (changesToReject) {
await this.rejectChanges(changesToReject);
}
}