npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

magicmock

v1.4.0

Published

Mocked objects with ES6

Downloads

4

Readme

Install

npm i -D --save-dev magicmock

Prerequisite

Use magicmock with Node.js/io.js (with --harmony_proxies). Currently we only test it against Node.js 4 and Node.js 5. To request more engine support, please kindly send in an issue or pull request.

Usage

Mock method - mockMethod(options)

The terms method and function are interchangeable here.

import {mockMethod} from 'magicmock';

let mockedMethod = mockMethod();
mockedMethod.returnValue = 100;
mockedMethod(1, 2, 3); // => 100
console.log(mockedMethod.called()); // => true
console.log(mockedMethod.calledWith(1, 2, 3)); // => true
console.log(mockedMethod.calledWith(1, 2)); // => false
mockedMethod.returnValue / options.returnValue

Setting returnValue property of the mockedMethod changes the return value when executing mockedMethod.

returnValue is also accepted as an option to mockMethod() factory method.

let mockedMethod = mockMethod({
  returnValue: 100
});
mockedMethod(1, 2, 3); // => 100
options.sideEffect

options.sideEffect allows mockedMethod returns different values depending on side effects from outside world. If sideEffect was defined, returnValue won't not work anymore.

let method = mockMethod({
  sideEffect: [1, 4, 2]
});
method(); // => 1
method(); // => 4
method(); // => 2
method(); // => undefined

It also accepts a mapping function.

let resultMap = {'a': 1, 'b': 2, 'c': 3}
let method = mockMethod({
  sideEffect: (key) => resultMap[key]
});
method('a'); // => 1
method('b'); // => 2
method('c'); // => 3
method('d'); // => undefined

mockedMethod can raise an error defined by options.sideEffect.

let mockedMethod = mockMethod({
  sideEffect: new Error('My GOSH!')
});
mockedMethod(); // => throws error
mockedMethod.callCount()

Returns how many times was the method called.

mockedMethod.called()

Returns a boolean. Indicates whether the method was called or not.

mockedMethod.calledWith(args)

Returns a boolean. Indicates whether the method was called with exactly the same argument list or not.

mockedMethod.getInvocationHistory()

Returns an array of argument lists which mockedMethod was called.

let method = mockMethod();
method();
method(1);
method({a: 1});
method.getInvocationHistory(); // => [[], [1], [{a: 1}]]

Mock object - mockObject(options)

mockObject() factory method creates an object mockedObject. When you access any of undefined property of mockedObject, it returns a mockedMethod instead of undefined.

let mockedObject = mockObject();
// mockedObject.testMethod is undefined
mockedObject.testMethod.returnValue = 101;
console.log(mockedObject.testMethod()); // => 101

You could still assign property value to mockedObject after its creation.

let mockedObject = mockObject();
// mockedObject.testMethod is undefined
mockedObject.testMethod.returnValue = 101;
console.log(mockedObject.testMethod()); // => 101
mockedObject.testMethod = 'not a method anymore';
console.log(mockedObject.testMethod()); // => 'not a method anymore'

mockObject() factory method accepts an option with props key, which defines properties upon creation.

let mockedObject = mockObject({
  props: {
    myProperty: 'this is a string'
  }
});
console.log(mockedObject.myProperty); // => this is a string

Use for testing only

magicmock was designed for testing. You don't want to run magicmock code in production code, neither client-side nor server-side.

If you are using mocha to run test, simply add flag --harmony_proxies. You'd better use some ES6 transpiler like babel.

mocha --compilers js:babel-register --harmony_proxies

License

MIT © tjwudi