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

chai-superagent

v3.1.0

Published

Extend Chai Assertion library with tests for superagent requests

Downloads

5,118

Readme

chai-superagent

Travis npm package Coverage Status

superagent integration testing with Chai assertions.

Features

  • simplified fork of chai-http
  • esm module only, support node >= 14
  • integration test request composition
  • test http apps or external services
  • assertions for common http tasks
  • chai expect and should interfaces

Install

$ npm install chai-superagent superagent

Plugin

Use this plugin as you would all other Chai plugins. Notice the function call - it accepts an optional parameter { strict }: { strict?: boolean }. When strict is true, the assertions will assert that the object being tested is an instance of superagent Request, Response or Agent types. It defaults to true.

import { use } from 'chai';
import superagent from 'chai-superagent';

use(superagent());

Integration Testing

Use superagent as you normally would, and test the responses using the assertions provided in this library.

import request from 'superagent';

request
  .get('http://localhost:8000/foo')
  .then(res => expect(res).to.have.status(200));

Assertions

The Chai HTTP module provides a number of assertions for the expect and should interfaces.

.status (code)

  • @param {Number} status number

Assert that a response has a supplied status.

expect(res).to.have.status(200);

.header (key[, value])

  • @param {String} header key (case insensitive)
  • @param {String|RegExp} header value (optional)

Assert that a Response or Request object has a header. If a value is provided, equality to value will be asserted. You may also pass a regular expression to check.

Note: When running in a web browser, the same-origin policy only allows Chai HTTP to read certain headers, which can cause assertions to fail.

expect(req).to.have.header('x-api-key');
expect(req).to.have.header('content-type', 'text/plain');
expect(req).to.have.header('content-type', /^text/);

.headers

Assert that a Response or Request object has headers.

Note: When running in a web browser, the same-origin policy only allows Chai HTTP to read certain headers, which can cause assertions to fail.

expect(req).to.have.headers;

.json / .text / .html

Assert that a Response or Request object has a given content-type.

expect(req).to.be.json;
expect(req).to.be.html;
expect(req).to.be.text;

.charset

Assert that a Response or Request object has a given charset.

expect(req).to.have.charset('utf-8');

.redirect

Assert that a Response object has a redirect status code.

expect(res).to.redirect;
expect(res).to.not.redirect;

.redirectTo

  • @param {String|RegExp} location url

Assert that a Response object redirects to the supplied location.

expect(res).to.redirectTo('http://example.com');
expect(res).to.redirectTo(/^\/search\/results\?orderBy=desc$/);

.param

  • @param {String} parameter name
  • @param {String} parameter value

Assert that a Request object has a query string parameter with a given key, (optionally) equal to value

expect(req).to.have.param('orderby');
expect(req).to.have.param('orderby', 'date');
expect(req).to.not.have.param('limit');

.cookie

  • @param {String} parameter name
  • @param {String} parameter value

Assert that a Request or Response object has a cookie header with a given key, (optionally) equal to value

expect(req).to.have.cookie('session_id');
expect(req).to.have.cookie('session_id', '1234');
expect(req).to.not.have.cookie('PHPSESSID');
expect(res).to.have.cookie('session_id');
expect(res).to.have.cookie('session_id', '1234');
expect(res).to.not.have.cookie('PHPSESSID');

License

See the LICENSE file for license rights and limitations (MIT).