gpt4 book ai didi

javascript - 如何模拟 firebase.auth.UserCredential Jest ?

转载 作者:行者123 更新时间:2023-12-03 08:46:09 25 4
gpt4 key购买 nike

这是我当前的模拟,我希望createUserWithEmailAndPassword返回firebase.auth.UserCredential,以便我可以测试它是否在我的应用程序中被调用。 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

最佳答案

这是单元测试解决方案:

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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2b362f233e222b0e29232f2722602d2123" rel="noreferrer noopener nofollow">[email protected]</a>';
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

关于javascript - 如何模拟 firebase.auth.UserCredential Jest ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61391590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com