Tests don't pass using react-testing-library when adding Firebase persistence, but when testing manually, everything works

Viewed 254

I am testing my SingIn component in my React app. All tests passed until I added firebase.auth().setPersistance() to my submit handler. I use jest with react-testing-library for tests.

Part of test that fails:

typeLoginCredentials(email, 'badpassword');
act(() => {
  userEvent.click(screen.getByText(/Login/));
});
expect(await screen.findByText(/Wrong password/)).toBeInTheDocument();

and part of my submit handler:

...
try {
  let persistence = rememberAuth ? firebase.auth.Auth.Persistence.LOCAL : 
                                   firebase.auth.Auth.Persistence.SESSION;
  await fireApp
    .auth()
    .setPersistence(persistence);
  await fireApp
     .auth()
     .signInWithEmailAndPassword(email, password);
...

When I test this manually in my browser, everything works fine. But tests pass only when I remove persistence part:

await fireApp.auth().setPersistence(persistence);

I even tried to change findBy timeout, but it didn't help.

1 Answers

Figured out it is the problem with node test environment supporting only firebase.auth.Auth.Persistence.NONE. Error was always thrown in my code when testing, but my catch block was badly implemented, so it failed only by not finding expected text in the DOM.

Related