gpt4 book ai didi

javascript - 如何单元测试并检查函数是否在 Jest 中调用预期的 firebase 方法?

转载 作者:行者123 更新时间:2023-12-04 10:10:28 25 4
gpt4 key购买 nike

App.ts我打电话firebase.auth().signInWithEmailAndPassword(email, password)方法

现在我想进行单元测试以检查何时 myAuthenticationPlugin.authenticate(email, password)方法是从 App.spec.ts 调用的它还调用 firebase.auth().signInWithEmailAndPassword(email, password)方法因为那是App.ts基本上是..

我尝试了多种变通方法,但无济于事。

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

我收到错误
● 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)

最佳答案

这是uni测试解决方案:
app.ts :

import firebase from 'firebase/app';

const App = {
authenticate: async (email, password) => {
await firebase.auth().signInWithEmailAndPassword(email, password);
},
};

export default App;
app.test.ts :

import App from './app';
import firebase from 'firebase/app';

jest.mock('firebase/app', () => {
return {
auth: jest.fn().mockReturnThis(),
signInWithEmailAndPassword: jest.fn(),
};
});

describe('61352544', () => {
it('should pass', async () => {
const email = 'example@gmail.com';
const password = '123';
await App.authenticate(email, password);
expect(firebase.auth().signInWithEmailAndPassword).toBeCalledWith(email, password);
});
});

100% 覆盖率的单元测试结果:

 PASS  stackoverflow/61352544/app.test.ts (12.122s)
61352544
✓ should pass (9ms)

----------|---------|----------|---------|---------|-------------------
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.061s

源代码: https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/61352544

关于javascript - 如何单元测试并检查函数是否在 Jest 中调用预期的 firebase 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61352544/

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