我的jest测试失败了

zy1mlcev  于 2023-04-27  发布在  Jest
关注(0)|答案(1)|浏览(150)

我有一个相当简单的Facebook登录创建后,按照指南。代码工作正常,在Android和网络,但当我运行我的测试测试,我得到错误。

import React, { useState, useEffect } from 'react';
import { Fontisto } from '@expo/vector-icons';

import * as AuthSession from 'expo-auth-session';
import * as Facebook from 'expo-auth-session/providers/facebook';
import * as WebBrowser from 'expo-web-browser';

import {
  StyledButton,
  ButtonText,
  Colors,
} from './styles.js';

// keyboard avoiding wrapper

// Colors
const { primary } = Colors;

WebBrowser.maybeCompleteAuthSession();

function FacebookLogin({ navigation }) {
  const [user, setUser] = useState(null);
  const [request, response, promptAsync] = Facebook.useAuthRequest({
    clientId: '1264001381219589',
  });

  useEffect(() => {
    if (response && response.type === 'success' && response.authentication) {
      (async () => {
        const userInfoResponse = await fetch(
          `https://graph.facebook.com/me?access_token=${response.authentication.accessToken}&fields=id,name,email,about,picture.type(large),link`,
        );
        const userInfo = await userInfoResponse.json();
        setUser(userInfo);
        console.log(JSON.stringify(response, null, 2));
        console.log(userInfo);
      })();
    } else {
      console.log('something is not working! ', response);
    }
  }, [response]);

  const handlePressAsync = async () => {
    const result = await promptAsync();
    if (result.type !== 'success') {
      alert('Uh oh, something went wrong');
    }
  };

  return (
    <StyledButton facebook onPress={handlePressAsync}>
      <Fontisto name="facebook" color={primary} size={25} />
      <ButtonText facebook>Sign in with Facebook </ButtonText>
    </StyledButton>
  );
}

export default FacebookLogin;

当我在任何测试中呈现这个时,测试都会失败,例如:

import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import * as AuthSession from 'expo-auth-session';
import FacebookLogin from '../../components/FacebookLogin';

describe('FacebookLogin', () => {
  it('renders correctly', () => {
    const tree = render(<FacebookLogin />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});

将返回错误-
expo-linking需要访问expo-constants manifest(app.json或app.config.js)来确定要使用的URI方案。设置manifest并重建:https://github.com/expo/expo/blob/main/packages/expo-constants/README.md
但指南基本上只是告诉我安装expo-constants,它安装在我的package.json中:

"expo-constants": "~14.2.1",

我还应该尝试什么?

qyyhg6bp

qyyhg6bp1#

我至少得到了jest测试来停止级联故障,通过有条件地呈现违规控件...并忽略相关文件。

function ConditionallyRenderFB({ navigation }) {
  if (process.env.JEST_WORKER_ID) {
    return (
      <View />
    );
  }

  return (
    <FacebookLogin navigation={{ navigation }} />
  );
}

如果我真的修复了它,我会尝试更新这个

相关问题