【发布时间】:2021-05-11 07:06:33
【问题描述】:
设置
我在我的一个组件中使用了一个名为 react-firebase-hooks 的库。
// Component.tsx
import React, {useEffect} from 'react';
import { Firebase } from '../firebase/firebase';
import { useAuthState } from 'react-firebase-hooks/auth';
export const Component = () => {
// Note that Firebase.getAuth is a static method of a class of mine for getting the firebase authentication object
const [user, loading ] = useAuthState(Firebase.getAuth());
if (!loading && !user) {
// ... stuff
}
return (
<div>
...stuff
</div>
);
};
一种有效的方式
我试图在我的一项测试中模拟出useAuthState 调用,但在找出执行模拟的最佳方法时遇到了很多麻烦。我找到了一个可行的解决方案:
// Component.test.tsx
import {useAuthState} from 'react-firebase-hooks/auth'
jest.mock('react-firebase-hooks/auth', () => ({
useAuthState: jest.fn()
}));
但是,上面的模拟实现使得模拟对象无法在 IDE 中完成。例如,这是我尝试设置一个模拟返回值:
test(`component correctly loads`, () => {
useAuthState.mockReturnValue([true, false])
// renderApp is a utility method of mine to render using react-testing-library
const { getByText } = renderApp();
// expect some stuff
});
我的首选结果(和尝试 1)
我发现useAuthState 没有自动完成功能。
理想情况下,我希望声明模拟的方式更接近
const mockAuth = jest.fn();
jest.mock('react-firebase-hooks/auth', () => {
return jest.fn().mockImplementation(() => {
return {useAuthState: mockAuth}
})
});
然后让我通过我声明的测试(并获得自动完成)很好地改变 mockAuth 对象,例如:
test(`component correctly loads`, () => {
mockAuth.mockReturnValue([true, false])
// renderApp is a utility method of mine to render using react-testing-library which renders Component
const { getByText } = renderApp();
// expect some stuff
});
但是,通过上述实现,我收到以下错误:(0 , _auth.useAuthState) is not a function or its return value is not iterable in Component。
尝试 2
我也尝试过将模拟声明为
const mockAuth = jest.fn();
jest.mock('react-firebase-hooks/auth', () => ({
useAuthState: mockAuth
}));
但这给了我ReferenceError: Cannot access 'mockAuth' before initialization的错误。
尝试 3
我也试过
const mockAuth = jest.fn();
import {useAuthState} from 'react-firebase-hooks/auth'
jest.mock('react-firebase-hooks/auth', () => ({
useAuthState: mockAuth
}));
但这给出了同样的错误。
结束问题
有没有办法让我在模拟声明的“外部”声明一个模拟函数,以便我可以自动完成它?
【问题讨论】:
-
我看不出这如何有助于在模块模拟声明之外声明模拟函数。
-
你能做codesanbox吗?
-
@DaniilLoban CodeSandbox doesn't support jest mocking。我必须以我认为你想知道的艰难方式弄清楚这一点。
-
我只需要最小的安装项目,而不是在 sunbox 中运行
标签: reactjs typescript firebase jestjs