You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cypress 13.9.0执行cy.reload()时遇RangeError问题求助

Cypress 13.9.0中cy.reload()触发RangeError: Invalid time value的解决方法

问题场景

使用Cypress 13.9.0编写测试时,执行cy.reload()后触发RangeError: Invalid time value错误,错误根源指向Date.toISOString()调用。测试代码中原本使用cy.clock(new Date())来控制超时逻辑。

复现代码

waitForUpdate(employeeId, employeeName, employeeEmail) {
  const token = JSON.parse(localStorage.getItem('activeToken')).token.token;
  cy.clock(new Date()); // 启动时钟以控制超时

  const checkStatus = () => {
    cy.request({
      method: 'GET',
      url: Cypress.config().baseUrl + `api/v1/employee/${employeeId}`,
      headers: {
        Authorization: `Bearer ${token}`,
      },
    }).then((response) => {
      expect(response.status).to.equal(200);
      const { name, email } = response.body;

      if (name === employeeName && email === employeeEmail) {
        cy.log("Employees' data updated");
        cy.reload();
      } else if (new Date().getTime() - startTime < 900000) {
        cy.wait(15000);
        checkStatus();
      } else {
        cy.log('Employees data is not updated within 15 minutes.');
        expect(false).to.equal(true);
      }
    });
  };

  const startTime = new Date().getTime();
  checkStatus();
}

错误日志

RangeError: The following error originated from your application code, not from Cypress.

  > Invalid time value

When Cypress detects uncaught errors originating from your application it will automatically fail the current test.

This behavior is configurable, and you can choose to turn this off by listening to the `uncaught:exception` event.
    at Date.toISOString (<anonymous>)
    at q.end (main.a1444de99b433347.js:1:216062)
    at HTMLDocument.<anonymous> (main.a1444de99b433347.js:1:62383)
    at I.invokeTask (polyfills.2ba72d64643256ee.js:1:7246)
    at B.runTask (polyfills.2ba72d64643256ee.js:1:2614)
    at b.invokeTask [as invoke] (polyfills.2ba72d64643256ee.js:1:8300)
    at j (polyfills.2ba72d64643256ee.js:1:20943)
    at x (polyfills.2ba72d64643256ee.js:1:21349)
    at HTMLDocument.J (polyfills.2ba72d64643256ee.js:1:21513)

解决方法及原因

  • 尝试过调整不同刷新方式、修改cy.clock()参数,均无法解决问题。
  • 最终移除cy.clock(new Date())语句,仅保留const startTime = new Date().getTime()用于超时判断,问题彻底解决。

原因分析

cy.clock()会替换浏览器原生的Date对象和时间相关API,模拟一个受控的时钟环境。当执行cy.reload()刷新页面后,应用代码中的某些逻辑可能依赖真实的系统时间,而模拟时钟生成的时间值在页面重新初始化后出现异常,导致调用Date.toISOString()时传入了无效的时间值,触发RangeError。移除cy.clock()后,页面使用真实系统时间,避免了时间值无效的问题。

内容的提问来源于stack exchange,提问作者Edd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 19:32:23