我正在编写Storybook教程,但使用Material-UI作为UI框架。
我已经完成了可以使用Task和TaskList组件的阶段。我开始使用Jest和enzyme编写测试,这就是事情开始动摇的地方。
我的测试看起来像
import React from "react";
import ReactDOM from "react-dom";
import TaskList from "./TaskList";
import { withPinnedTasks } from "../../stories/TaskList.stories";
import { MuiThemeProvider } from "@material-ui/core";
import { theme } from "../theme";
import { createMount } from "@material-ui/core/test-utils";
describe("SearchField", () => {
let mount;
beforeEach(() => {
mount = createMount();
});
afterEach(() => {
mount.cleanUp();
});
it("renders pinned tasks at the start of the list", () => {
const div = document.createElement("div");
const events = { onPinTask: jest.fn(), onArchiveTask: jest.fn() };
let wrapper = mount(
<MuiThemeProvider theme={theme}>
<TaskList tasks={withPinnedTasks} {...events} />
</MuiThemeProvider>
);
console.log(wrapper);
// We expect the task titled "Task 6 (pinned)" to be rendered first, not at the end
const lastTaskInput = wrapper.find('#id').to.have.lengthOf(1);
// querySelector(
// '.list-item:nth-child(1) input[value="Task 6 (pinned)"]'
// );
expect(lastTaskInput).not.toBe(null);
ReactDOM.unmountComponentAtNode(div);
});
});当我运行yarn test时,它会失败,并显示以下错误
● SearchField › renders pinned tasks at the start of the list
TypeError: Cannot read property 'have' of undefined
30 |
31 | // We expect the task titled "Task 6 (pinned)" to be rendered first, not at the end
> 32 | const lastTaskInput = wrapper.find('#id').to.have.lengthOf(1);
| ^
33 |
34 | // querySelector(
35 | // '.list-item:nth-child(1) input[value="Task 6 (pinned)"]'
at Object.it (src/components/TaskList.test.js:32:27)整个代码库可以在https://codesandbox.io/s/github/hhimanshu/storyboook-materialui-react/tree/jest上找到
但由于这是测试中的一个失败,如果您正在寻找直接的github链接,您可以在https://github.com/hhimanshu/storyboook-materialui-react/tree/jest上找到此链接
我正在寻求以下方面的帮助
Jest和enzyme进行测试,我很想知道我需要学习MuiThemeProvider测试Material-UI自定义主题组件。谢谢
发布于 2019-08-23 02:41:54
我不认为.to.have是正确的jest语法,我相信酶文档采用了不同的测试框架?在这里查看jest expect方法调用中可以调用的内容
https://jestjs.io/docs/en/expect
看起来你需要像这样的东西
const lastTaskInput = expect(wrapper.find('#id').length).toEqual(1);
具体从这里开始阅读。
https://stackoverflow.com/questions/57548288
复制相似问题