jeudi 27 août 2020

TypeError: firebase.analytics is not a function when mocking with jest

I am starting to use Jest for testing my React app, I am writing a basic test and I have an exception. The test code is the following

// __test__/App.test.js
import '@babel/polyfill'
import React from 'react';
import App from '../components/App'
import { render } from '@testing-library/react';

jest.mock('firebase/app')
// The error is the same with or without the following line:
firebase.analytics.mockResolvedValue(null)

it("renders without crashing", () => {
  render(<App />);
});

But when I run npx jest I get this output:

 FAIL  src/__test__/App.test.js
  ● Test suite failed to run

    TypeError: firebase.analytics is not a function

       8 | // Initialize Firebase
       9 | firebase.initializeApp(firebaseConfig);
    > 10 | firebase.analytics();
         |          ^
      11 | 
      12 | const db = firebase.firestore();
      13 | 

The app works as it should outside tests

Aucun commentaire:

Enregistrer un commentaire