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

toboggan

v1.0.0

Published

mock template engine and testing framework for express

Downloads

5

Readme

toboggan

A mock template engine and testing framework for express

Installation

$ npm install toboggan

Motivation

Testing your express routes is good. Having a slow test suite is bad. You want to ensure your views are working with the correct data, but testing by inspecting DOM elements (or similar) is brittle.

toboggan mocks out any actual template rendering, regardless of which engine your real app uses, and provides test hooks which lets you verify the template path and variables.

Let's take a look at the following simplified example. Suppose you have a route set up like below:

app.get('/user', function(req, res){
  new User({id: req.param('id')}).fetch().then(function(user){
    res.render('user', {
      user: user.toJSON()
    });
  });
});

Now suppose you want to write some tests for the route. What can you do? You could certainly check for a correct status code or maybe even perform a string search for the username you expect to appear in the rendered HTML, for example. But this relies too much on how the view ends up displaying the information and less on the pure data you had made available to the view. And how do you know the correct template was even used to begin with?

Here is how you might write your tests using toboggan:

describe('GET /user', function(){
  it('should display the information for the user', function(done){
    request(app)
      .get('/user')
      .send({id: 1})
      .expectTemplate('user')
      .expectTemplate(function(path, options, verified){
        new User({id: 1}).fetch().then(function(user){
          options.user.should.eql(user.toJSON());
          verified();
        });
      })
      .end(done);
  });
});

Toboggan introduces a new chainable .expectTemplate function to supertest for verifying template related assertions, like the name of the template or the variables passed to the view. Best of all, you don't waste precious time actually rendering the view so your test suite remains fast.

Usage

Toboggan works with supertest and express. To use it, pass the supertest library to toboggan and install the mock template engine using the .install method:

var request = require('supertest'),
  toboggan = require('toboggan')(request),
  app = require('express')();
  
  // more app set up here ... 
  
  toboggan.install(app, 'jade');

Ontop of the regular .expect and .end supertest functions, you will now have access to the .expectTemplate function, described below.

Supported APIs

.install(express app, extension)

Sets up the app to use the mock template engine for the supplied extension. Note this will overwrite an existing view engine, if one is configured.

.uninstall()

Reverts any mocked-out template engines back to their original configuration before .install was called

.expectTemplate(string or fn)

Adds a template related assertion. When the only argument is a string, the name of the actual template rendered should equal the supplied parameter.

When the supplied parameter is a function, the body of the function is executed and passed path, options, and verified parameters, where:

  • path is the full file path of the template.

  • options contains the values passed as the second parameter to .render

  • verified needs to be called when you're done

    Note that asynchronous verifications are supported. Also note that .expectTemplate can be called multiple times, and all functions will finish executing before .end executes.

    Any errors arising during the execution of an .expectTemplate assertion will be passed as the first parameter to the function supplied to supertest's .end function. Typically, you would be passing mocha's done function to .end, which would fail the test if any template related assertions have failed.

License

MIT