模拟服务工作者返回空
我是 React测试库的新手。我正在尝试测试一个调用某些API的简单组件 然后,它填充了这些API的数据的输入。但是,当我运行npm test
时,它会一直向我展示以下信息:收到的值是空的,
预期元素具有值: 100 收到:
7 | it('displays returned client info on successful fetch', async () => {
8 | render(<Client />);
> 9 | expect(await screen.findByLabelText('IFU')).toHaveValue('100');
| ^
10 | });
11 |
12 | it('displays error message when fetching tasks raises error', async () => {
at Object.<anonymous> (src/unit-tests/Client.test.js:9:51)
我
import { render, screen } from '@testing-library/react';
import Client from './../components/Client';
import { mswServer } from './api-mocks/msw-server';
import { clientHandlerException } from './api-mocks/handlers';
describe('Component: Client', () => {
it('displays returned client info on successful fetch', async () => {
render(<Client />);
expect(await screen.findByLabelText('IFU')).toHaveValue('100');
});
it('displays error message when fetching tasks raises error', async () => {
mswServer.use(clientHandlerException);
render(<Client />);
});
});
程序
import { rest } from 'msw';
const importateur = {numeroRc: "1000", description: "desc one", ifu: "100", ice: "789", company: "mycom", city: "paris"}
export const clientHandler = rest.get("http://localhost:8080/api/informationsClient", async (req, res, ctx) => {
// res(ctx.json(mockClientInfo))
return res(
ctx.status(200),
ctx.json(importateur)
)
});
export const clientHandlerException = rest.get("http://localhost:8080/api/informationsClient", async (req, res, ctx) =>
res(ctx.status(500), ctx.json({ message: 'Deliberately broken request' }))
);
export const handlers = [clientHandler];
。
import { setupServer } from 'msw/node';
import { handlers } from './handlers';
export const mswServer = setupServer(...handlers);
测试
import '@testing-library/jest-dom';
import { mswServer } from './msw-server';
beforeAll(() => mswServer.listen());
afterEach(() => mswServer.resetHandlers());
afterAll(() => mswServer.close());
的
export interface IClient {
numeroRc: string,
description: string,
ifu: string,
ice: string
}
const Client = (props: ClientProps) => {
const [clientData, setClientData] = React.useState<IClient>(
{numeroRc: "",
description: "",
ifu: "",
ice: ""}
);
//other useEffect s that call other apis
React.useEffect(() => {
(async () => {
const response = await getClientInfo();
setClientData(response as IClient);
})();
}, [])
return(
<Stack direction="row" spacing={3} className='client' >
<TextField id="numro-rc" label="Numero RC" variant="outlined" value={clientData.numeroRc}/>
<TextField id="client" label="Client" variant="outlined" value={clientData.description}/>
<TextField id="ifu" label="IFU" variant="outlined" value={clientData.ifu} />
<TextField title='ice' id="ice" label="ICE" variant="outlined" value={clientData.ice} />
</Stack>
);
}
处理
这
axios.defaults.baseURL = 'http://localhost:8080/api'
...
export const getClientInfo = async () => {
try {
const response = await axios.get(`/informationsClient`);
return response.data;
} catch(e) {
console.log(e);
return 0;
}
}
是 问题 ?
I'm new to react testing library. I am trying to test a simple component that call some apis
and then it fills inputs with the data of these apis. But when I run npm test
it keeps showing me this : the recieved value is empty
Expected the element to have value:
100
Received:
7 | it('displays returned client info on successful fetch', async () => {
8 | render(<Client />);
> 9 | expect(await screen.findByLabelText('IFU')).toHaveValue('100');
| ^
10 | });
11 |
12 | it('displays error message when fetching tasks raises error', async () => {
at Object.<anonymous> (src/unit-tests/Client.test.js:9:51)
here is my test
import { render, screen } from '@testing-library/react';
import Client from './../components/Client';
import { mswServer } from './api-mocks/msw-server';
import { clientHandlerException } from './api-mocks/handlers';
describe('Component: Client', () => {
it('displays returned client info on successful fetch', async () => {
render(<Client />);
expect(await screen.findByLabelText('IFU')).toHaveValue('100');
});
it('displays error message when fetching tasks raises error', async () => {
mswServer.use(clientHandlerException);
render(<Client />);
});
});
handlers.js
import { rest } from 'msw';
const importateur = {numeroRc: "1000", description: "desc one", ifu: "100", ice: "789", company: "mycom", city: "paris"}
export const clientHandler = rest.get("http://localhost:8080/api/informationsClient", async (req, res, ctx) => {
// res(ctx.json(mockClientInfo))
return res(
ctx.status(200),
ctx.json(importateur)
)
});
export const clientHandlerException = rest.get("http://localhost:8080/api/informationsClient", async (req, res, ctx) =>
res(ctx.status(500), ctx.json({ message: 'Deliberately broken request' }))
);
export const handlers = [clientHandler];
msw-server.js
import { setupServer } from 'msw/node';
import { handlers } from './handlers';
export const mswServer = setupServer(...handlers);
setupTests.js
import '@testing-library/jest-dom';
import { mswServer } from './msw-server';
beforeAll(() => mswServer.listen());
afterEach(() => mswServer.resetHandlers());
afterAll(() => mswServer.close());
My component
export interface IClient {
numeroRc: string,
description: string,
ifu: string,
ice: string
}
const Client = (props: ClientProps) => {
const [clientData, setClientData] = React.useState<IClient>(
{numeroRc: "",
description: "",
ifu: "",
ice: ""}
);
//other useEffect s that call other apis
React.useEffect(() => {
(async () => {
const response = await getClientInfo();
setClientData(response as IClient);
})();
}, [])
return(
<Stack direction="row" spacing={3} className='client' >
<TextField id="numro-rc" label="Numero RC" variant="outlined" value={clientData.numeroRc}/>
<TextField id="client" label="Client" variant="outlined" value={clientData.description}/>
<TextField id="ifu" label="IFU" variant="outlined" value={clientData.ifu} />
<TextField title='ice' id="ice" label="ICE" variant="outlined" value={clientData.ice} />
</Stack>
);
}
UPDATE
getClientInfo is a method that call my api using axios ( I put all my apis calls inside a file api.tsx)
axios.defaults.baseURL = 'http://localhost:8080/api'
...
export const getClientInfo = async () => {
try {
const response = await axios.get(`/informationsClient`);
return response.data;
} catch(e) {
console.log(e);
return 0;
}
}
please what it could be the issue ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您不在等待该值,而是元素,并且当组件以
的值安装时,该元素就在那里,因此您的断言失败了。
因此,您需要
等待该值
,可以使用waitfor
对此。您也可以使用
FindbyDisPlayValue
:更新:
进行以下两个更改后,以上两种测试方法都可以正常工作。
您已经创建了
setUptests.ts
api-mocks
具有 no 效果的文件,因为CRA不考虑任何setUptest.ts
文件,但唯一的src
中的一个。您会注意到src
中的文件已经有一个setuptest.ts
,只需将所有设置代码移动到此文件中。模拟服务器返回一个数组,但是您的
clientdata
状态期望对象,因此请执行以下任一项:更新模拟服务器以返回对象而不是数组(在
handlers.js
中)js const client = {numerorc:“ 1000”,描述:“ desc One”,ifu:“ 100”,ICE:“ 789”,Company:“ Mycom”,City:“ Paris”};
或在设置状态之前从数组中提取对象(在
client.tsx
)。您还需要将
onChange
处理程序提供给所有输入,以摆脱警告。The problem is that you're not awaiting for the value but the element, and the element is there when the component is mounted with a value of
""
, hence your assertion fails.So, you need to
await
for the value, you can usewaitFor
for the same.You can also do it using
findByDisplayValue
:UPDATE:
Once you've made the following two changes, both the above methods of testing would work fine.
You've created a
setupTests.ts
file insideapi-mocks
which has no effect because CRA doesn't consider anysetupTest.ts
file but only the one that is there insidesrc
. You would notice there's already asetupTest.ts
file insidesrc
, just move all your setup code into this file.The mock server returns an array but your
clientData
state expects an object, so do either of the following:Update the mock server to return an object instead of an array (in
handlers.js
)js const client = { numeroRc: "1000", description: "desc one", ifu: "100", ice: "789", company: "mycom", city: "paris" };
Or extract the object from the array before setting the state (in
Client.tsx
).You also need to give an
onChange
handler to all your inputs to get rid of the warnings.