Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
Cypress: JavaScript Date format string gets converted to NaN

In one of my automation tests, I am importing a date format stored in the environment parameters, use it for my test and then increment it by one second after the test is executed. This is the code:

afterEach(()=>{
    var startTimeStamp = Cypress.env('aestartTimestamp');
    var startTimestampString = String(startTimeStamp);
    var epochStartTimeStamp = new Date(startTimestampString);
    var startYear = epochStartTimeStamp.getFullYear();
    var month = (epochStartTimeStamp.getMonth())+1;
    var realStartMonth = month <=9 ? '0'+month:month;
    var startDate = epochStartTimeStamp.getDate()<=9 ? '0'+epochStartTimeStamp.getDate():epochStartTimeStamp.getDate();
    var startHours = epochStartTimeStamp.getHours() <=9 ? '0'+epochStartTimeStamp.getHours():epochStartTimeStamp.getHours();
    var startMinutes = epochStartTimeStamp.getMinutes() <=9 ? '0'+epochStartTimeStamp.getMinutes():epochStartTimeStamp.getMinutes(); 
    var startSeconds = (epochStartTimeStamp.getSeconds()+1)<=9? '0'+(epochStartTimeStamp.getSeconds()+1):(epochStartTimeStamp.getSeconds()+1);
    var newStartTimeStamp = startYear+"-"+realStartMonth+"-"+startDate+" "+startHours+":"+startMinutes+":"+startSeconds;
    Cypress.env('aestartTimestamp',newStartTimeStamp);
    console.log(newStartTimeStamp);

    var endTimeStamp = Cypress.env('aeendTimestamp');
    var endTimeStampString = String(endTimeStamp);
    var epochEndTimeStamp = new Date(endTimeStampString);
    var endYear = epochEndTimeStamp.getFullYear();
    var endMonth = (epochEndTimeStamp.getMonth())+1;
    var realEndMonth = endMonth <=9 ? '0'+endMonth:endMonth;
    var endDate = epochEndTimeStamp.getDate()<=9 ? '0'+epochEndTimeStamp.getDate():epochEndTimeStamp.getDate();
    var endHours = epochEndTimeStamp.getHours() <=9 ? '0'+epochEndTimeStamp.getHours():epochEndTimeStamp.getHours();
    var endMinutes = epochEndTimeStamp.getMinutes() <=9 ? '0'+epochEndTimeStamp.getMinutes():epochEndTimeStamp.getMinutes();
    var endSeconds = (epochEndTimeStamp.getSeconds()+1) <=9 ? '0'+(epochEndTimeStamp.getSeconds()+1):(epochEndTimeStamp.getSeconds()+1);
    var newEndTimestamp = endYear+"-"+realEndMonth+"-"+endDate+" "+endHours+":"+endMinutes+":"+endSeconds;
    Cypress.env('aeendTimeStamp',newEndTimestamp);
    console.log(newEndTimestamp);
})

However, after running the tests, the date gets converted to NaN. This is what I see in the console:

enter image description here

The timestamp is converted to NaN.

When I run the code independently in any browser console, I see that the code works just perfectly. (The new time stamp is incremented by 1 second).

So I am mystified as to why the code works properly in the dev console but not in the Cypress. Test. Is there something missing?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda