Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-fs

Package Overview
Dependencies
Maintainers
1
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-fs

A configurable mock file system. You know, for testing.

  • 5.1.0-dev.1632400547829
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
504K
decreased by-2.92%
Maintainers
1
Weekly downloads
 
Created

What is mock-fs?

The mock-fs npm package allows you to mock the file system in Node.js, enabling you to test code that interacts with the file system without actually performing any real file system operations. This is particularly useful for unit testing and ensuring that your code behaves correctly in different file system scenarios.

What are mock-fs's main functionalities?

Mocking a Simple File System

This feature allows you to create a simple mock file system with directories and files. The `mock` function takes an object that represents the file system structure. After setting up the mock file system, you can run your code that interacts with the file system. Finally, you call `mock.restore()` to clean up and restore the real file system.

const mock = require('mock-fs');

mock({
  'path/to/fake/dir': {
    'some-file.txt': 'file content here',
    'empty-dir': {}
  }
});

// Your code that interacts with the file system

mock.restore();

Mocking File System Errors

This feature allows you to simulate file system errors, such as permission errors. In this example, the `mock.file` function is used to create a file with no permissions. When attempting to read the file, an error is thrown, which you can catch and handle in your code.

const mock = require('mock-fs');
const fs = require('fs');

mock({
  'path/to/fake/dir': {
    'some-file.txt': mock.file({
      content: 'file content here',
      mode: 0o000 // no permissions
    })
  }
});

try {
  fs.readFileSync('path/to/fake/dir/some-file.txt');
} catch (err) {
  console.error('Error reading file:', err);
}

mock.restore();

Mocking a Large File System

This feature allows you to create a large mock file system with many files. The `mock.directory` function is used to create a directory with a large number of files. This is useful for testing how your code handles large file systems.

const mock = require('mock-fs');

mock({
  'large-dir': mock.directory({
    items: Array.from({ length: 1000 }, (_, i) => ({
      [`file-${i}.txt`]: `content of file ${i}`
    })).reduce((acc, item) => Object.assign(acc, item), {})
  })
});

// Your code that interacts with the large file system

mock.restore();

Other packages similar to mock-fs

Keywords

FAQs

Package last updated on 23 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc