lukeed/uvu

Any equivalent of jest mock?

dorklord23 opened this issue · 1 comments

Hi folks

I've been trying to integrate uvu and react-testing-library into my team's tech stack. FYI, I can't use Jest because of the infamous Unexpected token 'export' in one of my dependencies.

The challenge I'm facing right now is to mock Firebase functions that is called in my custom hooks. Something like this:

// Custom hook
import { signInAnonymously, onAuthStateChanged } from "firebase/auth";
import { useEffect, useState } from "react";

import { auth } from "@/connections/firebase";

export function useAuthentication() {
  const [isReady, setReady] = useState(false);

  useEffect(() => {
    const unsubscribe = onAuthStateChanged(auth, (user) => {
      if (user) {
        setReady(true);
      } else {
        signInAnonymously(auth)
          .then(() => {
            console.log("Signed in anonymously");
          })
          .catch((error) => {
            console.error(error.message);
          });
      }
    });

    return unsubscribe;
  }, []);

  return isReady;
}
// The component

import { useAuthentication } from "@/hooks/auth";

import type { FC, ReactNode } from "react";

interface LayoutProps {
  children: ReactNode;
}

const Layout: FC<LayoutProps> = ({ children }) => {
  const isReady = useAuthentication();

  if (!isReady) {
    return null;
  }

  return children;
};

export default Layout;

I really need to mock useAuthentication and have it returns true so RTL can properly test the component. I've been trying to use sinon but still no luck. I'm sorry if this is not place to ask questions about usage. I'm getting desperate because of the lack of blog and articles about uvu out there. uvu's support for native ES modules—unlike Jest—is one of the factors that make me plan to stick with it in the future :)

Thank you.