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.
答案 0 :(得分:2)
在inputMax
-
Interlocked
这是您mock
获取const push = jest.fn(() => ({ key: 'mockContactKey' }));
const child = jest.fn(() => ({ push }));
const ref = jest.fn(() => ({ child }));
mockFirebase.database = jest.fn(() => ({ ref }));
答案 1 :(得分:0)
我相信您需要像这样首先访问prototype
:
firebase.prototype.database()
答案 2 :(得分:0)
您只需要获取genMockFromModule
返回的默认值即可。
const mockFirebase = jest.genMockFromModule('firebase').default;
export default mockFirebase;
documentation对此进行了很好的解释。