我正在尝试使用 Jest 和 Typescript 测试我的 LoginScreen。我使用 redux 和 redux-persist 进行存储,并将存储设置为使用 AsyncStorage 作为配置的一部分。我怀疑 redux-persist 试图在它使用的内置超时功能用完并尝试将存储设置为默认存储后重新水合?我收到以下错误:
console.error
redux-persist: rehydrate for "root" called after timeout. undefined
undefined
at _rehydrate (node_modules/redux-persist/lib/persistReducer.js:70:71)
at node_modules/redux-persist/lib/persistReducer.js:102:11
at tryCallOne (node_modules/promise/setimmediate/core.js:37:12)
at Immediate._onImmediate (node_modules/promise/setimmediate/core.js:123:15)
Run Code Online (Sandbox Code Playgroud)
目前我的测试是这样的:
describe('Testing LoginScreen', () => {
it('should render correctly', async () => {
const { toJSON } = render(<MockedNavigator component={LoginScreen} />);
await act(async () => await flushMicrotasksQueue());
expect(toJSON()).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
我的 MockNavigator 看起来像这样:
type MockedNavigatorProps = {
component: React.ComponentType<any>;
params?: {};
};
const Stack = …Run Code Online (Sandbox Code Playgroud) jestjs react-native redux-persist react-native-testing-library
我正在运行一个 React Native 项目,在其中测试用 React-query 编写的自定义钩子。我正在使用 Jest、@testing-library/react-hooks 和 @testing-library/react-native。在我的测试中,我似乎找不到调用钩子返回的 mutate 函数的方法。
看一下自定义钩子:
export default function useAuthentication() {
const { mutate, data, isSuccess, isError } = useMutation(
authenticationApi.authenticateUser
);
return { mutate, data, isSuccess, isError };
}
Run Code Online (Sandbox Code Playgroud)
根据react-query的文档,我使用 renderHook() 渲染钩子并等待突变调用的结果:
const authBody: AuthBody = {
username: '111111',
password: '111111',
};
describe('Authentication Hook', () => {
const sanityCall = () =>
axios.post('http://localhost:4000/auth/authenticate');
console.log(sanityCall());
const queryClient = new QueryClient();
it('Gets authentication data', async () => {
const wrapper = ({ children }: any) …Run Code Online (Sandbox Code Playgroud) jestjs react-native react-hooks react-native-testing-library react-query