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

moxpress

v0.0.3

Published

Mock library for unit testing Express applications

Downloads

2

Readme

moxpress

Useful mocks for unit testing Node.js's Express.

This is an actively-under-development utility that we use internally at Webonise Lab. It enables us to write tests for our Express controller code that executes quickly and gives nice, precise errors.

Usage

Install

npm install --save-dev moxpress

Use

// Perform the import
var mockFactory = require("moxpress");
var mocks = mockFactory();

// Get your hands on the relevant variables
var app = mocks.app;  // Mock Express "app"
var req = mocks.req;  // Mock Express "req"
var res = mocks.res;  // Mock Express "res"

// Execute your test
var ctrl = require("./my/controller"); // Load your controller
ctrl.index(req, res);                  // Call the method
// Assert away

Supported APIs

app

  • set
  • get
  • enable
  • disable
  • enabled
  • disabled
  • listen (does nothing)
  • HTTP verb methods
  • all
  • route

To retrieve the routes that were created through all and the HTTP verb methods, use the plural of the HTTP verb (OPTIONS => optionses), which will be an object whose keys are paths and whose values are callbacks.

req

  • params
  • param
  • query
  • cookies
  • signedCookies
  • headers
  • get (including lowercasing the argument and aliasing 'referrer' to 'referer')
  • header (alias of get)
  • ip (defaults to 127.0.0.1)
  • ips (defaults to list with single element 127.0.0.1)
  • host (defaults to localhost)
  • fresh (defaults to true)
  • stale (defaults to false)
  • xhr (defaults to true)
  • protocol (defaults to http)
  • secure (defaults to false)
  • subdomains (defaults to empty array)
  • originalUrl (defaults to empty string)

res

  • statusCode (holds the status value)
  • status
  • set (both object and string versions)
  • header (alias of set)
  • get (including lowercasing the argument)
  • cookie
  • clearCookie
  • cookies (provides access to cookies as a map of name onto [value,options] lists)
  • redirect
  • redirectUrl (holds the URL which redirect redirects to)
  • location
  • locationUrl (holds the URL which location specifies)
  • send (in all its various permutations)
  • body (holds the body assigned by send, json, etc.)
  • json
  • type (uses node-mime to do the lookup)
  • format
  • formatter (holds the value passed to format)
  • attachment
  • attachmentFile (holds the value pased to attachment
  • sendFile (both with and without the callback)
  • download (both with and without the callback)
  • links
  • link\_values (holds the values passed to links)
  • locals

Contributing

Don't like the fact that some part of the API isn't implemented? Then feel free to contribute! This project uses GitHub Flow, so fork, branch, and then submit your pull request. No reasonable pull turned away!

The interesting file is ./lib/moxpress.js.

Note that the response has access to the request and application objects, and the request has access to the application object, so they can use that upstream logic as part of the mocking logic.

Other Useful Libraries for Testing