【问题标题】:Mocking Auth0Client in @auth0 in jest开玩笑地在@auth0 中模拟 Auth0Client
【发布时间】:2021-05-14 02:19:38
【问题描述】:

我正在尝试在 react 应用程序中模拟 import { Auth0Provider } from "@auth0/auth0-react"; 并继续遇到此错误 [Error: For security reasons, window.crypto is required to run auth0-spa-js.] 的问题据我所知,此错误来自 Auth0Client,它是从 import { Auth0Client } from '@auth0/auth0-spa-js'; 导入的我的想法是模拟出作用域模块@auth0/auth0-spa-js 并做这样的事情

const handleRedirectCallback = jest.fn(() => ({ appState: {} }));
const buildLogoutUrl = jest.fn();
const buildAuthorizeUrl = jest.fn();
const checkSession = jest.fn();
const getTokenSilently = jest.fn();
const getTokenWithPopup = jest.fn();
const getUser = jest.fn();
const getIdTokenClaims = jest.fn();
const isAuthenticated = jest.fn(() => false);
const loginWithPopup = jest.fn();
const loginWithRedirect = jest.fn();
const logout = jest.fn();

export const Auth0Client = jest.fn(() => {
  return {
    buildAuthorizeUrl,
    buildLogoutUrl,
    checkSession,
    handleRedirectCallback,
    getTokenSilently,
    getTokenWithPopup,
    getUser,
    getIdTokenClaims,
    isAuthenticated,
    loginWithPopup,
    loginWithRedirect,
    logout,
  };
});

如果我将Auth0Client 导入到我的任何测试中,这似乎可行,但问题是Auth0Provider 仍在导入非模拟客户端。有没有办法让Auth0Provider 导入模拟出来的Auth0Client 而不是实际的实现?使用Auth0Provider 的文件如下所示

// test-utils.js
import React from 'react'
import { render as rtlRender } from '@testing-library/react'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import { MockedProvider } from '@apollo/client/testing';
import { Auth0Provider } from "@auth0/auth0-react";
import { Auth0Client } from '@auth0/auth0-spa-js';

// Import your own reducer
import applicationsReducer from "../app/slices/applications.slice";
import { UserProvider } from "../user-context";

import {getUserMock} from "../__tests__/apollo_mocks/get_user.mock"

// const MockedAuth0Provider = jest.requireActual("@auth0/auth0-react").Auth0Provider

function render(
  ui,
  {
    initialState,
    mocks,
    store = createStore(applicationsReducer, initialState),
    ...renderOptions
  } = {}
) {
  function Wrapper({ children }) {
    return <Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
              <Provider store={store}>
                <MockedProvider mocks={[getUserMock, ...mocks]} addTypename={false}>
                  <UserProvider>{children}</UserProvider>
                </MockedProvider>
              </Provider>
            </Auth0Provider>
  }
  return rtlRender(ui, { wrapper: Wrapper, ...renderOptions })
}

// re-export everything
export * from '@testing-library/react'
// override render method
export { render }

【问题讨论】:

    标签: reactjs jestjs auth0


    【解决方案1】:

    希望这对其他人有所帮助,但我最终嘲笑了提供程序以及我以这种方式使用的其他一些 auth0 模块

    jest.mock('@auth0/auth0-react', () => ({
      Auth0Provider: ({ children }) => children,
      withAuthenticationRequired: ((component, _) => component),
      useAuth0: () => {
        return {
          isLoading: false,
          user: { sub: "foobar" },
          isAuthenticated: true,
          loginWithRedirect: jest.fn()
        }
      }
    }));
    

    这些都在我的setupTests.js 文件中

    【讨论】:

    • 我也在为此苦苦挣扎。不明白为什么我要在全球范围内嘲笑 Auth0,但开玩笑并不想接受它。我正在使用你的设置,它就像一个魅力!非常感谢。
    • 谢谢!!我还添加了一个从useAuth0 返回的字段:``` getAccessTokenSilently: jest .fn() .mockImplementation( () => new Promise(resolve => resolve("test-token")) ), ```
    猜你喜欢
    • 2017-07-01
    • 1970-01-01
    • 2023-03-08
    • 2020-03-05
    • 1970-01-01
    • 2019-03-23
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多