【问题标题】:React & Enzyme: why isn't beforeEach() working?React & Enzyme:为什么 beforeEach() 不起作用?
【发布时间】:2017-07-08 05:04:43
【问题描述】:

我正在编写我的第一个 React 测试并遇到了我的 beforeEach 语句不起作用的问题。这是我的测试文件:

import React from 'react';
import { shallow } from 'enzyme';
import Home from '../components/Home';
import IntroText from '../components/IntroText';
import Form from '../components/Form';

describe('<Home />', () => {
  beforeEach(() => {
    const wrapper = shallow(<Home />);
  });

  it('renders the IntroText component', () => {
    expect(wrapper.find(IntroText).length).toBe(1);
  });

  it('renders the Form component', () => {
    expect(wrapper.find(Form).length).toBe(1);
  });
});

这是我package.json的相关部分:

"devDependencies": {
  "babel-jest": "^18.0.0",
  "babel-preset-es2015": "^6.22.0",
  "babel-preset-react": "^6.23.0",
  "jest": "^18.1.0",
  "react-scripts": "0.9.0",
  "react-test-renderer": "^15.4.2"
 },
"dependencies": {
  "enzyme": "^2.7.1",
  "jest-enzyme": "^2.1.2",
  "react": "^15.4.2",
  "react-addons-test-utils": "^15.4.2",
  "react-dom": "^15.4.2",
  "react-router": "^3.0.2"
},
"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test --env=jsdom",
  "eject": "react-scripts eject"
}

测试运行时出现此错误:

ReferenceError: wrapper is not defined

我错过了什么?

【问题讨论】:

    标签: reactjs testing jestjs enzyme


    【解决方案1】:

    您在 beforeEach 范围内定义包装器 const,将其移到外部,如下所示:

    import React from 'react';
    import { shallow } from 'enzyme';
    import Home from '../components/Home';
    import IntroText from '../components/IntroText';
    import Form from '../components/Form';
    
    describe('<Home />', () => {
      let wrapper;
      beforeEach(() => {
        wrapper = shallow(<Home />);
      });
    
      it('renders the IntroText component', () => {
        expect(wrapper.find(IntroText).length).toBe(1);
      });
    
      it('renders the Form component', () => {
        expect(wrapper.find(Form).length).toBe(1);
      });
    });
    

    这样您就可以访问its 范围内的包装器。

    常量是块范围的,很像使用 let 定义的变量 陈述。常量的值不能通过 重新分配,并且不能重新声明。

    由于您要在 beforeEach 范围内分配变量并在 it 范围内使用它,因此您必须在公共范围内声明该变量,在本例中为 @987654326 @ 范围。

    已添加(适用于 mocha 但不适用于 jest):

    解决此问题的另一种可能方法是使用 this 关键字(如果使用 mocha,我更喜欢...不适用于 jest)。

    import React from 'react';
    import { shallow } from 'enzyme';
    import Home from '../components/Home';
    import IntroText from '../components/IntroText';
    import Form from '../components/Form';
    
    describe('<Home />', function() {
      beforeEach(function() {
        this.wrapper = shallow(<Home />);
      });
    
      it('renders the IntroText component', function() {
        expect(this.wrapper.find(IntroText).length).toBe(1);
      });
    
      it('renders the Form component', function() {
        expect(this.wrapper.find(Form).length).toBe(1);
      });
    });
    

    【讨论】:

    • 谢谢,非常感谢!
    • 这是一个经典的作用域示例!
    • 如果您仍然未定义:- 我在测试中需要的对象上尝试了这种 ^ 方法(它在 beforeEach() 中定义,然后被引用/使用在测试语句中,但仍然未定义。即使使用this.myobject 方法^^。我阅读了 Jest 文档,看起来他们在导入下方的全局/顶级范围中定义了对象本身,then 在beforeEach() 中指定它是什么,然后可以在测试语句中使用。
    猜你喜欢
    • 2020-06-08
    • 2018-12-16
    • 2019-07-05
    • 1970-01-01
    • 2022-08-08
    • 2021-01-11
    • 1970-01-01
    • 2022-01-18
    • 2021-03-15
    相关资源
    最近更新 更多