Jest: Cannot read properties of undefined (reading ‘getItem’) when trying to mock Async Storage

I am struggling getting my jest spec to work with AsyncStorage. I followed the guide on Jest integration with Async Storage and used the guidance for setting up the mocks directory. This makes my project structure look like so…
.

My async-storage.js has the following contents…

export * from "@react-native-async-storage/async-storage/jest/async-storage-mock";

The spec I am trying to test is checking that I redirect the user to a certain page based on the values retrieved from storage. This is how the spec is set up (please excuse that it is the nicest looking right now – just trying to get the tests to work for now!)…

import { render, waitFor } from "@testing-library/react-native";
import AsyncStorage from "@react-native-async-storage/async-storage/jest/async-storage-mock";
import { useRouter } from "expo-router";

import Index from "@/src/app/index";

jest.mock("expo-router", () => ({
  useNavigation: jest.fn(),
  useFocusEffect: jest.fn().mockImplementation((callback) => callback()),
  useRouter: jest.fn()
}));

describe("Index page", () => {
  const mockRouter = {
    replace: jest.fn()
  }
  ;(useRouter as jest.Mock).mockReturnValue(mockRouter)

  async function asyncOperationOnAsyncStorage(firstTime: string){
    await AsyncStorage.setItem("firstTime", firstTime)

    await AsyncStorage.getItem("firstTime")
  }

  it("renders successfully", async () => {
    render(<Index />);
  });

  it("redirects to 'First Launch' page when the user opens the app for the first time", async () => {
    await asyncOperationOnAsyncStorage("");
    render(<Index />);

    await waitFor(() => {
      expect(mockRouter.replace).toHaveBeenCalledWith("/first-launch");
    })
  })

  it("redirects to 'Login' page when it is not the user's first time using the app", async () => {
    await asyncOperationOnAsyncStorage("DONE");

    render(<Index />);

    await waitFor(() => {
      expect(mockRouter.replace).toHaveBeenCalledWith("/login");
    })
  })

The index page is coded up like so…

import { View, Text } from 'react-native';
import { useFocusEffect, useRouter } from "expo-router";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useCallback } from 'react';

export default function Index() {
  const router = useRouter();

  const handleRedirect = async () => {
    try {
      const firstTime = await AsyncStorage.getItem("firstTime");
      
      if (firstTime !== "DONE") {
        router.replace("/first-launch");
      else {
        router.replace("/login");
      }
    } catch (error) {
      console.log("Cannot retrieve data from storage", error);
      router.replace("/first-launch");
    }
  }

  useFocusEffect(
    useCallback(() => {
      handleRedirect()
    }, [handleRedirect])
  );

  return (
    <View>
      <Text>Redirecting...</Text>
    </View>
  );
}

I thought I had my async set up right, but whenever I run the tests, I get this error in the console…

Cannot retrieve data from storage TypeError: Cannot read properties of undefined (reading 'getItem')

This implies that the spec fails when performing async’s getItem when running my index‘s handleRedirect() function. This error does not occur when I run the app, so I assume I must not have set up my Jest and Async Storage integration right – but I have no clue where I went wrong!

Thank you in advance for any help!

The problem seems to be resolved now. The fix was to change my async-storage.js to…

import AsyncStorage from "@react-native-async-storage/async-storage/jest/async-storage-mock";
export default AsyncStorage;

…And then change my import for AsyncStorage on my index spec file to be import AsyncStorage from "@react-native-async-storage/async-storage/jest/async-storage-mock";

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật