跳转到主文档

​测试容器组件

测试组件​​​中一样,不需要做任何配置,可以直接给Model 写测试用例。

containers/Contacts.tsx 为例,我们创建对应的 .test 文件:

touch src/contacts/containers/contacts.test.tsx

在测试用例中可以使用 Modern.js 提供的 API 进行渲染,并通过 API 返回的工具函数进行断言。

测试用例文件的示例:

import { renderApp, waitFor } from '@modern-js/runtime/testing';
import ContactContainer from './Contacts';

describe('test contracts model', () => {
it('actions works well', async () => {
const { getByText } = renderApp(<ContactContainer source="items" />);

await waitFor(() => {
expect(getByText('Pending...')).toBeInTheDocument();
});
});
});

更多相关内容可以查看 Test API

执行 pnpm run test,可以看到测试报告。


本小节的代码可以在这里查看