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

ez-mock

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ez-mock

Yet another mocking framework for Node

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

ez-mock

alt text

Simple mocking of require modules.

About

I found myself repeating a similar pattern for setting up mocks for unit tests. This package encapsulates that pattern and provides a simple, yet robust, interface.

Example

We want to test a simple module addToDBValue.js that adds an input value to a database value.

// addToDBValue.js
const queryDatabase = require('./database.js');

const addToDBValue = (inputValue) => {
  return inputValue + queryDatabase();
}

module.exports = addToDBValue;

The only issue is that database.js connects to an actual database.

// database.js
const queryDatabase = () => {
  /* database connection and query logic */
}

module.exports = queryDatabase;

But we don't want to touch any external systems for a unit test... We can simply mock that required dependency

// test.js
const {Mock, TestSubjectMocker} = require('ez-mock');

// database mock behavior that returns 1 from query
const databaseMockReturn1 = new Mock('./database.js', () => 1);

// database mock behavior that returns 2 from query
const databaseMockReturn2 = new Mock('./database.js', () => 2);

// set the default database mock to return 1 from query
const testSubjectMocker = new TestSubjectMocker('./addToDBValue.js', databaseMockReturn1);

// instantiate foo using the default database mock
let addToDBValue = testSubjectMocker.generateSubject();
console.log(addToDBValue(1)); // 2
console.log(addToDBValue(2)); // 3

// instantiate foo with an override mock behavior
addToDBValue = testSubjectMocker.generateSubject(databaseMockReturn2);
console.log(addToDBValue(1)); // 3
console.log(addToDBValue(2)); // 4

API

Mock(modulePath, functionality)

modulePath: String

The module name, absolute path, or relative path to the module you wish to mock.

functionality : object/function

The functionality you wish to return when require(...) is called.

TestSubjectMocker(modulePath, defaultMocks)

modulePath: String

The module name, absolute path, or relative path to the module you wish to test with its dependencies mocked.

defaultMocks : Mock/[Mock]

The mock behavior that will always be applied when generating a test subject.

TestSubjectMocker.generateTestSubject(overrideMocks)

defaultMocks : Mock/[Mock]

The mock behavior that overrides the default behavior for this instance of the test subject.

returns : object/function

An instance of the test subject with the mocks applied

TestSubjectMocker.clearMocks()

Clears all registered mocks (even the default mocks)

Keywords

FAQs

Package last updated on 23 Nov 2017

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