最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest: How to consume result of jest.genMockFromModule - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Aug 5, 2017 at 11:23 w--w-- 6,69714 gold badges60 silver badges93 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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

You just need to get the default of what is returned from genMockFromModule.

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

The documentation explains this very well.

I believe you need to access the prototype first like so:

firebase.prototype.database()

发布评论

评论列表(0)

  1. 暂无评论