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

doubleagent

v2.0.0

Published

A superagent based async/await friendly testing util

Downloads

6,402

Readme

Double Agent

doubleagent is an ES7 async/await compatible wrapper on top of superagent that makes testing of expressjs/connect apps easier in a modern environment.

Lets say you have an expressjs app, that looks somewhat like this:

import express from "express";

const app = express();

app.get("/hello", (req, res) => {
  res.json({ok: true});
});

export default app;

Now, doubleagent allows you to test the app like it's 2016 out there:

import { expect } from "chai";
import agent from "doubleagent";
import app from "../src/app";

describe("app", () => {
  it("handles GET /hello", async () => {
    const response = await agent(app).get("/hello");

    expect(response.status).to.be(200);
    expect(response.body).to.eql({ok: true});
  });
});

There are two main points to this whole thing:

  1. Using ES7 async/await syntax for more maintainable tests codebase
  2. Use your favorite assertion library for testing

API & Usage

Installation is straight forward

npm install doubleagent --save-dev

As for the API, the agent(app) call returns a object with all the basic HTTP methods that all have the same API:

agent(app)[method](path[, params[, headers[, files]]]) -> Promise

NOTE: params are contextual, they are handled as query for GET/HEAD requests and as body for POST, PUT, etc. When files are passed the request changes into a multi-part form and params is treated as fields.

Default Headers

If you need to specify app wide default HTTP headers, just assign them to the defaultHeaders property:

agent.defaultHeaders = { Authorization: `Bearer ${token}` };
agent.get('/some/url'); // <- will automatically send the headers

NOTE any headers that you send through with specific #get, #post, etc. requests will override the defaultHeaders values.

Full URL Locations

If you need to access a full URL location to the http server that runs underneath the doubleagent interface, please use the #urlFor(path) method;

agent.urlFor('/users'); // -> 'http://127.0.0.1:0/users'

Custom Query String Encoding

Under the hood, doubleagent uses superagent. Superagent uses the qs library for both query string stringification and parsing. However you may use a different form of querystring encoding such that it matches your application. You can optionally pass in a custom parser to doubleagent.

import { expect } from "chai";
import agent from "doubleagent";
import app from "../src/app";

/* define a custom stringify function */
const stringify = (query) => qs.stringify(query, { arrayFormat: 'indicies' });
const test = agent(app, { serializer: stringify });

describe("app", () => {
  it("handles GET /hello", async () => {
    const response = await test.get("/hello");

    expect(response.status).to.be(200);
    expect(response.body).to.eql({ok: true});
  });
});

Copyright & License

All code in this library is released under the terms of the MIT license

Copyright (C) 2016 Nikolay Nemshilov