doubleagent
v2.0.0
Published
A superagent based async/await friendly testing util
Downloads
6,402
Maintainers
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:
- Using ES7
async/await
syntax for more maintainable tests codebase - 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