reactjs React测试错误测试完成后无法记录,是否忘记在测试中等待某些异步操作?

wdebmtf2  于 2022-12-22  发布在  React
关注(0)|答案(1)|浏览(87)

我有一个功能组件,我试图测试,似乎有一些问题,围绕端点调用通过axios。

const url = user.myUrl + "/someEndpoint";
const RESPONSE = await axios.post(url);
console.log("RESPONSE ::::::::::::::::::::::::" + RESPONSE);

试验如下:

test("Validate something", async () => {
    const {container} = render(
            <MyComponent url={url} />
    );
    expect(await container.getElementsByClassName('someGrid').length).toBe(2);
});

当我运行测试时,我得到了下面的错误;

Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "RESPONSE ::::::::::::::::::::::::[object Object]".

PS:我正在通过MSW模拟端点。

fnvucqvd

fnvucqvd1#

由于MyComponent在其中有一个影响,即axios POST,您需要等待结果。

test("Validate something", async () => {
    const {container} = render(<MyComponent url={url} />);
    await waitFor(() => expect(container.getElementsByClassName('someGrid').length).toBe(2));
});

(You'如果您尚未使用waitFor,则很可能需要导入它)

相关问题