Skip to content

Instantly share code, notes, and snippets.

@sloyless
Last active May 6, 2025 16:19
Show Gist options
  • Save sloyless/22ecaa2d0cecf8f7b13d02cb54f62a2d to your computer and use it in GitHub Desktop.
Save sloyless/22ecaa2d0cecf8f7b13d02cb54f62a2d to your computer and use it in GitHub Desktop.

Revisions

  1. sloyless revised this gist May 6, 2025. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions jestGlobals.js
    Original file line number Diff line number Diff line change
    @@ -21,11 +21,6 @@ jest.useFakeTimers().setSystemTime(new Date('2025-01-01'));
    // Set time zone
    process.env.TZ = 'America/Los_Angeles';

    // FontAwesome
    jest.mock('@fortawesome/react-native-fontawesome', () => ({
    FontAwesomeIcon: 'house',
    }));

    // ** REACT NATIVE ONLY BELOW **
    Animated.timing = () => ({
    start: () => jest.fn(),
    @@ -47,6 +42,11 @@ jest.mock('react-native/Libraries/Utilities/Platform', () => {
    return platform;
    });

    // FontAwesome
    jest.mock('@fortawesome/react-native-fontawesome', () => ({
    FontAwesomeIcon: 'house',
    }));

    // Animation Mocks
    jest.mock('react-native/Libraries/LayoutAnimation/LayoutAnimation', () => ({
    ...jest.requireActual(
  2. sloyless revised this gist May 6, 2025. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions jestGlobals.js
    Original file line number Diff line number Diff line change
    @@ -9,12 +9,18 @@ global.fetch = jest.fn(() => Promise.resolve({
    }));
    global.navigator = { geolocation: () => {} };

    // Mock axios to prevent network errors
    jest.mock('axios');

    // Mock UUID values
    jest.mock('uuid', () => ({ v4: () => 'mocked-uuid' }));

    // Mock date
    jest.useFakeTimers().setSystemTime(new Date('2025-01-01'));

    // Set time zone
    process.env.TZ = 'America/Los_Angeles';

    // FontAwesome
    jest.mock('@fortawesome/react-native-fontawesome', () => ({
    FontAwesomeIcon: 'house',
  3. sloyless revised this gist May 6, 2025. 1 changed file with 16 additions and 33 deletions.
    49 changes: 16 additions & 33 deletions jestGlobals.js
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,25 @@ import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/asy
    import fetch from 'isomorphic-fetch';
    import { Animated, NativeModules } from 'react-native';

    global.fetch = fetch;
    // General React Mocks
    global.fetch = jest.fn(() => Promise.resolve({
    ok: true,
    json: () => Promise.resolve({})
    }));
    global.navigator = { geolocation: () => {} };

    jest.useFakeTimers();
    jest.mock('axios');

    jest.mock('uuid', () => ({ v4: () => 'mocked-uuid' }));

    jest.useFakeTimers().setSystemTime(new Date('2025-01-01'));

    // FontAwesome
    jest.mock('@fortawesome/react-native-fontawesome', () => ({
    FontAwesomeIcon: 'house',
    }));

    // ** REACT NATIVE ONLY BELOW **
    Animated.timing = () => ({
    start: () => jest.fn(),
    });
    @@ -161,37 +175,6 @@ jest.mock('react-native-permissions', () =>
    require('react-native-permissions/mock')
    );

    // Allows mocks of BottomSheet
    jest.mock('@gorhom/bottom-sheet', () => {
    const reactNative = jest.requireActual('react-native');
    const { View } = reactNative;

    return {
    __esModule: true,
    default: View,
    BottomSheetModal: View,
    BottomSheetModalProvider: View,
    useBottomSheetModal: () => ({
    present: jest.fn(),
    dismiss: jest.fn(),
    }),
    useBottomSheet: () => ({
    present: jest.fn(),
    dismiss: jest.fn(),
    }),
    useBottomSheetDynamicSnapPoints: () => ({
    animatedHandleHeight: jest.fn(),
    animatedSnapPoints: jest.fn(),
    animatedContentHeight: jest.fn(),
    }),
    };
    });

    // FontAwesome
    jest.mock('@fortawesome/react-native-fontawesome', () => ({
    FontAwesomeIcon: 'house',
    }));

    // Safe Area Context
    jest.mock(
    'react-native-safe-area-context',
  4. sloyless created this gist Sep 17, 2023.
    205 changes: 205 additions & 0 deletions jestGlobals.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,205 @@
    import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';
    import fetch from 'isomorphic-fetch';
    import { Animated, NativeModules } from 'react-native';

    global.fetch = fetch;
    global.navigator = { geolocation: () => {} };

    jest.useFakeTimers();

    Animated.timing = () => ({
    start: () => jest.fn(),
    });

    // Platform mocks
    jest.mock('react-native/Libraries/Utilities/Platform', () => {
    let platform = {
    OS: 'web',
    };

    const select = jest.fn().mockImplementation((obj) => {
    const value = obj[platform.OS];
    return !value ? obj.default : value;
    });

    platform.select = select;

    return platform;
    });

    // Animation Mocks
    jest.mock('react-native/Libraries/LayoutAnimation/LayoutAnimation', () => ({
    ...jest.requireActual(
    'react-native/Libraries/LayoutAnimation/LayoutAnimation'
    ),
    configureNext: jest.fn(),
    }));

    jest.mock('react-native-gesture-handler', () => {});
    jest.mock('react-native-reanimated', () => {});

    jest.mock('react-native-sensitive-info', () => {
    return {
    setItem: jest.fn(() => new Promise.resolve(null)),
    };
    });

    jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);

    jest.mock('@react-native-community/netinfo', () => {
    return {
    fetch: () => {
    return new Promise((resolve, reject) => {
    resolve({
    isConnected: true,
    });
    reject({
    isConnected: false,
    });
    });
    },
    };
    });

    jest.mock('react-native-device-info', () => {
    return {
    getVersion: () => 4,
    getSystemVersion: () => '14',
    getUserAgent: jest.fn(() => new Promise.resolve('test')),
    getIpAddress: jest.fn(() => {
    return {};
    }),
    hasNotch: jest.fn(() => {
    return true;
    }),
    };
    });

    // React Navigation Mocks
    jest.mock('@react-navigation/drawer', () => {
    return {
    createDrawerNavigator: jest.fn(),
    StackActions: {
    push: jest
    .fn()
    .mockImplementation((x) => ({ ...x, type: 'Navigation/PUSH' })),
    replace: jest
    .fn()
    .mockImplementation((x) => ({ ...x, type: 'Navigation/REPLACE' })),
    },
    NavigationActions: {
    navigate: jest.fn().mockImplementation((x) => x),
    },
    useDrawerStatus: jest.fn(),
    };
    });

    jest.mock('@react-navigation/bottom-tabs', () => {
    return {
    createBottomTabNavigator: jest.fn(),
    };
    });

    jest.mock('@react-navigation/stack', () => {
    return {
    createStackNavigator: jest.fn().mockReturnValue({
    Navigator: ({ children }) => <>{children}</>,
    Screen: ({ children }) => <>{children}</>,
    }),
    };
    });

    jest.mock('@react-navigation/native', () => {
    return {
    createDrawerNavigator: jest.fn(),
    createStackNavigator: jest.fn(),
    useIsFocused: () => true,
    useNavigation: () => ({
    navigate: jest.fn(),
    dispatch: jest.fn(),
    goBack: jest.fn(),
    dismiss: jest.fn(),
    navigate: jest.fn(),
    openDrawer: jest.fn(),
    closeDrawer: jest.fn(),
    toggleDrawer: jest.fn(),
    getParam: jest.fn(),
    setParams: jest.fn(),
    addListener: jest.fn(),
    push: jest.fn(),
    replace: jest.fn(),
    pop: jest.fn(),
    popToTop: jest.fn(),
    isFocused: jest.fn(),
    }),
    StackActions: {
    push: jest
    .fn()
    .mockImplementation((x) => ({ ...x, type: 'Navigation/PUSH' })),
    replace: jest
    .fn()
    .mockImplementation((x) => ({ ...x, type: 'Navigation/REPLACE' })),
    reset: jest.fn(),
    },
    useFocusEffect: () => jest.fn(),
    useNavigationContainerRef: () => jest.fn(),
    useTheme: () => {
    return {
    selectedTheme: '',
    colors: {},
    theme: {
    color: {},
    fontFamily: {},
    fontSize: {},
    },
    };
    },
    };
    });

    jest.mock('react-native-permissions', () =>
    require('react-native-permissions/mock')
    );

    // Allows mocks of BottomSheet
    jest.mock('@gorhom/bottom-sheet', () => {
    const reactNative = jest.requireActual('react-native');
    const { View } = reactNative;

    return {
    __esModule: true,
    default: View,
    BottomSheetModal: View,
    BottomSheetModalProvider: View,
    useBottomSheetModal: () => ({
    present: jest.fn(),
    dismiss: jest.fn(),
    }),
    useBottomSheet: () => ({
    present: jest.fn(),
    dismiss: jest.fn(),
    }),
    useBottomSheetDynamicSnapPoints: () => ({
    animatedHandleHeight: jest.fn(),
    animatedSnapPoints: jest.fn(),
    animatedContentHeight: jest.fn(),
    }),
    };
    });

    // FontAwesome
    jest.mock('@fortawesome/react-native-fontawesome', () => ({
    FontAwesomeIcon: 'house',
    }));

    // Safe Area Context
    jest.mock(
    'react-native-safe-area-context',
    () => require('react-native-safe-area-context/jest/mock').default
    );

    jest.mock('react-native-avoid-softinput', () => {
    const mock = require('react-native-avoid-softinput/jest/mock');
    return mock;
    });