Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A faking library
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


A faking library

I must immediately apologize for the name; it is a horrible pun combining fzk (a shortening of the name of my compay) and fakes. But it does ring clear, and I want it to be short and unique. So there it is.

How to use

Creating fakes

var fzkes = require('fzkes')
var fake = fzkes.fake()
var fs = require('fs')
fzkes.fake(fs, 'readFile')

Restoring original functions

There are three ways to easily doing this, depending on the scope:

  1. Restoring a single fake: fake.restore()
  2. Restoring all fakes across the board: fzkes.restore()

The last is a bit more tricky; A sub-scope can be creating by calling fzkes.scope(). The scope have all methods (except chai) that the original fzkes object have, except the restore() function on a scope only affects fakes created within that scope.

Injecting data

fake.throws(new Error('some error'))
fake.calls(function() { console.log('was called') })

// If it replaced a function on an object:

// Conditional injects

Each action (returns, throws, calls, etc) returns the fake, to make it easier to assign to return values and the like:

    a: fzkes.fake('a').returns(3),
    b: fzkes.fake('b').throws()

It can also chain the withArgs() automatically:

var fake = fzkes.fake('name')
    .withArgs(1,4).throws(new Error('some error'))
fake(1,1) // => 1
fake(1,2) // => 3
fake(1,3) // => 4
fake(1,4) // => throws

Advanced withArgs

There is a more advanced form of withArgs called withComplexArgs. It allows for skipping arguments entirely, as well as defining regular expressions to validate against strings.

var fake = fzkes.fake('name')
    .withComplexArgs(null, { value: 2 }).returns(2)
    .withComplexArgs({ regex: /ab?c/ }).returns(3)

fake(1, 1) // => 1
fake(1, 2) // => 2
fake('a', 2) // => 2
fake('abc') // => 3
fake('ac') // => 3

Calling callbacks

// Default is calling the last function found, node-style

// It can be controlled
fake.callsArg({ arg: 'first' })
// 0-indexed argument list
fake.callsArg({ arg: 1 })

// It defaults to calling the callback immediately, but this can be changed
fake.callsArg({ async: true })

// Default is no parameters to the callback, but these can be controlled
fake.callsArg({ arguments: [ 1, 2 ] })

Emulating calls after they have been called

Sometimes, it is not feasible to prepare the fake properly; in these cases, emulating the call after the fact makes the code much better.

fzkes supports this as an option for the fake.calls(), fake.callsArg() and fake.callsOriginal() functions.

The code would look as the following:


fake.calls(fn, { now: true })
fake.callsOriginal({ now: true })
fake.callsArg({ now: true })

It works with all other options on the fake.callsArg() call.

It forwards the next unhandled call as it appeared on the fake, and throws an exception if there are no unhandled calls:

// Goes through
fake.callsOriginal({ now: true })

try {
    fake.calls(fn, { now: true })
} catch(e) {
    // e.message would say that fake had no unhandled calls.

If any of the functions was set up in advance, calls are not considered unhandled, and any call with { now: true } will throw an exception. To begin building unhandled calls, make a fake.calls(null) invocation.

// this call is handled immediately
expect(function() {
    fake.callsOriginal({ now: true })

// resetting the expectations

// it now works again
fake.callsOriginal({ now: true })


fake.wasCalledWith(1, 2, 3)
fake.wasCalledWithExactly(1, 2, 3)
fake.callCount == 2

Using with chai

fake.should.have.been.called // at least once
fake.should.have.been.called(2) // precisely 2 times

Running in the browser

When installing the package, a convenience package is built using browserify. It adds both an AMD wrapper for use with require or a similar tool, and a global version if neither node nor AMD is detected.

The browser-version is located in the root folder, and is called browser.js. It can either be included by <script src="node_modules/fzkes/browser.js"></script>, or copied to a lib-folder of your choosing.

Then simply follow the guide above for setting it up and interacting with it.

NOTE: The 0.10 version of browser.js is incompatible with earlier versions, because of how it is now wrapped. This should be for the better though.

Something went wrong with that request. Please try again.