Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda