单元测试JS Amplitude SDK

时间:2018-06-07 13:56:35

标签: javascript reactjs unit-testing mocking jestjs

我有这段代码:

import amplitude from 'amplitude-js/amplitude';

export const initAmplitude = () => {
  amplitude.getInstance().init(process.env.REACT_APP_AMPLITUDE);
};

这是测试:

import amplitude from 'amplitude-js/amplitude';

import { initAmplitude } from './amplitude';

jest.mock('amplitude-js/amplitude', () => ({
  getInstance: () => ({
    init: () => jest.fn()
  })
}));

describe('amplitude', () => {
  describe('initAmplitude', () => {
    it('should init amplitude', () => {
      initAmplitude();
      expect(amplitude.getInstance().init()).toHaveBeenCalledTimes(1);
    });
  });
});

我不知道为什么但是没有接到电话:

Expected mock function to have been called one time, but it was called zero times.

如果我将console.logs打印出来,那么我知道它已经到了那里。

0 个答案:

没有答案