Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest: How to consume result of jest.genMockFromModule

I'm attempting to mock the firebase module

in my __mocks__/firebase.js file i simply put

const mockFirebase = jest.genMockFromModule('firebase');
export default mockFirebase;

and in my code if i do

import * as firebase from 'firebase';
console.log(firebase);

I get

{ default:
  {
    __esModule: true,
    initializeApp:
    { [Function: initializeApp]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function],
      mockRestore: [Function] },
    app:
    { [Function: app]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function],
      mockRestore: [Function],
      App: [Object] },
    apps: [],
    Promise:
    { [Function: Promise]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function],
      mockRestore: [Function],
      _37: null,
      _87: null,
      _61: [Object],
      resolve: [Object],
      all: [Object],
      reject: [Object],
      race: [Object],
      denodeify: [Object],
      nodeify: [Object],
      enableSynchronous: [Object],
      disableSynchronous: [Object] },
    SDK_VERSION: '4.2.0',
    INTERNAL:
    { registerService: [Object],
      createFirebaseNamespace: [Object],
      extendNamespace: [Object],
      createSubscribe: [Object],
      ErrorFactory: [Object],
      removeApp: [Object],
      factories: [Object],
      useAsService: [Object],
      deepExtend: [Object],
      node: [Object],
      Promise: [Object] },
    auth:
    { [Function: serviceNamespace]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function],
      mockRestore: [Function],
      Auth: [Object],
      Error: [Object],
      EmailAuthProvider: [Object],
      FacebookAuthProvider: [Object],
      GithubAuthProvider: [Object],
      GoogleAuthProvider: [Object],
      TwitterAuthProvider: [Object],
      OAuthProvider: [Object],
      PhoneAuthProvider: [Object],
      RecaptchaVerifier: [Object] },
    User:
    { [Function: S]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function],
      mockRestore: [Function],
      Cg: [Object],
      Sc: [Object] },
    database:
    { [Function: serviceNamespace]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function],
      mockRestore: [Function],
      Reference: [Object],
      Query: [Object],
      Database: [Object],
      enableLogging: [Object],
      INTERNAL: [Object],
      ServerValue: [Object],
      TEST_ACCESS: [Object] },
    default: [Circular]
  } 
}

From this i understand that i'm mocking the module as expected.

I have code that does stuff like
contactKey = firebase.database().ref().child(path).push().key;

My tests fail with the following error:

TypeError: firebase.database is not a function

If i do console.log(firebase.default.database) I get

function serviceNamespace() {return mockConstructor.apply(this,arguments);}

If i do console.log(firebase.default.database()) I get

undefined

How am I supposed to be consuming this mock? Output from console.log makes it look like the generated mock is what I expect (more or less) but nothing is callable.

like image 813
w-- Avatar asked Oct 17 '22 08:10

w--


1 Answers

try something like this in your mock -

const push = jest.fn(() => ({ key: 'mockContactKey' }));

const child = jest.fn(() => ({ push }));

const ref = jest.fn(() => ({ child }));

mockFirebase.database = jest.fn(() => ({ ref }));

this is all you need for your contactKey to get mockContactKey

like image 64
grgmo Avatar answered Oct 21 '22 07:10

grgmo