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

vanilli

v5.0.2

Published

Setup and serve stubs, fakes and expectations

Downloads

35

Readme

vanilli

Build Status

Nodejs library that allows managing of a RESTful server for storing, matching and verifying stubs/expectations from a test suite.

API

How it works

Vanilli is designed to act as a "fake" version of the REST services that your SUT (System Under Test) depends on. It sits running on a port you specify, waiting to serve up responses that you specify via adding stubs. Stubs are added and verified via the javascript API.

Your SUT is then configured to call vanilli instead of the REST services it usually uses.

Installation

npm install vanilli

Initialisation and configuration

Vanilli configuration can be specified when initalising:

var vanilli = require('vanilli');
...
vanilli.listen(port, config);

config is an optional parameter. All config options are given below with their default values:

{
    logLevel: "error", // See 'Diagnostics' section below
    static: undefined // See 'Static Content' section below
}

Usage

Typical usage:

var vanilli = require('vanilli');

describe('My SUT', function () {
    before(function () {
        vanilli.listen(port); // Start the vanilli REST server
    });

    after(function () {
        vanilli.stop(); // Shutdown vanilli REST server
    });

    afterEach(function () {
        vanilli.verify(); // Verify all expectations have been met
        vanilli.clear(); // Clear down stubs from vanilli ready for next test
    });

    it('does something', function (done) {
        vanilli.stub(
            vanilli.onGet("/this/url/MIGHT/happen").respondWith(200)
        );

        vanilli.expect(
            vanilli.onGet("/this/url/MUST/happen").respondWith(200)
        );

        // Manipulate SUT to required state

        // Make assertions

        // Note that the vanilli expectation above will be verified by the vanilli.verify() in 'afterEach'.
    });
});

See the API for more usage information.

Lazy matching

Vanilli's matching logic is lazy - i.e. a as long as ALL the criteria on a given stub match an incoming request vanilli does not care about any further details of that request. So, for example, if one specifies a stub that matches on a specific query parameter then the matching logic ONLY cares about that query parameter - any other query parameters are considered irrelevant.

This approach means more succinct stubs and less matching criteria irrelevant to the test at hand.

JSONP

Vanilli stub responses will automatically be wrapped in JSONP if either a "callback" or "jsonp" query string parameter is found on the request that the stub response is being produced for. This is not explicitly handled in vanilli but by its underlying restify server instead.

Diagnostics

Vanilli logs to sysout and syserr via bunyan. Switching logLevel to debug will cause vanilli to spit out a whole load of diagnostic information relating to what stubs are stored and how it is matching stubs against incoming requests. In such situations I recommend piping the output to the bunyan executable itself (which you can install in the usual way with npm install -g bunyan) to get nicely formatted output.

See the bunyan project itself for more info on logging and log levels.

Stubs vs expectations

For vanilli, an "expectation" is simply a specialized stub. In short: a stub MIGHT be matched; an expectation MUST be matched.

A stub...

  • CAN be matched UP TO the specified number of times (1 if not explicitly specified).
  • WILL cause an error if matched too many times.
  • WILL NOT cause an error if matched too few times.

An expectation...

  • MUST be matched the specified number of times (1 if not explicitly specified).
  • WILL cause an error if matched too many times.
  • WILL cause an error if matched too few times.

So, if you want to assert on the actual calls that your SUT is using use an expectation; otherwise use a stub.

REMEMBER: The more vanilli expectations you add to your tests the more brittle they will get: consider using stubs as your first choice.

Static Content

To serve up the stubbed responses vanilli is, at its core, an HTTP server. This means that it could, potentially, serve up content other than stubs. This opens up the possibility of vanilli acting as your web app's HTTP server.

So, to this end, the static config option was created. It acts like a "pass through" filter - if an incoming request matches the static filter then the static content will be served rather than attempting to match against a stub. The option takes the form:

{
    static: {
        root: "sut/static/root", // Root path for static content 
        "default": "page.html", // Default document from root
        include: [
            "**/*.html", // normal include
            { path: '/foo', target: 'foo.html' }, // proxy route to specified target
            { path: '/foo/**', target: 'foo-sub.html' }, // proxy wildcard route to specified target
            { path: '/bar', useDefault: true } // proxy route to default "page.html"
        ],
        exclude: [ globA, globB, ... , globZ ]
    }
}

You can see an example in test/e2e/vanilli-test.js

CLI

As well as the javascript API described above, the vanilli server also provides a CLI for starting a server from non-javascript environments. To see the CLI simply run vanilli --help. (You may need to install vanilli globally first with npm install -g vanilli.)