我正在做一个React-Nextjs项目,并试图集成BDD工具Cucumber进行规范和功能级测试。尽管在使用 enzyme
浅表渲染组件时,我在将cucumber
与 React 集成时遇到了一些问题:
这是我得到的错误: TypeError: Cannot read property 'contextTypes' of undefined
const wrapper = shallow(<Referrer/>);
黄瓜步骤测试文件的代码:
import React from 'react';
import { defineSupportCode } from "cucumber";
import { shallow } from "enzyme";
import {Referrer} from "./../../components/Referrer";
defineSupportCode(({ Given, When, Then }) => {
Given("I want to do something", function (callback) {
// Write code here that turns the phrase above into concrete actions
callback();
});
When("Xyz link is clicked", function (callback) {
const wrapper = shallow(<Referrer/>);
... // Write code here that turns the phrase above into concrete actions
});
Then("Appropriate action happens", function (callback) {
// Write code here that turns the phrase above into concrete actions
callback();
});
});
该组件是一个简单的UI组件,非常简单,这是它的结构:
import React from "react"; // eslint-disable-line no-unused-vars
export default class Referrer extends React.Component {
render () {
return (
<div className="some-class" id="some-id">
// html tags
<style jsx>{`
.some-class {
//styling
}
.some-class2 {
//styling
}
`}
</style>
</div>
);
}
}
我正在使用"cucumber": "^2.3.1"
,"enzyme": "^2.6.0"
,我不确定如何解决这个问题,到目前为止在线没有任何帮助,我一直在尝试调试过去几个小时但没有运气。
确切的错误片段:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.
TypeError: Cannot read property 'contextTypes' of undefined
在const wrapper = shallow(<Referrer/>);
我意识到出了什么问题,我的Referrer
组件被默认导出,尽管我没有正确导入它。我必须将其作为import Referrer from "./../../components/Referrer";
而不是import {Referrer} from "./../../components/Referrer";
导入