w--*_*w-- 6 javascript unit-testing jestjs
我正在尝试模拟 firebase 模块
在我的__mocks__/firebase.js文件中我只是简单地输入
const mockFirebase = jest.genMockFromModule('firebase');
export default mockFirebase;
Run Code Online (Sandbox Code Playgroud)
在我的代码中如果我这样做
import * as firebase from 'firebase';
console.log(firebase);
Run Code Online (Sandbox Code Playgroud)
我明白了
{ 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]
}
}
Run Code Online (Sandbox Code Playgroud)
由此我了解到我正在按预期嘲笑该模块。
我有类似的代码
contactKey = firebase.database().ref().child(path).push().key;
我的测试失败并出现以下错误:
TypeError: firebase.database is not a function
Run Code Online (Sandbox Code Playgroud)
如果我这样做console.log(firebase.default.database)我得到
function serviceNamespace() {return mockConstructor.apply(this,arguments);}
Run Code Online (Sandbox Code Playgroud)
如果我这样做console.log(firebase.default.database())我得到
undefined
Run Code Online (Sandbox Code Playgroud)
我应该如何消费这个模拟?console.log 的输出使得生成的模拟看起来像是我所期望的(或多或少),但没有任何东西是可调用的。
mock在你的-中尝试这样的事情
const push = jest.fn(() => ({ key: 'mockContactKey' }));
const child = jest.fn(() => ({ push }));
const ref = jest.fn(() => ({ child }));
mockFirebase.database = jest.fn(() => ({ ref }));
Run Code Online (Sandbox Code Playgroud)
这就是你所需要contactKey的mockContactKey
| 归档时间: |
|
| 查看次数: |
10057 次 |
| 最近记录: |