Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any equivalent of jest mock? #208

Open
dorklord23 opened this issue May 28, 2022 · 1 comment
Open

Any equivalent of jest mock? #208

dorklord23 opened this issue May 28, 2022 · 1 comment

Comments

@dorklord23
Copy link

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.

@gkiely
Copy link

gkiely commented Jun 12, 2022

@dorklord23 Related: #170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants