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

mock-require

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-require

Simple, intuitive mocking of Node.js modules.

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
186K
increased by4.27%
Maintainers
1
Weekly downloads
 
Created

What is mock-require?

The mock-require npm package allows you to mock Node.js modules during testing. This is particularly useful for unit tests where you want to isolate the module under test from its dependencies.

What are mock-require's main functionalities?

Mocking a module

This feature allows you to replace a module with a mock implementation. In this example, the 'fs' module is mocked to return 'mocked content' when 'readFileSync' is called.

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

// Mock the 'fs' module
mock('fs', {
  readFileSync: () => 'mocked content'
});

const fs = require('fs');
console.log(fs.readFileSync('somefile.txt')); // Outputs: 'mocked content'

// Stop mocking
mock.stop('fs');

Mocking a module with a factory function

This feature allows you to use a factory function to create the mock implementation. This can be useful if the mock needs to maintain some state or if the mock implementation is more complex.

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

// Mock the 'fs' module with a factory function
mock('fs', () => {
  return {
    readFileSync: () => 'mocked content from factory'
  };
});

const fs = require('fs');
console.log(fs.readFileSync('somefile.txt')); // Outputs: 'mocked content from factory'

// Stop mocking
mock.stop('fs');

Mocking a module conditionally

This feature allows you to conditionally mock a module based on some runtime condition, such as an environment variable. This can be useful for setting up different mocks for different environments.

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

if (process.env.NODE_ENV === 'test') {
  mock('fs', {
    readFileSync: () => 'mocked content for test'
  });
}

const fs = require('fs');
console.log(fs.readFileSync('somefile.txt')); // Outputs: 'mocked content for test' if NODE_ENV is 'test'

// Stop mocking
mock.stop('fs');

Other packages similar to mock-require

Keywords

FAQs

Package last updated on 28 May 2016

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