Javascript 正在调用Jest Mock函数,但expect.toHaveBeenCalled仍然失败
以下测试的结果失败: 预期呼叫数:>=1 收到的呼叫数:0 即使调用了模拟函数,测试仍失败。在我的日志中,console.log('event wrapper')实际上也显示console.log('this mock function's called'),因此我不理解为什么行Javascript 正在调用Jest Mock函数,但expect.toHaveBeenCalled仍然失败,javascript,typescript,testing,jestjs,Javascript,Typescript,Testing,Jestjs,以下测试的结果失败: 预期呼叫数:>=1 收到的呼叫数:0 即使调用了模拟函数,测试仍失败。在我的日志中,console.log('event wrapper')实际上也显示console.log('this mock function's called'),因此我不理解为什么行expect(eventWrapper.client.createChannel).tohavebeencall()仍然失败 功能定义如下: 模拟功能: // eventWrapper.ts export const
expect(eventWrapper.client.createChannel).tohavebeencall()代码>仍然失败
功能定义如下:
模拟功能:
// eventWrapper.ts
export const eventWrapper = {
client: {
createChannel: jest
.fn()
.mockImplementation(() => {
console.log('event wrapper');
return {
assertQueue: () => console.log('this mock function is being called'),
sendToQueue: jest.fn()
}
}),
}
};
// test.ts
// POST /api/tickets is successful, its also the one who trigger/call the event wrapper
jest.mock('../../events/EventWrapper');
it('should publish an event after successful creation of tickets', async (done) => {
await request(app).post('/api/tickets').send({
title: 'string',
price: 23,
userId: 'stringdsa',
}).expect(201);
expect(eventWrapper.client.createChannel).toHaveBeenCalled();
});