我正在做一个React-Nextjs项目,并试图集成BDD工具cucumber进行规范和功能级测试。虽然我在使用enzyme进行浅层渲染组件时遇到了一些cucumber和React的集成问题:
下面是我收到的错误:const wrapper = shallow(<Referrer/>);上的TypeError: Cannot read property 'contextTypes' of undefined
cucumber步骤测试文件代码:
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.const wrapper = shallow(<Referrer/>);上的TypeError: Cannot read property 'contextTypes' of undefined
发布于 2017-06-23 05:04:31
我意识到问题出在哪里,我的Referrer组件被默认导出,尽管我没有正确地导入它。我不得不将其导入为import Referrer from "./../../components/Referrer";而不是import {Referrer} from "./../../components/Referrer";
https://stackoverflow.com/questions/44683865
复制相似问题