Socket
Socket
Sign inDemoInstall

chai-superagent

Package Overview
Dependencies
46
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    chai-superagent

Extend Chai Assertion library with tests for superagent requests


Version published
Weekly downloads
1.3K
increased by26.71%
Maintainers
1
Install size
678 kB
Created
Weekly downloads
 

Readme

Source

chai-superagent

Travis npm package Coverage Status

superagent integration testing with Chai assertions.

Features

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');
  • @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).

Keywords

FAQs

Last updated on 09 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc