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

nodeunit-express

v0.0.5

Published

A Mock Request object to use when node-unit-testing an express app

Downloads

351

Readme

nodeunit-express

nodeunit-express is a utility function to make it easy to write tests for Node.JS Express applications. It is based on the https://github.com/rubymaverick/express-mock-request project, which I could not get to work properly.

How to use:

First, install using npm:

npm install nodeunit-express

Then require it in your test file like so:

var request = require('nodeunit-express');

Let's say you want to test this simple express app to make sure it returns the proper status, body, and headers:

var express = require('express')
var app = module.exports = express();

app.get('/', function(req, res) {
  res.send("ok", {'Content-Type': 'text/html'}, 200);
});

// Only start listening on 8080 when this file is run directly i.e.: node app.js
if(!module.parent) {
  app.listen(8080);
}

You could write your test like so:

var request = require('nodeunit-express');
// require the express application, notice how we exported the express app using `module.exports` above
var app = require('../app');

// This is a nodeunit test example
exports.testGet = function(test){
  var express = request(app);
  express.get('/').expect(function(response) {
    // response is the response from hitting '/'
    test.equal(response.body, "ok");
    test.equal(response.statusCode, 200);
    test.equal(response.headers['content-type'], "text/html");
    test.done();
    express.close();
  });
}

Tester

var testerInitializer = require('nodeunit-express/tester');
var app = require('../app');

var globalOptions = {
    prepare: function (res) {
        if (res.body != null) {
            res.body = JSON.parse(res.body);
        }

        return res;
    }
};

var tester = testerInitializer(globalOptions);

module.exports['test test'] = tester(app, {
    method: 'POST',
    uri: '/user/123/',
    headers: {
        'some-header-key': 'some-header-value'
    },
    body: {
        "first_name": "Mike",
        "last_name": "Portnoy"
    }

    expect: {
        // any response property what you need check

        // statusCode
        statusCode: 400

        // expect body as object. By default all specified expected properties compare deeply by test.deepEqual
        body: {
            error: {
                name: "ValidationError",
                rule: "required"
            }
        }

        // expect body as function if you need check not all property
        body: function (body, response, options, args) {
            // args: tester returns a function. args is called function arguments. for example for nodeunit first argument is "test"
            // response: response object
            // options: compiled options
            var test = args[0];

            test.strictEqual(body.error.name, 'ValidationError');
        }
    }
});