{"id":28391,"library":"timezone-mock","title":"timezone-mock","description":"A JavaScript library to mock the local timezone for testing purposes. Version 1.4.2 is current stable, with infrequent releases. It replaces the global Date constructor with a mocked version that behaves as if in a specified timezone, including DST transitions. Key differentiators: lightweight, no dependencies, supports multiple timezones (US/Pacific, US/Eastern, Brazil/East, UTC, Europe/London, Australia/Adelaide) and GMT offsets, and includes TypeScript types. Unlike full timezone libraries, it focuses solely on mocking for testing, not on timezone conversions or comprehensive data.","status":"active","version":"1.4.2","language":"javascript","source_language":"en","source_url":"https://github.com/Jimbly/timezone-mock","tags":["javascript","timezone","mock","date","dst","daylight","saving","savings","time","typescript"],"install":[{"cmd":"npm install timezone-mock","lang":"bash","label":"npm"},{"cmd":"yarn add timezone-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add timezone-mock","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package exports named exports; default import works but is not typical.","wrong":"import timezoneMock from 'timezone-mock'; timezoneMock.register()","symbol":"register","correct":"import { register } from 'timezone-mock'"},{"wrong":"const { unregister } = require('timezone-mock'); // CommonJS works but ESM preferred","symbol":"unregister","correct":"import { unregister } from 'timezone-mock'"},{"note":"Only available after register() is called; gives access to original Date.","symbol":"_Date","correct":"import { _Date } from 'timezone-mock'"},{"note":"Accepts an object with fallbackFn for unparseable dates.","symbol":"options","correct":"import { options } from 'timezone-mock'"}],"quickstart":{"code":"import { register, unregister } from 'timezone-mock';\n\nregister('US/Pacific');\nconst date = new Date('2023-06-15T12:00:00');\nconsole.log(date.getHours()); // Output may differ from local timezone\nunregister();\n\n// Test DST behavior\nregister('US/Eastern');\nconst dstDate = new Date('2023-03-12T02:30:00'); // DST transition\nconsole.log(dstDate.getHours()); // 3 (spring ahead)\nunregister();","lang":"typescript","description":"Demonstrates registering mock timezone, creating a Date, and unregistering. Also shows DST transition test."},"warnings":[{"fix":"Use afterEach or cleanup hooks to call unregister().","message":"timezone-mock replaces the global Date constructor; if used in a test suite, must call unregister() after each test to avoid cross-test contamination.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure Node >= 8 for consistent behavior, or avoid date string parsing in tests.","message":"Node v8.0.0 changed ISO string 'YYYY-MM-DDTHH:MM:SS' from UTC to local interpretation. timezone-mock treats it as local, matching Node v8+; tests on older Node may behave differently.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use US/Pacific or US/Eastern for DST testing.","message":"Brazil/East timezone no longer observes DST as of 2019; DST transition only applies to dates 2018 and earlier. Using it for modern dates may not test DST correctly.","severity":"deprecated","affected_versions":">=1.4.0"},{"fix":"Remember Etc/GMT+N = UTC-N, not UTC+N.","message":"Timezones Etc/GMT+X have inverted sign: Etc/GMT+5 is UTC-5, not UTC+5. Common mistake.","severity":"gotcha","affected_versions":">=1.4.0"},{"fix":"Call register() first, then access timezone_mock._Date.","message":"The _Date property is only available after register() is called; accessing it before results in undefined.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Use import { register } from 'timezone-mock' OR import * as timezoneMock from 'timezone-mock' then timezoneMock.register.","cause":"Importing default instead of named export (e.g., import timezoneMock from 'timezone-mock' then timezoneMock.register).","error":"TypeError: timezone_mock.register is not a function"},{"fix":"Use UTC dates (new Date('2021-01-01T00:00:00Z')) or ensure consistent timezone handling.","cause":"Date comparison fails because mock timezone changes the timestamp for date string '2021-01-01T00:00:00' (interpreted as local time).","error":"AssertionError [ERR_ASSERTION]: Expected values to be strictly equal. Expected: 1609459200000. Received: 1609455600000"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}