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

foggy

v1.1.1

Published

spy, stub for ES6 generator based on co

Downloads

11

Readme

foggy

spy, stub library for generators running on co.

Installation

Foggy require Generator Feature enabled environment, which could run co library. Node(>=0.11.13) and iojs are ok for foggy. So please remember enable harmony features.

Install

$ npm install foggy

Spy

Quike Start

var foggy = require('foggy');
var co = require('co');

var context = {
  generator: function* () {
    return yeild Promise.resolve(true);
  }
};

var spy = foggy.spy(context, 'generator');
co(context.generator).then(function (value) {
  console.log(value); // true
  console.log(spy.called); // true
});

Create a spy

Create a spy by foggy.spy() method:

Anonymous spy

Create a anonymous spy.

var spy = foggy.spy();

Spy on provided generator

Simply pass the generator in first argument.

var spy = foggy.spy(generator);

Spy on object's generator

Create a proxy generator to replace object.generator with same return value. spy.reset() method could replace the original generator back.

var object = { 
  generator: function* () { 
    /* yields and return */ 
  } 
};

var spy = foggy.spy(object, 'generator');

Spy API

One spy that contains properties / methods listed as below:

calledCount

The number of recorded calls.

called

return true if the spy was called at least once.

calledOnce

return true if the spy was called exactly once.

calledTwice

return true if the spy was called exactly twice.

calledThrice

return true if the spy was called exactly thrice.

getCall(idx)

return the nth call object, which combines the informations of that generator call.

var gen = function* (obj) { return obj; };
var spy = foggy.spy(gen);

co.wrap(spy)({ test: 1 }).then(function () {
  console.log(spy.getCall(0) === spy.firstCall); // true
  console.log(spy.getCall(0).calledWith({ test: 1 })); // true
});

firstCall

return the first call.

secondCall

return the second call

thirdCall

return the third call

calledWith

TODO

Call API

one call object's properties / methods:

calledWith(arg1[, arg2, ...])

Returns true if the call was called with the provided arguments.

var gen = function* (obj) { return obj; };
var spy = foggy.spy(gen);
var arg0 = {}, arg1 = [];

co.wrap(spy)(arg0, arg1).then(function () {
  console.log(spy.firstCall.calledWith(arg0, arg1)); // true
  console.log(spy.firstCall.calledWith(arg1, arg0)); // false
});

thisValue

Returns the this value for this generator call.

var context = { gen: function* (obj) { return obj; } };
var spy = foggy.spy(context, 'gen');

co(spy).then(function () {
  console.log(spy.firstCall.thisValue === context); // true
});

returnValue

returns the "return value" for the generator's result.

var gen = function* (a, b) { return a + b; };
var spy = foggy.spy(gen);

co.wrap(spy)(1, 2).then(function () {
  console.log(spy.firstCall.returnValue); // 3
});

Stub

Quick Start

var foggy = require('foggy');
var co = require('co');

var context = {
  generator: function* () {
    return yield Promise.resolve(true);
  }
};

var stub = foggy.stub(context, 'generator').returns(1);
co(context.generator).then(function(val) {
  console.log(val); // 1
});

Create a stub

Create a stub by foggy.stub() method:

Anonymous stub

Create an anonymous stub.

var stub = foggy.stub();

Stub on object's generator

Create a stub generator to replace object.generator. stub.reset() method could replace the original generator back.

var object = { 
  generator: function* () { 
    /* yields and return */ 
  } 
};

var stub = foggy.stub(object, 'generator');

Stub API

One stub generator contains properties / methods listed as below:

returns(value)

make stub return a provided value which could be any object. If empty, it would return undefined by default.

var stub = foggy.stub().returns(100);
co(stub).then(console.log); // 100

throws(err)

make stub throw a provided err which could be any object. If empty, it would throws an Error object.

var stub = foggy.stub().throws(100);
co(stub).catch(console.error); // 100

calls(anotherGenerator)

make stub return value from another generator.

var context = { 
  gen: function* () { 
    return 0; 
  }
};

var replacement = function* () { 
  return 1; 
};

var stub = foggy.stub(context, 'gen').calls(replacement);
co(stub).then(console.log); // 1;

onCall(index)

configure stub for a specific call.

var context = { 
  gen: function* () { 
    return 0; 
  }
};

var stub = foggy.stub(context, 'gen');
stub.onCall(0).returns(1);
stub.onCall(1).returns(2);

co(stub).then(console.log); // 1
co(stub).then(console.log); // 2
co(stub).then(console.log); // undefined

onFirstCall

configure stub for first call.

onSecondCall

configure stub for second call

onThirdCall

configure stub for third call

withArgs(arg1[, arg2 ... ])

configure stub for a specific arguments.

var stub = foggy.stub().returns(10);
stub.withArgs(1, 2).returns(100);

co.wrap(stub)(2, 1).then(console.log); // 10
co.wrap(stub)(1, 2).then(console.log); // 100

reset()

reset a stub which would replace the original generator back on the object.

var obj = {
  generator: function* () {
    return 0;
  }
};

var stub = foggy.stub(obj, 'generator').returns(1);
co(obj.generator).then(function(val) {
  console.log(val); // 1
  stub.reset();

  co(obj.generator).then(function(resetedReturnValue) {
    console.log(resetedReturnValue); // 0
  });
});

Mock

TODO

License

The MIT License (MIT)

Copyright (c) 2015 Wang Qiu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.