reactjs - React 与 cucumberjs 集成

标签 reactjs webpack cucumber enzyme next.js

我正在开发一个 React-Nextjs 项目并尝试集成 BDD 工具 cucumber用于规范和功能级别测试。虽然我在集成时遇到一些麻烦cucumber使用 enzyme 时与 React浅层渲染组件:

这是出现的错误: TypeError: Cannot read property 'contextTypes' of undefinedconst wrapper = shallow(<Referrer/>);

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.

TypeError: Cannot read property 'contextTypes' of undefinedconst wrapper = shallow(<Referrer/>);

最佳答案

我意识到出了什么问题,我的 Referrer 组件正在默认导出,尽管我没有正确导入它。我必须将其导入为 import Referrer from "./../../components/Referrer"; 而不是 import {Referrer} from "./../../components/Referrer";

关于reactjs - React 与 cucumberjs 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44683865/

相关文章:

reactjs - Docker-compose 与 WSL 2 React 应用程序不会在更改时热重载

webpack - 如何避免 webpack 加载不必要的包文件?

ruby-on-rails - Rails 中后台作业的集成测试

java - 通过 cucumber 时没有得到结果。尝试实现BDD

javascript - React js map() undefined 不是一个函数

javascript - React JSX 元素中的循环依赖

javascript - 在初始 ReactDOM.render 之后,当 componentDidMount 方法开始触发时,整个 DOM 是否准备就绪?

reactjs - React Components 的构造函数需要 props 参数吗?

Webpack -p vs --mode=production

ruby - 为什么我在 Cucumber "Around"中看不到抛出的异常?