代码之家  ›  专栏  ›  技术社区  ›  Faizuddin Mohammed

如何在jest中的测试之前配置beforeach函数?

  •  0
  • Faizuddin Mohammed  · 技术社区  · 8 年前

    我用 beforeEach 就像在我的react组件测试中一样。

    let component;
    let configProp;
    
    beforeEach(() => {
      component = shallow(<MyComponent config={configProp} />);
    });
    

    现在,我想改变 configProp 测试中的变量。像这样的:

    let component;
    let configProp;
    
    beforeEach(() => {
      component = shallow(<MyComponent config={configProp} />);
    });
    
    // runs before beforeEach for the next test
    beforeNext(() => {
        const config_1 = {...};
        configProp = config_1;
    });
    
    test('config_1', () => {
      // component now has config=config_1
      expect(component.find({config: config_1}).exists());
    });
    

    有可能吗?或者,我们必须 shallow(<MyComponent />) 每次单独测试?

    现在,我正在使用 it 块为下一个函数更改配置。这是合法的吗?像这样的:

    it('changes configProp', () => {
        const config_1 = {...};
        configProp = config_1;
    });
    
    test('config_1', () => {
      expect(component.find({config: config_1}).exists());
    });
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Bartek Fryzowicz    8 年前

    您可以在“内部测试”中设置新的道具:

    test('config_1', () => {
      component.setProps({ config: configProp });
      ...
    })