【发布时间】:2022-01-13 23:32:20
【问题描述】:
当我进行纱线测试时,结果会不断变化。
这是错误名称 "请求失败,状态码 400"
但是我已经 Api 的请求函数已经在模拟了
喜欢这个
import * as api from '@api/user';
jest.mock('@api/user');
(api.fetchApiKeyUpdate as jest.Mock).mockImplementationOnce(() => {
throw { response: { data: { code: 'EX_INVALID_APIKEY_2015' } } };
});
(api.fetchApiKeyUpdate as jest.Mock).mockImplementationOnce(() => ({
status: 'success',
user: {
id: '99b1231230',
country: 'KR',
language: 'ko',
},
}));
由此进行的测试正在通过。但是测试经常失败。我想知道我应该怀疑什么。
export const fetchApiKeyUpdate = async ({
exchange,
apiKey,
secretKey,
passphrase,
otpCode,
}: ApiKeyUpdateRequest): Promise<UserInfoUpdateResponse | ApiAuthResponse> => {
const { data } = await axios.post(
apiKeyUpdateUrl,
{ apiKey, secretKey, passphrase, otpCode },
{ headers: { exchange } },
);
return data;
};
底部是我修改的部分代码。
jest.mock('@api/user');
describe('API Register Success', () => {
const mockResponse = {
status: 'success',
user: {
id: '99bd10e123400',
userName: 't123st07',
receiveMarketingInfo: true,
},
};
beforeEach(() => {
(api.fetchApiKeyUpdate as jest.Mock).mockResolvedValueOnce(mockResponse);
});
it('키인증 성공시 아이콘 변경', async () => {
const { container } = render(
<ApiRegistrationBinanceTab
isOpen
handleOpenTab={jest.fn()}
/>,
);
userEvent.type(screen.getByPlaceholderText(/api key/i), 'apikey');
userEvent.click(screen.getByRole('button', { name: /Verify/i }));
await waitFor(() => {
expect(container.querySelector('#certified-icon')).toBeTruthy();
});
});
});
describe('API Register Fail', () => {
const mockResponse = { response: { data: { code: 'EX_INVALID_APIKEY_2015' } } };
beforeEach(() => {
(api.fetchApiKeyUpdate as jest.Mock).mockRejectedValueOnce(mockResponse);
});
it('remove input value if error code EX_INVALID_APIKEY_2015 or API_MANAGEMENT_ALREADY_REGISTERED', async () => {
render(
<ApiRegistrationBinanceTab
isOpen
handleOpenTab={jest.fn()}
/>,
);
userEvent.type(screen.getByPlaceholderText(/api key/i), 'apikey');
userEvent.click(screen.getByRole('button', { name: /Verify/i }));
await waitFor(() => {
expect(screen.getByPlaceholderText(/api key/i)).toHaveValue('');
});
});
});
FAIL src/components/articles/modal/custom/forgotPassword/ForgotPassword.spec.tsx
● 비밀번호 변경 스텝별로 진행
Request failed with status code 400
at createError (node_modules/axios/lib/core/createError.js:16:15)
at settle (node_modules/axios/lib/core/settle.js:17:12)
at XMLHttpRequest.handleLoad (node_modules/axios/lib/adapters/xhr.js:62:7)
at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:32:32)
at innerInvokeEventListeners ...
我应用 beforeEach 并更改 mockimplementationOnce > mockResolvedValueOnce, mockRejectedValueOnce
但问题是失败会随着页面(测试)的变化而间歇性出现。
我对错误消息感到好奇的是......我在请求中模拟了一个函数(如 fetchAuthMail.. fetchApiKeyUpdate)。
所以我不知道为什么错误消息来自 axios,即使我认为我实际上并没有在测试中提出请求。
【问题讨论】:
-
你是如何使用
api.fetchApiKeyUpdate的? -
userEvent.type(screen.getByPlaceholderText(/输入密码/i), 'password'); userEvent.click(screen.getByRole('button', { name: /Verify/i })); >>fetchapikey更新
-
await waitFor(() => { expect(screen.getByPlaceholderText(/api key/i)).toHaveValue(''); });
-
有时测试全部通过并发出警告 UnhandledPromiseRejectionWarning:错误:请求失败,状态码为 400,UnhandledPromiseRejectionWarning:未处理的承诺拒绝
标签: typescript jestjs mocking ts-jest testing-library