简体   繁体   English

与“getByTestId”一起使用时,Jest 的“it.each”会导致“预期完成被调用一次,但它被多次调用”错误

[英]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?发生了什么?

You need to destructure your test variables您需要解构您的测试变量

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.

相关问题 为什么 mocha 测试在钩子 &lt;“before each” ....&gt; 中多次调用“错误完成()”? - Why is mocha test throwing "Error done() called multiple times in hook <"before each" ....>? 错误:尝试在pact javascript中实现多次交互时多次调用了done() - Error: done() called multiple times when trying to implement multiple interactions in pact javascript 当被引用为 $predicate 时,Jest 的 `it.each()` 描述渲染箭头函数源代码 - Jest's `it.each()` description to render arrow function source code when referred as $predicate 测试失败 - 多次调用 Mocha 的 done() - Failing test - Mocha's done() called multiple times “done() 被多次调用”带摩卡咖啡的 Moongoose - "done() called multiple times" moongoose with mocha jest.fn() 被多次调用 - jest.fn() is being called multiple times setTimeout() 只运行一次,多次调用时 - setTimeout() only running once, when called multiple times 与angular一起使用时,在getContenttool中多次调用保存功能 - Save function is getting called multiple times in getContenttool when used with angular 在 Jest 中测试异步代码:done() 未按预期调用 - Testing asynchronous code in Jest: done() not being called as expected Sinon js AssertError:预期存根被调用一次,但被调用了0次 - Sinon js AssertError: expected stub to be called once but was called 0 times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM