Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!