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

mockmock

v0.0.7

Published

Mocking library for minimalists and people that don't want to remember 500 different methods.

Downloads

184

Readme

mockmock

Build Status npm js-standard-style

Mocking library for minimalists and people that don't want to remember 500 different methods.

Curiout Chicken

Why?

I like sinon, but I am not a big fan of forcing users to remember the difference between stubs, spies and mocks. There is no need for a ton of different methods. mockmock is a much simpler approach that offers a sufficient solution for the majority of use-cases.

Example

mockmock exports a single function mock. mock accepts the function to be mocked or a constant value to be returned:

var mocked = mock(function (color, fruit) {
  console.log('A', color, fruit)
})

mocked('red', 'apple') // #=> A red apple
mocked('yellow', 'banana') // #=> A yellow banana
mocked('green', 'cucumber') // #=> A green cucumber
var mocked = mock(123)

console.log(mocked()) // #=> 123

Apart from defining behavior, the mock function is also a "spy", which means you can access its arguments, thisArg etc.:

var mocked = mock()

mocked(1, 2, 3)
console.log(mocked.args) // #=> [[1, 2, 3]]

mocked(4, 5, 6)
console.log(mocked.args) // #=> [[1, 2, 3], [4, 5, 6]]

mocked.apply({}, [])
console.log(mocked.thisValues) // #=> [this, this, {}]

Install

With npm do:

npm i mockmock -S

mockmock can then be required as follows:

var mock = require('mockmock')

or:

var mock = require('mockmock').mock

API

If you are familiar with sinon, a lot of the methods might look familiar to you.

mock() #=> mockedFn

Either accepts a constant value to be returned using an identity function or a function to be mocked. If no argument is provided, a noop function will be used.

var mockedIdentityFn = mock('123')
console.log(mockedIdentityFn()) // #=> '123'

var mockedFn = mock(function () {
  return 'hello'
})
console.log(mockedFn()) // #=> 'hello'

var mockedNoop = mocke()
console.log(mockedNoop()) // #=> undefined

args

An array of recorded arguments. arguments will be converted to an actual array to allow easy usage of deepEqual methods.

var mocked = mock(function () {})
mocked(1, 2, 3)
mocked(4, 5, 6)
console.log(mocked.args) // #=> [[1, 2, 3], [4, 5, 6]]

thisValues

Similar to args, records the thisValue the mocked function has been called with.

var mocked = mock(function () {})
mocked.call({ a: 'a' })
mocked.call({ b: 'b' })
console.log(mocked.thisValues) // #=> [{ a: 'a' }, { b: 'b' }]

returnValues

Similar to args, records the return values of the mocked function.

var i = 0
var mocked = mock(function () {
  return i++
})
mocked()
mocked()
mocked()
console.log(mocked.returnValues) // #=> [0, 1, 2]

calls

Keeps track of when (unix timestamp) the mocked function has been called.

var mocked = mock()
mocked()
mocked()
mocked()
console.log(mocked.calls) // #=> [1445386361361, 1445386361365, 1445386361369]

errors

Records the errors thrown by the function that has been mocked. Note that thrown errors will be passed through the mock function.

var error = new Error()
var mocked = mock(function () { throws error })
try { mocked() } catch (e) {}
console.log(mocked.errors) // #=> [error]

callCount

How many times has the mocked function been called?

var mocked = mock()
mocked()
mocked()
mocked()
console.log(mocked.callCount) // #=> 3

called

If the mocked function has been called at all.

var mocked = mock()
console.log(mocked.called) // #=> false
mocked()
console.log(mocked.called) // #=> true

calledOnce

calledTwice

calledThrice

If the mocked function has been called exactly once, twice or three times.

var mocked = mock()
console.log(mocked.calledOnce) // #=> false
mocked()
console.log(mocked.calledOnce) // #=> true
mocked()
console.log(mocked.calledOnce) // #=> false
console.log(mocked.calledTwice) // #=> true

firstCall

secondCall

thirdCall

lastCall

Returns an object describing the first, second, third or last call.

var mocked = mock('hello world')
mocked(1, 2, 3)
console.log(mocked.firstCall) // #=> { thisValue: this, args: [1, 2, 3], returnValue: 'hello world', error: undefined }

clear, flush, reset

Resets the internal spy. All recorded arguments, errors, return values etc. will be reset.

var mocked = mock()
mocked()
console.log(mocked.called) // #=> true
mocked.clear()
console.log(mocked.called) // #=> false

mockFn

The function that is being mocked. Identity function of the passed in value when a constant value has been supplied.

var fn = function () {}
var mocked = mock(fn)
console.log(mocked.mockFn, fn) // #=> true

mockValue

Equivalent of mockFn for mocked identities. References the supplied constant value (if any).

var c = 123
var mocked = mock(c)
console.log(mocked.mockValue, c) // #=> true

For more usage examples, have a look at the test suite.

Credits

License

ISC