我正在跟踪这个Mock new Function() with Jest来模拟PubSub,不幸的是没有运气。
jest.mock('@google-cloud/pubsub', () => jest.fn())
...
const topic = jest.fn((name) => ({ '@type': 'pubsub#topic', name }))
const publish = jest.fn((data) => ({ '@type': 'Buffer', data }))
const mockedPubSub = PubSub as jest.Mock<PubSub>
mockedPubSub.mockImplementation(() => ({ topic }))我有两个错误。
第一个是线
PubSub as jest.Mock<PubSub>将“
of PubSub”类型转换为“Mock”类型可能是一个错误,因为两种类型都没有与另一种类型充分重叠。如果这是有意的,首先将表达式转换为“未知”。
第二条在最后一条线
mockedPubSub.mockImplementation(() => ({ publish }))参数类型'() => {发布: jest.Mock<{ '@ type ':jest.Mock<;data: any;},data: any>;}‘不能分配给类型的参数'(...args: any) => PubSub’。
我怎么能嘲笑这个?
发布于 2022-01-11 12:04:38
找到答案
const mockTopic = jest.fn().mockImplementation(() => ({
get: jest.fn(),
publish: jest.fn(),
}));
const mockPublish = jest.fn();
jest.mock('@google-cloud/pubsub', () => ({
__esModule: true,
PubSub: jest.fn().mockImplementation(() => ({
topic: mockTopic,
publish: mockPublish,
})),
}))https://stackoverflow.com/questions/70666327
复制相似问题