期望模拟库函数被调用
Expect mocked library function to have been called
我正在以这种方式模拟库 (auth0) 中的一个函数:
jest.mock('@auth0/auth0-react', () => ({
useAuth0: () => {
return {
logout: jest.fn(),
}
}
}));
在我的测试过程中,我怎么能期望注销功能被调用?
由于您尝试手动 mock a node_module,您需要创建一个模拟模块文件。手动模拟是通过在模块紧邻的 mocks/ 子目录中编写模块来定义的。在你的情况下,模拟应该放在 __mocks__
目录中 node_modules
.
旁边
root/__mocks__
/@auth0/auth0-react.js
'use strict';
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,
};
});
test.ts(如果你用打字稿写测试)
import { mocked } from 'ts-jest/utils';
const clientMock = mocked(new Auth0Client({ client_id: '', domain: '' }));
describe('auth0 test', () => {
it('should check logout is called', async () => {
await my_logout_function();
expect(clientMock.logout).toHaveBeenCalled();
});
});
test.js(如果您在 javascript 中编写测试)
const clientMock = new Auth0Client({ client_id: '', domain: '' });
describe('auth0 test', () => {
it('should check logout is called', async () => {
await my_logout_function();
expect(clientMock.logout).toHaveBeenCalled();
});
});
我正在以这种方式模拟库 (auth0) 中的一个函数:
jest.mock('@auth0/auth0-react', () => ({
useAuth0: () => {
return {
logout: jest.fn(),
}
}
}));
在我的测试过程中,我怎么能期望注销功能被调用?
由于您尝试手动 mock a node_module,您需要创建一个模拟模块文件。手动模拟是通过在模块紧邻的 mocks/ 子目录中编写模块来定义的。在你的情况下,模拟应该放在 __mocks__
目录中 node_modules
.
root/__mocks__
/@auth0/auth0-react.js
'use strict';
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,
};
});
test.ts(如果你用打字稿写测试)
import { mocked } from 'ts-jest/utils';
const clientMock = mocked(new Auth0Client({ client_id: '', domain: '' }));
describe('auth0 test', () => {
it('should check logout is called', async () => {
await my_logout_function();
expect(clientMock.logout).toHaveBeenCalled();
});
});
test.js(如果您在 javascript 中编写测试)
const clientMock = new Auth0Client({ client_id: '', domain: '' });
describe('auth0 test', () => {
it('should check logout is called', async () => {
await my_logout_function();
expect(clientMock.logout).toHaveBeenCalled();
});
});