Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当父方法在JEST中抛出错误时,期望子方法抛出错误 在方法注册中,我在模仿 firebase.auth().createUserWithEmailAndPassword并使其抛出 错误 因此,现在我希望父方法,即signup也应该抛出,因为signup方法的子方法抛出了一个错误 但是测试用例失败了,我在这里遗漏了什么_Javascript_Typescript_Unit Testing_Firebase Authentication_Jestjs - Fatal编程技术网

Javascript 当父方法在JEST中抛出错误时,期望子方法抛出错误 在方法注册中,我在模仿 firebase.auth().createUserWithEmailAndPassword并使其抛出 错误 因此,现在我希望父方法,即signup也应该抛出,因为signup方法的子方法抛出了一个错误 但是测试用例失败了,我在这里遗漏了什么

Javascript 当父方法在JEST中抛出错误时,期望子方法抛出错误 在方法注册中,我在模仿 firebase.auth().createUserWithEmailAndPassword并使其抛出 错误 因此,现在我希望父方法,即signup也应该抛出,因为signup方法的子方法抛出了一个错误 但是测试用例失败了,我在这里遗漏了什么,javascript,typescript,unit-testing,firebase-authentication,jestjs,Javascript,Typescript,Unit Testing,Firebase Authentication,Jestjs,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

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
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
    })
应用规范ts

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
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 |   })


这有几个问题:

expect(await myAuthenticationPlugin.signup).toThrow() // THIS FAILS
首先,传递给
expect
的参数必须是一个函数,因此这几乎是正确的:

…但它也不起作用。这是因为
.toThrow()
函数用于检测同步错误抛出,而不是检查拒绝的承诺。您将希望使用“告诉Jest”来打开承诺:

await expect(myAuthenticationPlugin.signup()).rejects.toThrow()

感谢您分享您的专业知识,极大地帮助了我,解决了我的问题,并帮助我了解jest如何在内部工作:)非常感谢Jacob@Phil FYI您还可以检查错误消息本身,如:test('checks the newUserCreated function throws',async()=>{//Create a Database snap const snap=testEnv.Database.makeDataSnapshot({id:testUserId,firstName:'Dan Pralea',testUserPath);wait expect(wrapped(snap)).rejects.TothRowerr('无法读取未定义的属性'make\');});