[英]Jest's `it.each` causes a 'Expected done to be called once, but it was called multiple times' error when used with 'getByTestId'
I have a component with some parts that are conditionally rendered based on some config on the window
object.我有一个组件,其中一些部分根据
window
对象上的某些配置有条件地呈现。 This all works, I can confirm this manually.这一切都有效,我可以手动确认。
I have this sort of test setup, but I have simplified it somewhat:我有这种测试设置,但我对其进行了一些简化:
it('renders both by default', () => { // PASS
const Comp = getComponent();
render(<Comp />);
expect(screen.getByTestId('one')).toBeInTheDocument();
expect(screen.getByTestId('two')).toBeInTheDocument();
})
it.each([
{ testId: 'one', otherTestId: 'two'},
{ testId: 'two', otherTestId: 'one' },
])('renders $testId, not $otherTestId', (testId, otherTestId) => { // FAIL
delete window.config[otherTestId]; // this works
const Comp = getComponent();
render(<Comp />);
expect(screen.getByTestId(testId)).toBeInTheDocument();
expect(screen.getByTestId(otherTestId)).not.toBeInTheDocument();
})
But I am getting this error:但我收到此错误:
Expected done to be called once, but it was called multiples times. Reason 'two'.
Which is not something I've ever seen before.这不是我以前见过的。 None of my tests that are running here are async.
我在这里运行的测试都不是异步的。 The component isn't async.
该组件不是异步的。 I'm not using done anywhere near this.
我没有在这附近的任何地方使用完成。
What's happening?发生了什么?
Jest is expecting testId
to be an object containing all your test variables, and otherTestId
to be the function done
. Jest 期望
testId
是一个包含所有测试变量的对象,而otherTestId
是函数done
。
I'm not sure why it thinks it's called multiple times, but making the first argument:我不确定为什么它认为它被多次调用,但提出第一个论点:
{ testId, otherTestId }
Works around this, and matches the documentation correctly.解决此问题,并正确匹配文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.