【问题标题】:failed to test Container although setup for enzyme successfully尽管成功设置酶,但未能测试容器
【发布时间】:2019-05-12 14:32:27
【问题描述】:

我有这个容器

import React from "react";

class App extends React.Component {
  render() {
    return (
      <div className="App">
        <input type="text" onChange={this.changeInput} />
        <button onClick={this.addItem}>Add</button>
      </div>
    );
  }
}

export default App;

为什么我不能在上面使用shallow?我的测试看起来像这样

import App from "./index";

describe("App", function() {
  it("should render", function() {
    const wrapper = shallow(<App />);
    //expect(wrapper).toBe(1);
  });
});

错误提示 Enzyme expects an adapter to be configured, but found none. 但我相信我已经正确设置了 Enzyme。我想测试 App 是否包含输入和按钮,但我卡在那里..

演示https://codesandbox.io/s/21rz901mp0

【问题讨论】:

标签: reactjs ecmascript-6 jestjs enzyme


【解决方案1】:

创建一个酵素.js 文件并添加以下代码... 并将其导入到测试单元测试用例文件中

import Enzyme, { configure, shallow, mount, render } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });
export { shallow, mount, render };
export default Enzyme;

【讨论】:

    【解决方案2】:

    只需安装这个包enzyme-adapter-react-16,然后在你的测试中添加这两行:

    import * as Adapter from 'enzyme-adapter-react-16';
    enzyme.configure({ adapter: new Adapter() });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 2019-04-24
      • 2020-09-14
      • 1970-01-01
      相关资源
      最近更新 更多