简体   繁体   English

React&Enzyme:为什么不能在每个()工作之前?

[英]React & Enzyme: why isn't beforeEach() working?

I'm writing my first React tests and running into an issue where my beforeEach statement isn't working. 我正在编写我的第一个React测试并遇到一个我的beforeEach语句无效的问题。 Here's my test file: 这是我的测试文件:

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);
  });
});

Here's the relevant part of my package.json : 这是我的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"
}

I get this error when the tests run: 测试运行时出现此错误:

ReferenceError: wrapper is not defined

What am I missing? 我错过了什么?

You are defining the wrapper const inside of the beforeEach scope, move it outside like this: 您正在定义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);
  });
});

This way you'll have access to wrapper inside of the it s scope. 通过这种方式,您可以访问it范围内的包装器。

Constants are block-scoped, much like variables defined using the let statement. 常量是块范围的,非常类似于使用let语句定义的变量。 The value of a constant cannot change through re-assignment, and it can't be redeclared. 常量的值不能通过重新赋值来改变,也不能重新声明。

Since you want to assign the variable inside of the beforeEach scope and use it inside of the it scopes, you'll have to declare the variable in a common scope, which, in this case is the describe scope. 由于您希望在beforeEach作用域内分配变量并在其beforeEach域内使用它, it您必须在一个公共作用域中声明该变量,在本例中是describe作用域。

Added (works on mocha but not on jest): 添加(适用于摩卡但不适用于开玩笑):

Another possible way to fix this is to use the this keyword (which I prefer, if using mocha... Does not work with jest). 解决这个问题的另一种可能方法是使用this关键字(如果使用mocha,我更喜欢this ...不适用于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);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM