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

146
Vistas
Jest unit test for datetime passes on local, but fails in Jenkins CICD

I am using date-fns to parse some date-like strings into dates, then back into into a string, like so. My local unit tests are passing, but in Jenkins CICD, my unit tests fail. I am pretty sure it is due to the Jenkins machine being in GMT+0, while I am in a GMT+8 timezone. The results of the failed tests in Jenkins supports this, as the received values are 8 hours behind the expected values.

import { format } from 'date-fns';

function stringToTime(date: string) {
    const newDate = new Date(date);
    return format(newDate, 'k:mm'); // this returns a string
}

And my unit tests:

const sampleText = 'Mon Aug 02 2021 22:00:15 GMT+0800';
const expectedText = '22:00';
expect(stringToTime(sampleText)).toEqual(expectedText); // this passes locally

In my Jenkins CICD, I receive 14:00 instead. Is this a common problem, and what's a quick way I can fix up my unit tests so it can pass on Jenkins?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I've always had trouble using the Date constructors when passing in a time string. Try using parseISO from date-fns like parseISO(date) instead of new Date(date).

also, you should import from 'date-fns/tmz' to pass in timezone information. That way you can ensure the timezones are consistent and parsed. here's an example from the docs: https://date-fns.org/v2.28.0/docs/Time-Zones

const { zonedTimeToUtc, utcToZonedTime, format } = require('date-fns-tz')

// Set the date to "2018-09-01T16:01:36.386Z"
const utcDate = zonedTimeToUtc('2018-09-01 18:01:36.386', 'Europe/Berlin')

// Obtain a Date instance that will render the equivalent Berlin time for the UTC date
const date = new Date('2018-09-01T16:01:36.386Z')
const timeZone = 'Europe/Berlin'
const zonedDate = utcToZonedTime(date, timeZone)
// zonedDate could be used to initialize a date picker or display the formatted local date/time

// Set the output to "1.9.2018 18:01:36.386 GMT+02:00 (CEST)"
const pattern = 'd.M.yyyy HH:mm:ss.SSS \'GMT\' XXX (z)'
const output = format(zonedDate, pattern, { timeZone: 'Europe/Berlin' })
about 4 years ago · Juan Pablo Isaza Denunciar
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