cypress cy.tick 在 Cypress 组件测试中无法工作

huangapple go评论52阅读模式
英文:

cypress cy.tick not working in cypress component tests

问题

有人在Cypress组件测试中成功使用cy.tick()使时间前进吗?cy.clock()可以停止时间,但tick在Cypress组件测试中不会使时间前进。如果有人能够使其工作,请提供一个示例。

再次强调,这是针对Cypress组件测试,而不是端到端测试。我还将clock添加到before()方法中。

英文:

Has anyone out there had any success in getting cy.tick() to work in cypress component tests.
cy.clock() stops the time but tick will not move it forward in cypress component tests. If someone was able to get this working please give mean example .

And again this is in cypress component testing not e2n testing.
I've also added clock to a before() method.

答案1

得分: 5

你可以在set-timeout-example找到一个名为LoadingIndicator的示例组件以及测试。

我在一个新的 CRA 应用程序中运行了名为loading-indicator.cy.jsx的测试,测试通过。

其中一个使用了cy.tick()的测试如下:

it('应该渲染加载指示器(模拟时钟)', () => {
  cy.clock()
  mount(
    <LoadingIndicator isLoading={true}>
      <div>ahoy!</div>
    </LoadingIndicator>,
  )

  // 强制立即过去2秒钟
  // 并触发组件的setTimeout
  cy.tick(2010)

  cy.contains('loading...',
    { timeout: 100 }               // 如果加载器不迅速显示,则失败
  ).should('be.visible')
})

也许你的应用程序没有使用其中一个支持的本机定时器函数?

英文:

You can find an example component LoadingIndicator plus test here set-timeout-example.

I ran the code in a new CRA application, and the test loading-indicator.cy.jsx passed.

One of the tests that uses cy.tick() is

it(&#39;should render loading indicator (mocks clock)&#39;, () =&gt; {
  cy.clock()
  mount(
    &lt;LoadingIndicator isLoading={true}&gt;
      &lt;div&gt;ahoy!&lt;/div&gt;
    &lt;/LoadingIndicator&gt;,
  )

  // force 2 seconds to pass instantly
  // and component&#39;s setTimeout to fire
  cy.tick(2010)

  cy.contains(&#39;loading...&#39;, 
    { timeout: 100 }               // fail if loader not displayed quickly
  ).should(&#39;be.visible&#39;)
})

Maybe your app does not use one of the supported native timer functions?

huangapple
  • 本文由 发表于 2023年2月27日 08:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定