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.
$ npm install superagent hamjest superjest
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();
});
});
For examples of the Matchers in action, see the tests.
- @param {number} code
Assert that the response has the correct status code.
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.
assertThat(resp, hasHeader('etag'));
assertThat(resp, hasHeader('content-type', equalTo('text/plain')));
- @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")));
- @param {Matcher} matcher
Assert that the response has the correct charset.
The charset must be specified with a '-'
assertThat(resp, hasCharset(equalTo("utf-8")));
- @param {Matcher} matcher
Assert that the response has the correct content length header.
assertThat(resp, hasContentLength(equalTo(12)));
- @param {Matcher} matcher
Assert that the response has the correct location header.
assertThat(resp, hasLocation(equalTo("http://www.google.com")));
- @param {string} location
Convenience to assertion redirection
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