Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

In-memory opfs (origin private file system) mock for Jest and Vitest.

License

NotificationsYou must be signed in to change notification settings

jurerotar/opfs-mock

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.

Installation

npm install -save-dev opfs-mock

Limitations

  • opfs-mock requiresNode.js v20.0.0 or higher. It can work on Node v18.0.0 with either--experimental-fetch flag enabled or a globalFile polyfill.

  • jsdom testing environment is missingFile.prototype.text() method, which is required for reading opfs files. Ensure your opfs-dependant tests are raninnode orhappy-dom environment.

Usage

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});

Vitest

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);

Jest

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"]}

Wiping/resetting the OPFS mock for a fresh state

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

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp