Within the file App.ts
, I am utilizing the method
firebase.auth().signInWithEmailAndPassword(email, password)
.
Now, my objective is to conduct a unit test to ensure that when the
myAuthenticationPlugin.authenticate(email, password)
method is invoked from App.spec.ts
, it triggers the firebase.auth().signInWithEmailAndPassword(email, password)
method as this is the core functionality of App.ts
.
Despite several attempts, I have been unable to find a solution.
App.ts
const App= {
authenticate: async (email, password) => {
await firebase.auth().signInWithEmailAndPassword(email, password)
},
}
App.spec.ts
import myAuthenticationPlugin from 'authenticationPlugin/App'
import firebase from 'firebase/app'
jest.mock('firebase/app', () => {
const firebase = {
auth: jest.fn(() => {
return {
currentUser: {
email: 'test',
uid: '123',
emailVerified: true
},
signInWithEmailAndPassword: jest.fn().mockImplementation()
}
}),
initializeApp: jest.fn()
}
return firebase
})
describe('Test for authenticate ()', () => {
it('signInWithEmailAndPassword ()', () => {
const email = 'test'
const password = 'mypassword'
myAuthenticationPlugin.authenticate(email, password)
expect(firebase.auth().signInWithEmailAndPassword).toHaveBeenCalled()
})
})
Error Received
● App.js (Authentication Plugin) › Test for authenticate () › signInWithEmailAndPassword ()
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
44 | const password = 'mypassword'
45 | myAuthenticationPlugin.authenticate(email, password)
> 46 | expect(firebase.auth().signInWithEmailAndPassword).toHaveBeenCalled()
| ^
47 | })
48 | })
49 | })
at Object.<anonymous> (tests/unit/App.spec.ts:46:58)