mock-browser-timezone
A Javascript library to mock the browser's timezone using ECMAScript Internationalization API.
What's different?
Many modern browsers use the Intl API to get the desired IANA timezone string which depicts the current browser timezone.
This library is specifically designed to mock the browser timezone, assisting to test the relevant code which uses the Intl
API.
Installation
Using npm:
npm install mock-browser-timezone --save-dev
Using yarn:
yarn add mock-browser-timezone -D
API
- registerTimezone: For mocking the browser's timezone represented in IANA timezone string.
- registerAbbreviatedTimezone: For mocking the abbreviated timezone string. Factually, there can be a lot of ambiguity in determining abbreviated timezone since it highly depends on the daylight time and a single abbreviated string can also represent many other timezone values.
For a better read regarding the same: https://stackoverflow.com/tags/timezone/info. - reset: Used to bring back the mocked timezones to their original state.
Usage
For a better implementation of mock-browser-timezone
, have a look at mock-browser-timezone.js
file.
const {
registerTimezone,
registerAbbreviatedTimezone,
reset,
} = require('mock-browser-timezone');
// Mocking the timezone using IANA timezone string.
registerTimezone('Europe/Paris');
reset(); // Default back to the orginal value.
new Intl.DateTimeFormat().resolvedOptions().timeZone; // Europe/Paris
registerAbbreviatedTimezone('CET');
new Intl.DateTimeFormat([], {
timeZoneName: 'short',
})
.formatToParts(date)
.find((locale) => locale.type === 'timeZoneName').value; // CET
reset(); // Default back to the orginal abbreviated timezone value.
Supported Timezones
A list of supported IANA formatted timezones as well as abbreviated timezones can be found at timezones.json
.