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

gently

v1.0.0

Published

<!-- badges/ --> [![Build Status](https://travis-ci.org/felixge/node-gently.svg?branch=master)](https://travis-ci.org/felixge/node-gently) [![npm](https://img.shields.io/npm/v/gently.svg)](https://www.npmjs.com/package/gently) [![Dependency Status](https

Downloads

2,724

Readme

Gently

Build Status npm Dependency Status Development Dependency Status

Purpose

A node.js module that helps with stubbing and behavior verification. It allows you to test the most remote and nested corners of your code while keeping being fully unobtrusive.

Features

  • Overwrite and stub individual object functions
  • Verify that all expected calls have been made in the expected order
  • Restore stubbed functions to their original behavior
  • Detect object / class names from obj.constructor.name and obj.toString()
  • Hijack any required module function or class constructor

Installation

Via npm:

npm install gently@latest

Example

Make sure your dog is working properly:

function Dog() {}

Dog.prototype.seeCat = function() {
  this.bark('whuf, whuf');
  this.run();
}

Dog.prototype.bark = function(bark) {
  require('sys').puts(bark);
}

const gently = new (require('gently'))
  , assert = require('assert')
  , dog = new Dog();

gently.expect(dog, 'bark', function(bark) {
  assert.equal(bark, 'whuf, whuf');
});
gently.expect(dog, 'run');

dog.seeCat();

You can also easily test event emitters with this, for example a simple sequence of 2 events emitted by fs.WriteStream:

const gently = new (require('gently'))
  , stream = new (require('fs').WriteStream)('my_file.txt');

gently.expect(stream, 'emit', function(event) {
  assert.equal(event, 'open');
});

gently.expect(stream, 'emit', function(event) {
  assert.equal(event, 'drain');
});

For a full read world example, check out this test case: test-incoming-form.js (in node-formdiable).

API

Gently

new Gently()

Creates a new gently instance. It listens to the process 'exit' event to make sure all expectations have been verified.

gently.expect(obj, method, [[count], stubFn])

Creates an expectation for an objects method to be called. You can optionally specify the call count you are expecting, as well as stubFn function that will run instead of the original function.

Returns a reference to the function that is getting overwritten.

gently.expect([count], stubFn)

Returns a function that is supposed to be executed count times, delegating any calls to the provided stubFn function. Naming your stubFn closure will help to properly diagnose errors that are being thrown:

childProcess.exec('ls', gently.expect(function lsCallback(code) {
  assert.equal(0, code);
}));

gently.restore(obj, method)

Restores an object method that has been previously overwritten using gently.expect().

gently.hijack(realRequire)

Returns a new require functions that catches a reference to all required modules into gently.hijacked.

To use this function, include a line like this in your 'my-module.js'.

if (global.GENTLY) require = GENTLY.hijack(require);

const sys = require('sys');
exports.hello = function() {
  sys.log('world');
};

Now you can write a test for the module above:

const gently = global.GENTLY = new (require('gently'))
  , myModule = require('./my-module');

gently.expect(gently.hijacked.sys, 'log', function(str) {
  assert.equal(str, 'world');
});

myModule.hello();

gently.stub(location, [exportsName])

Returns a stub class that will be used instead of the real class from the module at location with the given exportsName.

This allows to test an OOP version of the previous example, where 'my-module.js'.

if (global.GENTLY) require = GENTLY.hijack(require);

const World = require('./world');

exports.hello = function() {
  const world = new World();
  world.hello();
}

And world.js looks like this:

const sys = require('sys');

function World() {

}
module.exports = World;

World.prototype.hello = function() {
  sys.log('world');
};

Testing 'my-module.js' can now easily be accomplished:

const gently = global.GENTLY = new (require('gently'))
  , WorldStub = gently.stub('./world')
  , myModule = require('./my-module')
  , WORLD;

gently.expect(WorldStub, 'new', function() {
  WORLD = this;
});

gently.expect(WORLD, 'hello');

myModule.hello();

gently.hijacked

An object that holds the references to all hijacked modules.

gently.verify([msg])

Verifies that all expectations of this gently instance have been satisfied. If not called manually, this method is called when the process 'exit' event is fired.

If msg is given, it will appear in any error that might be thrown.

License

Gently is licensed under the MIT license.