【发布时间】:2020-04-28 18:40:45
【问题描述】:
- 在方法
signup里面,我在嘲笑firebase.auth().createUserWithEmailAndPassword并让它抛出 一个error - 所以现在我希望父方法,即
signup也应该抛出,因为signup方法的sub-method已经抛出错误 - 但是测试用例失败了,我在这里缺少什么?
App.js
import firebase from 'firebase/app'
import 'firebase/auth'
import './Init'
const App = {
firebase: firebase,
signup: async (email, password) => {
const userCredential = await App.firebase.auth().createUserWithEmailAndPassword(email, password)
await userCredential.user.sendEmailVerification()
return `Check your email for verification mail before logging in`
},
export default App
App.spec.ts
import myAuthenticationPlugin from 'authenticationPlugin/App'
it('to Throw',async ()=>{
myAuthenticationPlugin.firebase = {
auth: jest.fn().mockReturnThis(),
createUserWithEmailAndPassword: jest.fn(() => {
throw new Error('Network Error')
}),
}
expect(myAuthenticationPlugin.firebase.auth().createUserWithEmailAndPassword).toThrowError('Network Error')
expect(await myAuthenticationPlugin.signup).toThrow() // THIS FAILS
})
错误
expect(received).toThrow()
Received function did not throw
107 | expect(myAuthenticationPlugin.firebase.auth().createUserWithEmailAndPassword).toThrowError('Network Error')
> 108 | expect(await myAuthenticationPlugin.signup).toThrow()
| ^
109 |
110 | })
111 | })
【问题讨论】:
标签: javascript typescript unit-testing firebase-authentication jestjs