- Notifications
You must be signed in to change notification settings - Fork0
In-memory opfs (origin private file system) mock for Jest and Vitest.
License
jurerotar/opfs-mock
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
In-memory implementation of theorigin private file system. Its main utility is for testing OPFS-dependent code in Node.js. It's testedon Node.js versions 20-25.
npm install -save-dev opfs-mock
opfs-mockrequiresNode.js v20.0.0 or higher. It can work on Node v18.0.0 with either--experimental-fetchflag enabled or a globalFilepolyfill.jsdomtesting environment is missingFile.prototype.text()method, which is required for reading opfs files. Ensure your opfs-dependant tests are raninnodeorhappy-domenvironment.
It replicates the behavior of origin private file system, except data is not persisted to disk.
The easiest way to use it is to importopfs-mock, which will polyfill OPFS API to global scope.
import"opfs-mock";
Alternatively, you can explicitly importstorageFactory:
import{storageFactory}from"opfs-mock";test('Your test',async()=>{conststorage=awaitstorageFactory();constroot=awaitstorage.getDirectory();constdirectoryHandle=awaitroot.getFileHandle('test-file.txt',{create:true});// rest of your test});
storageFactory hasquota andusage values set to1024 ** 3 (1 GB) and0 respectively. When callingstorage.estimate(),usage is dynamically calculated by summing the predefined usage value and any additional computed storage consumption.In case you need specific values, you can pass both as arguments tostorageFactory.
import{storageFactory}from"opfs-mock";test('Your test',async()=>{conststorage=awaitstorageFactory({quota:1_000_000,usage:1_000});constroot=awaitstorage.getDirectory();constdirectoryHandle=awaitroot.getFileHandle('test-file.txt',{create:true});// rest of your test});
To useopfs-mock in a single Vitest test suite, requireopfs-mock at the beginning of the test file, as described above.
To use it on all Vitest tests without having to include it in each file, add the auto setup script to thetest.setupFiles in your Vite config:
// vite.config.tsimport{defineConfigasdefineViteConfig,mergeConfig}from'vite';import{defineConfigasdefineVitestConfig}from'vitest/config';constviteConfig=defineViteConfig({ ...});constvitestConfig=defineVitestConfig({test:{setupFiles:['opfs-mock'],},});exportdefaultmergeConfig(viteConfig,vitestConfig);
Alternatively you can create a new setup file which then imports this module.
// vitest-setup.tsimport"opfs-mock";
Add that file to yourtest.setupFiles array:
// vite.config.tsimport{defineConfigasdefineViteConfig,mergeConfig}from'vite';import{defineConfigasdefineVitestConfig}from'vitest/config';constviteConfig=defineViteConfig({ ...});constvitestConfig=defineVitestConfig({test:{setupFiles:['vitest-setup.ts'],},});exportdefaultmergeConfig(viteConfig,vitestConfig);
To useopfs-mock in a single Jest test suite, requireopfs-mock at the beginning of the test file, as described above.
To use it on all Jest tests without having to include it in each file, add the auto setup script to thesetupFiles in your Jest config:
// jest.config.js{ ..."setupFiles":["opfs-mock"]}
Alternatively you can create a new setup file which then imports this module.
// jest-setup.tsimport"opfs-mock";
Add that file to yoursetupFiles array:
// jest.config.js{ ..."setupFiles":["jest-setup"]}
If you are keeping your tests completely isolated you might want to "reset" the state of the mocked OPFS. You can do this by usingresetMockOPFS function, which creates a completely new instance of the mock.
import{resetMockOPFS}from'opfs-mock';beforeEach(()=>{resetMockOPFS();});test('First isolated test',async()=>{// rest of your test});test('Second isolated test',async()=>{// rest of your test});
About
In-memory opfs (origin private file system) mock for Jest and Vitest.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.