有谁知道为什么这不起作用?
我的api电话:
import firebase from 'react-native-firebase';
export default function getAuth() {
return new Promise((resolve) => {
firebase.auth().onAuthStateChanged((user) => {
resolve(user);
});
});
}
我的单元测试失败,"预期有一个断言要被调用但是接到零断言调用"。
import firebase from 'react-native-firebase';
import getAuth from '../'; // eslint-disable-line
jest.mock('react-native-firebase', () => {
return {
auth: () => {
return {
onAuthStateChanged: () => {
return Promise.resolve({
name: 'Shaun',
});
},
};
},
};
});
it('resolves a promise containing the user', async () => {
expect.assertions(1);
const response = await getAuth();
expect(response).toEqual({ name: 'Shaun' });
});
答案 0 :(得分:0)
onAuthStateChanged
是一个回调函数相反,用此替换onAuthStateChanged
的工具:
onAuthStateChanged: jest.fn(cb => cb({ name: 'Shaun' }));