Skip to content

Commit bd238c6

Browse files
committed
test: fix helper test to work on different timezones too
1 parent f60ae21 commit bd238c6

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

__tests__/Helper.spec.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
// helper.test.js
22
const { Helper } = require('../src/Helper');
3-
const moment = require('moment');
3+
const moment = require('moment-timezone');
44
const fs = require('fs');
55
const crypto = require('crypto');
66

77
describe('Helper', () => {
88
describe('LocalTime', () => {
9+
beforeAll(() => {
10+
// Set a default timezone for tests
11+
moment.tz.setDefault('America/New_York');
12+
});
13+
14+
afterAll(() => {
15+
// Reset the default timezone
16+
moment.tz.setDefault();
17+
});
18+
919
it('returns expected local time for valid timestamp', () => {
10-
const timestamp = 1737155257000; // Representing 2022-02-01 12:30:00
11-
const expectedTime = '18-01 01:07:37';
20+
const timestamp = 1737155257000; // Representing 2025-01-17 12:07:37 UTC
21+
const expectedTime = '17-01 18:07:37'; // Expected time in America/New_York (UTC-5)
1222
expect(Helper.LocalTime(timestamp)).toBe(expectedTime);
1323
});
1424

1525
it('returns expected local time for current timestamp', () => {
16-
const currentTimestamp = Math.floor(Date.now() / 1000);
17-
const currentTime =
26+
const currentTimestamp = Date.now();
27+
const expectedTime =
1828
moment(currentTimestamp).format('DD-MM HH:mm:ss');
19-
expect(Helper.LocalTime(currentTimestamp)).toBe(currentTime);
29+
expect(Helper.LocalTime(currentTimestamp)).toBe(expectedTime);
2030
});
2131
});
2232

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"husky": "^9.1.7",
3434
"jest": "^29.7.0",
3535
"lint-staged": "^16.1.2",
36+
"moment-timezone": "^0.6.0",
3637
"prettier": "^3.6.2"
3738
},
3839
"config": {

0 commit comments

Comments
 (0)