这是我当前的模拟,我希望createUserWithEmailAndPassword
返回firebase.auth.UserCredential
,以便我可以测试其是否在我的App.ts
中被调用
App.spec.ts
import myAuthenticationPlugin from 'authenticationPlugin/App'
import firebase from 'firebase/app'
jest.mock('firebase/app', () => {
return {
auth: jest.fn().mockReturnThis(),
currentUser: {
email: 'test',
uid: '123',
emailVerified: true
},
signInWithEmailAndPassword: jest.fn(),
createUserWithEmailAndPassword:jest.fn(() => {
return {
user:{
sendEmailVerification:jest.fn(),
},
}
}),
initializeApp:jest.fn()
};
});
describe('Test for signup (email,password)',() => {
it('createUserWithEmailAndPassword ()',async () => { //this works
await myAuthenticationPlugin.signup(email, password)
expect(firebase.auth().createUserWithEmailAndPassword).toBeCalledWith(email, password)
})
it('sendEmailVerification()',async ()=>{
await myAuthenticationPlugin.signup(email, password)
const userCredential= await firebase.auth().createUserWithEmailAndPassword(email,password)
if(userCredential.user!=null){
expect(userCredential.user.sendEmailVerification).toBeCalled() //this fails as i'm not able to mock properly
}
})
})
App.ts
import firebase from 'firebase/app'
import 'firebase/auth'
import './Init'
const App= {
signup: async (email, password) => {
const userCredential = await firebase.auth().createUserWithEmailAndPassword(email, password)
await userCredential.user.sendEmailVerification()
return `Check your email for verification mail before logging in`
},
}
export default App
答案 0 :(得分:3)
这是单元测试解决方案:
app.ts
:
import firebase from 'firebase/app';
const App = {
signup: async (email, password) => {
const userCredential = await firebase.auth().createUserWithEmailAndPassword(email, password);
await userCredential.user!.sendEmailVerification();
return `Check your email for verification mail before logging in`;
},
};
export default App;
app.test.ts
:
import App from './app';
import firebase from 'firebase/app';
const userCredentialMock = {
user: {
sendEmailVerification: jest.fn(),
},
};
jest.mock('firebase/app', () => {
return {
auth: jest.fn().mockReturnThis(),
createUserWithEmailAndPassword: jest.fn(() => userCredentialMock),
};
});
describe('61391590', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', async () => {
const email = 'example@gmail.com';
const password = '123';
const actual = await App.signup(email, password);
expect(actual).toEqual('Check your email for verification mail before logging in');
expect(firebase.auth().createUserWithEmailAndPassword).toBeCalledWith(email, password);
expect(userCredentialMock.user?.sendEmailVerification).toBeCalled();
});
});
具有100%覆盖率的单元测试结果:
PASS stackoverflow/61391590/app.test.ts (12.946s)
61391590
✓ should pass (7ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
app.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.863s
源代码:https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/61391590