superjest

0.1.0 • Public • Published

superjest

A set of Hamjest matchers for user with superagent

Usage

superagent and hamjest are peer dependencies, so users of superjest need to install those modules too.

$ npm install superagent hamjest superjest

Using with superagent

Perform the assertions at the end of the request, however you like to user superagent.

it("requests google homepage", function(done) {
  superagent
   .get("https://www.google.com")
   .end((err, resp) => {
     assertThat(resp, hasStatusCode(200));
 
     done();
   });
 });

Matchers

For examples of the Matchers in action, see the tests.

hasStatusCode

  • @param {number} code

Assert that the response has the correct status code.

assertThat(resp, hasStatusCode(200));

hasHeader

  • @param {string} name The header name
  • @param {Matcher} [matcher] Optional matcher for the header value

Assert that the response has a header, and that header matches expectations.

assertThat(resp, hasHeader('etag'));
assertThat(resp, hasHeader('content-type', equalTo('text/plain')));

hasContentType

  • @param {Matcher} matcher

Assert that the response has the correct content type header.

Ignores field parameters

assertThat(resp, hasContentType(equalTo(json())));
assertThat(resp, hasContentType(equalTo("text/xml")));

hasCharset

  • @param {Matcher} matcher

Assert that the response has the correct charset.

The charset must be specified with a '-'

assertThat(resp, hasCharset(equalTo("utf-8")));

hasContentLength

  • @param {Matcher} matcher

Assert that the response has the correct content length header.

assertThat(resp, hasContentLength(equalTo(12)));

hasLocation

  • @param {Matcher} matcher

Assert that the response has the correct location header.

assertThat(resp, hasLocation(equalTo("http://www.google.com")));

isRedirectedTo

  • @param {string} location

Convenience to assertion redirection

assertThat(resp, isRedirectedTo("http://www.google.com"));

html

Convenience to return 'text/html' MIME type

json

Convenience to return 'application/json' MIME type

text

Convenience to return 'text/plain' MIME type

Thanks

This library was inspired by chai-http. I just prefer to use hamjest

License

MIT

Package Sidebar

Install

npm i superjest

Weekly Downloads

66

Version

0.1.0

License

MIT

Unpacked Size

11.1 kB

Total Files

4

Last publish

Collaborators

  • kierans777