Jestjs 方法“;模拟&x201D;将在1个节点上运行。改为找到0

Jestjs 方法“;模拟&x201D;将在1个节点上运行。改为找到0,jestjs,enzyme,Jestjs,Enzyme,我最近用ThemeProvider在测试中包装了我的组件。当我运行测试时,它抛出以下错误 '方法“模拟”将在1个节点上运行。找到0而不是' 在包装之前,它工作得很好。我如何解决这个问题?我在GitHub中发现了许多类似的问题,我尝试了所有这些方法,但仍然得到了相同的错误 包装前的代码: test('handleSelect function called on option select', () => { const handleSelectSpy = sinon.spy();

我最近用ThemeProvider在测试中包装了我的组件。当我运行测试时,它抛出以下错误

'方法“模拟”将在1个节点上运行。找到0而不是'

在包装之前,它工作得很好。我如何解决这个问题?我在GitHub中发现了许多类似的问题,我尝试了所有这些方法,但仍然得到了相同的错误

包装前的代码:

 test('handleSelect function called on option select', () => {
    const handleSelectSpy = sinon.spy();
    wrapper = mount( 
        <Dropdown handleSelect={handleSelectSpy} options={options} />
    );
    dropdown = wrapper.find('Dropdown');
    dropdown
      .find('InputBase')
      .find('[role="button"]')
      .simulate('click');
    expect(true).toBe(true);
  });
});
 test('handleSelect function called on option select', () => {
    const handleSelectSpy = sinon.spy();
    wrapper = mount(
      <ThemeProvider>
        <Dropdown handleSelect={handleSelectSpy} options={options} />
      </ThemeProvider>,
    );
    dropdown = wrapper.find('Dropdown');
    dropdown
      .find('InputBase')
      .find('[role="button"]')
      .simulate('click');
    expect(true).toBe(true);
  });
}); 
test('handleSelect函数在选项select'上调用,()=>{
const handleSelectSpy=sinon.spy();
包装器=装载(
);
dropdown=wrapper.find('dropdown');
下拉列表
.find('InputBase')
.find(“[role=“button”]”)
.模拟(“点击”);
期望(真),期望(真);
});
});
包装后的代码:

 test('handleSelect function called on option select', () => {
    const handleSelectSpy = sinon.spy();
    wrapper = mount( 
        <Dropdown handleSelect={handleSelectSpy} options={options} />
    );
    dropdown = wrapper.find('Dropdown');
    dropdown
      .find('InputBase')
      .find('[role="button"]')
      .simulate('click');
    expect(true).toBe(true);
  });
});
 test('handleSelect function called on option select', () => {
    const handleSelectSpy = sinon.spy();
    wrapper = mount(
      <ThemeProvider>
        <Dropdown handleSelect={handleSelectSpy} options={options} />
      </ThemeProvider>,
    );
    dropdown = wrapper.find('Dropdown');
    dropdown
      .find('InputBase')
      .find('[role="button"]')
      .simulate('click');
    expect(true).toBe(true);
  });
}); 
test('handleSelect函数在选项select'上调用,()=>{
const handleSelectSpy=sinon.spy();
包装=装载(
,
);
dropdown=wrapper.find('dropdown');
下拉列表
.find('InputBase')
.find(“[role=“button”]”)
.模拟(“点击”);
期望(真),期望(真);
});
}); 
错误: 下拉菜单-在选项选择上调用完整DOM渲染›handleSelect函数 方法“simulate”将在1个节点上运行。改为找到0

.find('InputBase')
如果这是一个组件类名称/构造函数,则应不带引号:

.find(InputBase)

只有CSS选择器才能在引号中使用。

事实上,这并不是Ezyme API参考中所说的。虽然,React组件是一个字符串。重要提示:我在忘记导入组件时也遇到了这个问题。例如,在本例中,如果我忘记将
InputBase
组件导入到测试文件中,并试图通过其构造函数
.find(InputBase)
来查找它,那么在运行测试时,Jest会显示类似这样的错误
方法“someFunction”将在1个节点上运行。而是找到了0
。我花了几分钟想弄清楚到底发生了什么。