Cypress - RangeError:Date.toISOString 的时间值无效

问题描述 投票:0回答:1

我使用的是 cypress 版本 13.9.0,并且收到 RangeError: Invalid time value at Date.toISOString on cy.reload()。

有什么办法可以克服它吗?

  waitForUpdate(employeeId, employeeName, employeeEmail) {
    const token = JSON.parse(localStorage.getItem('activeToken')).token.token;
    cy.clock(new Date()); // Start a clock to control the timeout

    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) {
          // Task found and status is as expected
          cy.log("Employees' data updated");
          cy.reload();
        } else if (new Date().getTime() - startTime < 900000) {
          // Retry for a maximum of 15 minutes (900000 milliseconds)
          // eslint-disable-next-line cypress/no-unnecessary-waiting
          cy.wait(15000); // Wait for 15 seconds
          checkStatus(); // Retry the request
        } else {
          // Timeout exceeded, fail the test
          cy.log('Employees data is not updated within 15 minutes.');
          expect(false).to.equal(true); // Fail the test
        }
      });
    };

    const startTime = new Date().getTime();
    checkStatus(); // Start checking the status
  }

我收到以下错误:

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)

尝试了不同类型的页面重新加载,使用了 javascript win.reload。 尝试使用 cy.clock 操作。

错误仍然存在。

typescript automated-tests cypress uncaught-exception
1个回答
0
投票

您的

waitForUpdate
方法中似乎没有任何内容会导致这种情况,它只是执行
cy.request()
,这不应该对加载的页面产生影响。

您可以尝试将

cy.reload()
移到函数之外,在这种情况下,您可能会使用自定义命令来避免同步问题。

类似这样的:

Cypress.Commands.add('waitForUpdate', (employeeId, employeeName, employeeEmail) => {

  // code for recursive polling, without the cy.reload()

})

it('run the test', () => {
  cy.waitForUpdate(employeeId, employeeName, employeeEmail) 
    .then(() => cy.reload() )
© www.soinside.com 2019 - 2024. All rights reserved.