当使用来自酶的浅表时,我应该在Jest beforeEach中覆盖设置

问题描述 投票:1回答:1

如果您查看测试2和3,那是设置和测试我们期望的正确方法?

这里是否有性能问题,内存泄漏或“不良的处理方式”?

import { shallow } from 'enzyme';
import Component from 'path/to/my/component';

describe('<Component />', () => {
  const defaultProps = {...};

  const setup = (props = {}) => shallow(<Component {...defaultProps, ...props} />);

  let wrapper;

  describe('tests ...', () => {
    beforeEach(() => { wrapper = setup(); });

    // 1 - Use the wrapper without additional props
    it('should ...', () => {
      expect(wrapper.find('selector')).toHaveLength(1);
    });

    // 2 - Use setup with additional props directly inside an expect
    it('should ...', () => {
      expect(setup({ bar: 'bar' }).find('selector')).toHaveLength(0);
    });

    // 3 - Override wrapper with additional props and use it inside an expect
    it('should ...', () => {
      wrapper = setup({ bar: 'bar' });
      expect(wrapper.find('selector')).toHaveLength(0);
    });

    // more tests.. 
  });
});
javascript reactjs testing jestjs enzyme
1个回答
0
投票

如果您进行这种类型的测试,则可能首先不需要使用beforeEach。在2的情况下,不使用beforeEach创建的包装器,至少应该添加afterEach初始化包装器的动作。

© www.soinside.com 2019 - 2024. All rights reserved.