A set of Hamjest matchers for user with superagent
npm install superjest> A set of Hamjest matchers for user with superagent
superagent and hamjest are peer dependencies, so users of superjest need to install those modules too.
``bash`
$ npm install superagent hamjest superjest
Perform the assertions at the end of the request, however you like to user superagent.
`javascript`
it("requests google homepage", function(done) {
superagent
.get("https://www.google.com")
.end((err, resp) => {
assertThat(resp, hasStatusCode(200));
done();
});
});
For examples of the Matchers in action, see the tests.
* @param _{number}_ code
Assert that the response has the correct status code.
`js`
assertThat(resp, hasStatusCode(200));
* @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.
`js`
assertThat(resp, hasHeader('etag'));
assertThat(resp, hasHeader('content-type', equalTo('text/plain')));
* @param _{Matcher}_ matcher
Assert that the response has the correct body. Relies on a body property being present
on the response object.
`js`
assertThat(resp, hasBody(equalTo("Hello World")));
* @param _{Matcher}_ matcher
Assert that the response has the correct content type header.
Ignores field parameters
`js`
assertThat(resp, hasContentType(equalTo(json())));
assertThat(resp, hasContentType(equalTo("text/xml")));
* @param _{Matcher}_ matcher
Assert that the response has the correct charset.
The charset must be specified with a '-'
`js`
assertThat(resp, hasCharset(equalTo("utf-8")));
* @param _{Matcher}_ matcher
Assert that the response has the correct content length header.
`js`
assertThat(resp, hasContentLength(equalTo(12)));
* @param _{Matcher}_ matcher
Assert that the response has the correct location header.
`js`
assertThat(resp, hasLocation(equalTo("http://www.google.com")));
* @param _{string}_ location
Convenience to assertion redirection
`js``
assertThat(resp, isRedirectedTo("http://www.google.com"));
Convenience to return 'text/html' MIME type
Convenience to return 'application/json' MIME type
Convenience to return 'text/plain' MIME type
This library was inspired by chai-http. I just prefer to use hamjest
MIT