{"id":13769695,"url":"https://github.com/clarkbw/jest-localstorage-mock","last_synced_at":"2025-05-15T12:06:59.419Z","repository":{"id":20636070,"uuid":"90441810","full_name":"clarkbw/jest-localstorage-mock","owner":"clarkbw","description":"A module to mock window.localStorage and window.sessionStorage in Jest","archived":false,"fork":false,"pushed_at":"2024-09-27T09:18:10.000Z","size":1259,"stargazers_count":320,"open_issues_count":8,"forks_count":34,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-11T23:01:57.073Z","etag":null,"topics":["javascript","jest","jest-tests","localstorage","mock","react","sessionstorage","testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/clarkbw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-06T06:17:33.000Z","updated_at":"2025-03-05T20:54:50.000Z","dependencies_parsed_at":"2024-01-12T10:26:58.676Z","dependency_job_id":"bdfeb222-8713-4045-99cf-e2bef1e8aeb2","html_url":"https://github.com/clarkbw/jest-localstorage-mock","commit_stats":{"total_commits":151,"total_committers":15,"mean_commits":"10.066666666666666","dds":0.6754966887417219,"last_synced_commit":"afd695bca6d81d8848af63424737193797585e0f"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarkbw%2Fjest-localstorage-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarkbw%2Fjest-localstorage-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarkbw%2Fjest-localstorage-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarkbw%2Fjest-localstorage-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/clarkbw","download_url":"https://codeload.github.com/clarkbw/jest-localstorage-mock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337616,"owners_count":22054254,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","jest","jest-tests","localstorage","mock","react","sessionstorage","testing"],"created_at":"2024-08-03T17:00:30.581Z","updated_at":"2025-05-15T12:06:54.404Z","avatar_url":"https://github.com/clarkbw.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":["Mocks"],"readme":"Use this module with [Jest](https://facebook.github.io/jest/) to run web tests\nthat rely on `localstorage` and / or `sessionStorage` where you want a working\nlocalStorage API with mocked functions.\n\nThis module has no runtime dependencies so your project won't pull in additional\nmodule dependencies by using this.\n\n[![npm](https://img.shields.io/npm/v/jest-localstorage-mock.svg)](https://www.npmjs.com/package/jest-localstorage-mock)\n[![npm](https://img.shields.io/npm/l/jest-localstorage-mock.svg)](https://github.com/clarkbw/jest-localstorage-mock/blob/master/LICENSE)\n[![Codecov](https://img.shields.io/codecov/c/github/clarkbw/jest-localstorage-mock.svg)](https://codecov.io/gh/clarkbw/jest-localstorage-mock)\n[![Greenkeeper badge](https://badges.greenkeeper.io/clarkbw/jest-localstorage-mock.svg)](https://greenkeeper.io/)\n[![Twitter](https://img.shields.io/twitter/url/https/github.com/clarkbw/jest-localstorage-mock.svg?style=social)](https://twitter.com/intent/tweet?text=Wow:\u0026url=%5Bobject%20Object%5D)\n\n# Jest 24+\n\nNote that with `jest@24` and above this project potentially duplicating functionality.\n\n## Install\n\nThis should only be installed as a development dependency (`devDependencies`) as\nit is only designed for testing. The module is transpiled via\n[babel](https://github.com/babel/babel) to support the current active Node LTS\nversion (6.11.3).\n\nyarn:\n\n```bash\nyarn add --dev jest-localstorage-mock\n```\n\nnpm:\n\n```bash\nnpm i --save-dev jest-localstorage-mock\n```\n\n## Setup\n\nThe simplest setup is to use the [module](#module) system, you may also choose\nto create a [setup file](#setup-file) if needed.\n\n### Module\n\nIn your `package.json` under the `jest`\n[configuration section](https://facebook.github.io/jest/docs/en/configuration.html#content)\ncreate a `setupFiles` array and add `jest-localstorage-mock` to the array. Also, ensure you have *not* enabled [`resetMocks`](https://jestjs.io/docs/en/configuration#resetmocks-boolean).\n\n```json\n{\n  \"jest\": {\n    \"resetMocks\": false,\n    \"setupFiles\": [\"jest-localstorage-mock\"]\n  }\n}\n```\n\nIf you already have a `setupFiles` attribute you can also append\n`jest-localstorage-mock` to the array.\n\n```json\n{\n  \"jest\": {\n    \"resetMocks\": false,\n    \"setupFiles\": [\"./__setups__/other.js\", \"jest-localstorage-mock\"]\n  }\n}\n```\n\n### Setup file\n\nAlternatively you can create a new setup file which then requires this module or\nadd the `require` statement to an existing setup file.\n\n`__setups__/localstorage.js`\n\n```js\nimport 'jest-localstorage-mock';\n// or\nrequire('jest-localstorage-mock');\n```\n\nAdd that file to your `setupFiles` array:\n\n```json\n\"jest\": {\n  \"setupFiles\": [\n    \"./__setups__/localstorage.js\"\n  ]\n}\n```\n\n### In create-react-app\n\nFor a [create-react-app](https://github.com/facebookincubator/create-react-app)\nproject you can replace the\n[suggested mock](https://github.com/facebookincubator/create-react-app/tree/master/packages/react-scripts/template#srcsetuptestsjs-1)\nwith this at the beginning of the existing `src/setupTests.js` file:\n\n```js\nrequire('jest-localstorage-mock');\n```\n\nYou must also override some of create-react-app's default jest configuration. You can do so in your `package.json`:\n\n```json\n{\n  \"jest\": {\n    \"resetMocks\": false\n  }\n}\n```\n\nFor more information, see [#125](https://github.com/clarkbw/jest-localstorage-mock/issues/125).\n\n## In tests\n\nBy including this in your Jest setup you'll allow tests that expect a\n`localStorage` and `sessionStorage` object to continue to run. The module can\nalso allow you to use the mocks provided to check that your localStorage is\nbeing used as expected.\n\nThe `__STORE__` attribute of `localStorage.__STORE__` or\n`sessionStorage.__STORE__` is made available for you to directly access the\nstorage object if needed.\n\n### Test Examples\n\nCheck that your `localStorage` calls were made when they were supposed to.\n\n```js\ntest('should save to localStorage', () =\u003e {\n  const KEY = 'foo',\n    VALUE = 'bar';\n  dispatch(action.update(KEY, VALUE));\n  expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);\n  expect(localStorage.__STORE__[KEY]).toBe(VALUE);\n  expect(Object.keys(localStorage.__STORE__).length).toBe(1);\n});\n```\n\nCheck that your `sessionStorage` is empty, examples work with either\n`localStorage` or `sessionStorage`.\n\n```js\ntest('should have cleared the sessionStorage', () =\u003e {\n  dispatch(action.reset());\n  expect(sessionStorage.clear).toHaveBeenCalledTimes(1);\n  expect(sessionStorage.__STORE__).toEqual({}); // check store values\n  expect(sessionStorage.length).toBe(0); // or check length\n});\n```\n\nCheck that `localStorage` calls were not made when they shouldn't have been.\n\n```js\ntest('should not have saved to localStorage', () =\u003e {\n  const KEY = 'foo',\n    VALUE = 'bar';\n  dispatch(action.notIdempotent(KEY, VALUE));\n  expect(localStorage.setItem).not.toHaveBeenLastCalledWith(KEY, VALUE);\n  expect(Object.keys(localStorage.__STORE__).length).toBe(0);\n});\n```\n\nReset your `localStorage` data and mocks before each test to prevent leaking.\n\n```js\nbeforeEach(() =\u003e {\n  // to fully reset the state between tests, clear the storage\n  localStorage.clear();\n  // and reset all mocks\n  jest.clearAllMocks();\n  \n  // clearAllMocks will impact your other mocks too, so you can optionally reset individual mocks instead:\n  localStorage.setItem.mockClear();\n});\n\ntest('should not impact the next test', () =\u003e {\n  const KEY = 'foo',\n    VALUE = 'bar';\n  dispatch(action.update(KEY, VALUE));\n  expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);\n  expect(localStorage.__STORE__[KEY]).toBe(VALUE);\n  expect(Object.keys(localStorage.__STORE__).length).toBe(1);\n});\n\ntest('should not be impacted by the previous test', () =\u003e {\n  const KEY = 'baz',\n    VALUE = 'zab';\n  dispatch(action.update(KEY, VALUE));\n  expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);\n  expect(localStorage.__STORE__[KEY]).toBe(VALUE);\n  expect(Object.keys(localStorage.__STORE__).length).toBe(1);\n});\n```\n\nSee the [contributing guide](./CONTRIBUTING.md) for details on how you can\ncontribute.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclarkbw%2Fjest-localstorage-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclarkbw%2Fjest-localstorage-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclarkbw%2Fjest-localstorage-mock/lists"}