我正在使用jest測試一個模塊,該模塊調用一個函數,該函數又調用GoogleAnalyticsTracker從react-native-google-analytics-bridge。我試過所有我能想到的東西(文檔有點稀疏)來模擬這個函數,但我總是得到真正的模塊而不是模擬模塊。你能看到哪一步我錯了或失蹤?如何在使用jest進行測試時模擬react-native-google-analytics-bridge?
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);
})
})
...但它總是設法找到unmocked模塊...
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]}
我很高興你找到答案:) – Kutyel
你能分享整個代碼嗎?我仍然無法工作...... – alexmngn