{"id":15813759,"url":"https://github.com/jurerotar/opfs-mock","last_synced_at":"2026-05-06T07:03:55.523Z","repository":{"id":257809617,"uuid":"867747347","full_name":"jurerotar/opfs-mock","owner":"jurerotar","description":"In-memory implementation of the origin private file system for usage in your Jest or Vitest tests.","archived":false,"fork":false,"pushed_at":"2025-01-16T16:56:17.000Z","size":39,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-23T09:09:53.024Z","etag":null,"topics":["browser","jest","nodejs","opfs","storage","testing","typescript","vite","vitest"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/opfs-mock","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jurerotar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"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":"2024-10-04T16:34:18.000Z","updated_at":"2025-01-16T11:06:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"8a8b5767-d3ec-4fe3-b395-daa32f85a205","html_url":"https://github.com/jurerotar/opfs-mock","commit_stats":null,"previous_names":["jurerotar/opfs-mock"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jurerotar%2Fopfs-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jurerotar%2Fopfs-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jurerotar%2Fopfs-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jurerotar%2Fopfs-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jurerotar","download_url":"https://codeload.github.com/jurerotar/opfs-mock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250023531,"owners_count":21362417,"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":["browser","jest","nodejs","opfs","storage","testing","typescript","vite","vitest"],"created_at":"2024-10-05T04:05:32.893Z","updated_at":"2026-05-06T07:03:55.518Z","avatar_url":"https://github.com/jurerotar.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# opfs-mock\n\nIn-memory implementation of the [origin private file system](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system). Its main utility is for testing OPFS-dependent code in Node.js. It's tested\non all supported Node.js versions (`22.x`, `24.x`, `25.x`, `26.x`).\n\n## Installation\n\n```shell\nnpm install -save-dev opfs-mock\n```\n\n## Limitations\n\n- `opfs-mock` requires **Node.js v20.0.0** or higher. It can work on Node v18.0.0 with either `--experimental-fetch` flag enabled or a global\n`File` polyfill.\n\n- `jsdom` testing environment is missing `File.prototype.text()` method, which is required for reading opfs files. Ensure your opfs-dependant tests are ran\nin `node` or `happy-dom` environment.\n\n## Usage\n\nIt replicates the behavior of origin private file system, except data is not persisted to disk.\n\nThe easiest way to use it is to import `opfs-mock`, which will polyfill OPFS API to global scope.\n\n```ts\nimport \"opfs-mock\";\n```\n\nAlternatively, you can explicitly import `storageFactory`:\n\n```ts\nimport { storageFactory } from \"opfs-mock\";\n\ntest('Your test', async () =\u003e {\n  const storage = await storageFactory();\n  const root = await storage.getDirectory();\n  const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });\n  // rest of your test\n});\n```\n\n`storageFactory` has `quota` and `usage` values set to `1024 ** 3 (1 GB)` and `0` respectively. When calling `storage.estimate()`, `usage` is dynamically calculated by summing the predefined usage value and any additional computed storage consumption.\nIn case you need specific values, you can pass both as arguments to `storageFactory`.\n\n```ts\nimport { storageFactory } from \"opfs-mock\";\n\ntest('Your test', async () =\u003e {\n  const storage = await storageFactory({ quota: 1_000_000, usage: 1_000 });\n  const root = await storage.getDirectory();\n  const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });\n  // rest of your test\n});\n```\n\n### Vitest\n\nTo use `opfs-mock` in a single Vitest test suite, require `opfs-mock` at the beginning of the test file, as described above.\n\nTo use it on all Vitest tests without having to include it in each file, add the auto setup script to the `test.setupFiles` in your Vite config:\n\n```ts\n// vite.config.ts\n\nimport { defineConfig as defineViteConfig, mergeConfig } from 'vite';\nimport { defineConfig as defineVitestConfig } from 'vitest/config';\n\nconst viteConfig = defineViteConfig({\n  ...\n});\n\nconst vitestConfig = defineVitestConfig({\n  test: {\n    setupFiles: ['opfs-mock'],\n  },\n});\n\nexport default mergeConfig(viteConfig, vitestConfig);\n```\n\nAlternatively you can create a new setup file which then imports this module.\n\n```ts\n// vitest-setup.ts\n\nimport \"opfs-mock\";\n```\n\nAdd that file to your `test.setupFiles` array:\n\n```ts\n// vite.config.ts\n\nimport { defineConfig as defineViteConfig, mergeConfig } from 'vite';\nimport { defineConfig as defineVitestConfig } from 'vitest/config';\n\nconst viteConfig = defineViteConfig({\n  ...\n});\n\nconst vitestConfig = defineVitestConfig({\n  test: {\n    setupFiles: ['vitest-setup.ts'],\n  },\n});\n\nexport default mergeConfig(viteConfig, vitestConfig);\n```\n\n\n### Jest\n\nTo use `opfs-mock` in a single Jest test suite, require `opfs-mock` at the beginning of the test file, as described above.\n\nTo use it on all Jest tests without having to include it in each file, add the auto setup script to the `setupFiles` in your Jest config:\n\n```ts\n// jest.config.js\n\n{\n  ...\n  \"setupFiles\": [\n    \"opfs-mock\"\n  ]\n}\n```\n\nAlternatively you can create a new setup file which then imports this module.\n\n```ts\n// jest-setup.ts\n\nimport \"opfs-mock\";\n```\n\nAdd that file to your `setupFiles` array:\n\n```ts\n// jest.config.js\n\n{\n  ...\n  \"setupFiles\": [\n    \"jest-setup\"\n  ]\n}\n```\n\n## Wiping/resetting the OPFS mock for a fresh state\n\nIf you are keeping your tests completely isolated you might want to \"reset\" the state of the mocked OPFS. You can do this by using `resetMockOPFS` function, which creates a completely new instance of the mock.\n\n```ts\nimport { resetMockOPFS } from 'opfs-mock';\n\nbeforeEach(() =\u003e {\n  resetMockOPFS();\n});\n\ntest('First isolated test', async () =\u003e {\n  // rest of your test\n});\n\ntest('Second isolated test', async () =\u003e {\n  // rest of your test\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjurerotar%2Fopfs-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjurerotar%2Fopfs-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjurerotar%2Fopfs-mock/lists"}