Надеюсь, у вас все хорошо получается :) У меня ошибка в useContext при запуске теста рендеринга в приложении React в компоненте App.
Вот API контекста:
const {userId} = useContext (UserContext);
- # P4 # # P5 #
# P6 #
Вначале объект не определен, потому что это вызов API для получения userId. После входа в систему становится доступен userId. Так что при запуске в разработке он работает отлично, но при запуске тестов показывает эту ошибку.
Наконец, вот компонент UserContext:
экспорт const UserContext = createContext ();
const UserProvider = ({ children }) => { const [userId, setUserId] = useState(""); const getUserId = async () => { if (token) { await axios .get(`${baseURL}/auth/user`, { headers: { authorization: token } }) .then(function (response) { const { data } = response; return setUserId(data.userId); }) .catch(function (err) { if (err.response) return toast.error( <div> <InfoIcon style={{ marginBottom: "6px" }} /> <p> getUserId - {err.response.status} - {err.response.message} </p> </div> ); return toast.error( <div> <InfoIcon style={{ marginBottom: "6px" }} /> <p>User Context - CORS</p> </div> ); }); } }; useEffect(() => { getUserId(); }, []); return ( <UserContext.Provider value={{ userId }}>{children}</UserContext.Provider> ); }; export default UserProvider;