使用 jest 进行测试时如何模拟 react-native-google-analytics-bridge?

How do I mock react-native-google-analytics-bridge when using jest for testing?

我正在使用 jest 测试一个模块,该模块调用一个函数,该函数又从 react-native-google-analytics-bridge 调用 GoogleAnalyticsTracker。我已经尝试了我能想到的一切(文档有点稀疏)来模拟这个函数,但我总是得到真正的模块而不是模拟模块。你能看出我错了或漏掉了哪一步吗?

jest.mock('react-native-google-analytics-bridge');
import {GoogleAnalyticsTracker} from 'react-native-google-analytics-bridge';

describe('testing mocking', () => {
  it ('should return undefined when mocked', () => {
    let tracker2 = new GoogleAnalyticsTracker(null);
    expect(tracker2).toBe(undefined);
  })
})

...但它总能找到未模拟的模块...

Expected value to be (using ===):
      undefined
    Received:
      {"allowIDFA": [Function allowIDFA], "setAnonymizeIp": [Function setAnonymizeIp], "setAppName": [Function setAppName], "setAppVersion": [Function setAppVersion], "setSamplingRate": [Function setSamplingRate], "setTrackUncaughtExceptions": [Function setTrackUncaughtExceptions], "setUser": [Function setUser], "trackEvent": [Function trackEvent], "trackEventWithCustomDimensionValues": [Function trackEventWithCustomDimensionValues], "trackException": [Function trackException], "trackMultiProductsPurchaseEvent": [Function trackMultiProductsPurchaseEvent], "trackMultiProductsPurchaseEventWithCustomDimensionValues": [Function trackMultiProductsPurchaseEventWithCustomDimensionValues], "trackPurchaseEvent": [Function trackPurchaseEvent], "trackScreenView": [Function trackScreenView], "trackScreenViewWithCustomDimensionValues": [Function trackScreenViewWithCustomDimensionValues], "trackSocialInteraction": [Function trackSocialInteraction], "trackTiming": [Function trackTiming], "transformCustomDimensionsFieldsToIndexes": [Function transformCustomDimensionsFieldsToIndexes]}

您试过使用模拟版本作为导入版本吗?

var GoogleAnalyticsTracker = jest.mock('react-native-google-analytics-bridge');

这是答案...在尝试模拟之前,您确实需要从模块中获取真实对象!

import {GoogleAnalyticsTracker} from 'react-native-google-analytics-bridge';
jest.mock('react-native-google-analytics-bridge');

我现在可以创建一个跟踪器...

let tracker1 = new GoogleAnalyticsTracker('ABC-123');

...并将其传递到我要测试的函数中。

Jest 创建了 tracker1.trackTiming,我可以通过使用...

找出传递给它的参数
console.log('tracker1',tracker1.trackTiming.mock.calls);

在我的例子中,returns 将写入 GoogleAnalytics 的参数不会用测试数据实际污染我的分析数据。

您可以使用 Jest 的 genMockFromModule 方法并将其添加到模拟中。

const mockAnalytics = jest.genMockFromModule('react-native-google-analytics-bridge');
module.exports = mockAnalytics;

聚会有点晚了,Google 2019 年 10 月 31 日之后将不再支持移动分析,但这也是一个解决方案(使用 Jest module mocks solution

创建一个包含以下内容的 __mocks__/react-native-google-analytics-bridge.js 文件:

export const setAppVersion = jest.fn();
export const setUser = jest.fn();
export const trackScreenView = jest.fn();
export const trackEvent = jest.fn();

export const  GoogleAnalyticsTracker = jest.fn().mockImplementation(() => {
  return {
    setAppVersion,
    setUser,
    trackScreenView,
    trackEvent
  };
});

随意添加您需要的方法,这只是一个子集。仅此一项就可以使依赖于 react-native-google-analytics-bridge 的测试不会失败。

如果您想测试通话,请在您的测试文件上:

import { setAppVersion } from 'react-native-google-analytics-bridge';
...
expect(setAppVersion).toHaveBeenCalledWith('1.0.0');