s-taylor / doubleagent

The ES7 async/await aware testing wrapper on top of secretagent

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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'

Copyright & License

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

Copyright (C) 2016 Nikolay Nemshilov

About

The ES7 async/await aware testing wrapper on top of secretagent


Languages

Language:JavaScript 100.0%