我正在使用这个amplitude
模块,该模块要求我首先实例化一个类,然后使用实例方法。代码如下:
var Amplitude = require('amplitude');
const amplitude = new Amplitude(process.env.amplitudeApiKey);
然后我稍后调用await amplitude.track({something: 'here'})
并希望对其进行模拟,以便不调用外部库,并且可以验证参数。我必须初始化一个类的事实使我无法接受模拟。
我尝试使用__mocks__
文件夹,但Typescript建议将自动嘲笑变成一个。对于我的一生,我不知道该如何模拟这种情况。有什么想法吗?
答案 0 :(得分:0)
所以我最终使用了documented on the Jest website
const Amplitude = require('amplitude');
jest.mock('amplitude');
test('#trackInAmplitude calls the amplitude track event', async () => {
// Put the code here that calls my `track method`
const mockAmplitude = Amplitude.mock.instances[0];
expect(mockAmplitude.track.mock.calls[0][0]).toEqual('argument body');
});