VUE:如何模拟Auth0进行vitest测试
我正在尝试使用Vitest测试VUE
组件未定义(读取'oigreturnvalue'作为useauth0
,即使我在页面顶部将其导入了。也许我只是不太了解它的模拟面但是任何帮助将不胜感激。
import { vi } from 'vitest'
import { ref } from "vue";
import { shallowMount } from '@vue/test-utils'
import { useAuth0 } from '@auth0/auth0-vue';
import NavBar from "@/components/NavBar.vue";
const user = {
email: "[email protected]",
email_verified: true,
sub: ""
};
vi.mock("@auth0/auth0-vue");
const mockedUseAuth0 = vi.mocked(useAuth0, true);
describe("NavBar.vue", () => {
beforeEach(() => {
mockedUseAuth0.mockReturnValue({
isAuthenticated: ref(true),
user: ref(user),
logout: vi.fn(),
loginWithRedirect: vi.fn(),
...
isLoading: ref(false),
});
});
it("mounts", () => {
const wrapper = shallowMount(NavBar, {
props: { },
});
expect(wrapper).toBeTruthy();
});
afterEach(() => vi.clearAllMocks());
});
I am trying to test a Vue component with Vitest but in order to do that I need to mock auth0
Below is my Navbar.test.ts file, however when I run the test I keep getting the following error Cannot read properties of undefined (reading 'mockReturnValue'
as useAuth0
seems to be undefined even though I am imported it at the top of the page. Maybe I'm just not understanding the mocking side of it very well but any help would be appreciated.
import { vi } from 'vitest'
import { ref } from "vue";
import { shallowMount } from '@vue/test-utils'
import { useAuth0 } from '@auth0/auth0-vue';
import NavBar from "@/components/NavBar.vue";
const user = {
email: "[email protected]",
email_verified: true,
sub: ""
};
vi.mock("@auth0/auth0-vue");
const mockedUseAuth0 = vi.mocked(useAuth0, true);
describe("NavBar.vue", () => {
beforeEach(() => {
mockedUseAuth0.mockReturnValue({
isAuthenticated: ref(true),
user: ref(user),
logout: vi.fn(),
loginWithRedirect: vi.fn(),
...
isLoading: ref(false),
});
});
it("mounts", () => {
const wrapper = shallowMount(NavBar, {
props: { },
});
expect(wrapper).toBeTruthy();
});
afterEach(() => vi.clearAllMocks());
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模拟
auth0.useauth0
要访问模块的模块,以通配符导入导入整个模块,并避免命名导入。另外,
vi.mocked()
支持。它没有模拟任何东西。而不是这样
:
,
Mocking
auth0.useAuth0
To access the mocked module, import the whole module with a wildcard import, and avoid named imports. Also,
vi.mocked()
is just a helper for TypeScript support. It doesn't mock anything.So instead of this:
...do this:
Then, mock
useAuth0
by attaching a property directly to the mockedauth0
import:The value of
useAuth0
should be a mock function (vi.fn()
) that returns the fields relevant to the test. Assuming the component under test isNavBar.vue
from Auth0's sample app, one could write a test to verify the login button callsloginWithRedirect
. That button is only available whenisAuthenticated
andisLoading
are false. Note the fields do not need to be Vueref
s.So such a test could look like this:
Mocking
useAuth().user
One could also write a test to verify the authenticated user's details are shown in the component. Specifically, the user's name is shown in
.user-info
, and the user's picture is shown in.user-info img
whenisAuthenticated
is true. The mockeduseAuth0
should thus returnisAuthenticated
anduser
as follows:demo