mock-storagearea is a implementation of Chrome's extension storage interface, i.e. chrome.storage
. This package is intended for use in development and testing of extensions outside of a browser.
See: API
This module exports four storage areas, each corresponding to those in Chrome:
import {
createLocalStorageArea,
createSessionStorageArea,
createSyncStorageArea,
createManagedStorageArea,
onChanged,
} from '@lmcd/mock-storagearea';
const local = createLocalStorageArea();
const session = createSessionStorageArea();
const sync = createSyncStorageArea();
const managed = createManagedStorageArea();
const chrome = {
storage: {
local,
session,
sync,
managed,
onChanged: onChanged({ session, local, sync, managed }),
}
};
See: Listening to changes
Normally, quotas are defined by the browser and not configurable by the extension. However, for testing purposes, the quota constraints can be overwritten during initialisation.
See: Adjusting quotas
It is best practice to not rely on Chrome's serialisation behaviour and instead serialise/deserialise your values into strings before storage. However, this package attempt's to implement identical serialisation/deserialisation techniques as Chrome.
See: Serialisation & deserialisation and storage size
- Manifest V3 introduced support for promises, where as versions prior used a callback argument. This implementation is not backwards-compatible with callbacks and only supports promises. Providing a callback will throw an exception.
- Whilst attempts have been made to identify and replicate any undocumented behaviour in the
chrome.storage
documentation, some discrepancies may exist across versions and browsers. This package is intended to work with any browser which implements the Chromium-based extension API. - Due to the usage of
structuredClone
, this package only supports Node 18+.
pnpm run build
pnpm run test