The motivation with this module is to provide a high-level abstraction for testing
HTTP, while still allowing you to drop down to the lower-level API provided by superagent.
Getting Started
Install SuperTest as an npm module and save it to your package.json file as a development dependency:
npm install @eggjs/supertest --save-dev
Once installed it can now be referenced by simply calling require('supertest');
Example
You may pass an http.Server, or a Function to request() - if the server is not
already listening for connections then it is bound to an ephemeral port for you so
there is no need to keep track of ports.
SuperTest works with any test framework, here is an example without using any
test framework at all:
One thing to note with the above statement is that superagent now sends any HTTP
error (anything other than a 2XX response code) to the callback as the first argument if
you do not add a status code expect (i.e. .expect(302)).
If you are using the .end() method .expect() assertions that fail will
not throw - they will return the assertion as an error to the .end() callback. In
order to fail the test case, you will need to rethrow or pass err to done(), as follows:
Passing the app or url each time is not necessary, if you’re testing
the same host you may simply re-assign the request variable with the
initialization app or url, a new Test is created per request.VERB() call.
You may use any superagent methods,
including .write(), .pipe() etc and perform assertions in the .end() callback
for lower-level needs.
.expect(status[, fn])
Assert response status code.
.expect(status, body[, fn])
Assert response status code and body.
.expect(body[, fn])
Assert response body text with a string, regular expression, or
parsed body object.
.expect(field, value[, fn])
Assert header fieldvalue with a string or regular expression.
.expect(function(res) {})
Pass a custom assertion function. It’ll be given the response object to check. If the check fails, throw an error.
request(app)
.get('/')
.expect(hasPreviousAndNextKeys)
.end(done);
function hasPreviousAndNextKeys(res) {
if (!('next' in res.body)) throw new Error("missing next key");
if (!('prev' in res.body)) throw new Error("missing prev key");
}
@eggjs/supertest
Document see SuperTest
About
The motivation with this module is to provide a high-level abstraction for testing HTTP, while still allowing you to drop down to the lower-level API provided by superagent.
Getting Started
Install SuperTest as an npm module and save it to your package.json file as a development dependency:
Once installed it can now be referenced by simply calling
require('supertest');Example
You may pass an
http.Server, or aFunctiontorequest()- if the server is not already listening for connections then it is bound to an ephemeral port for you so there is no need to keep track of ports.SuperTest works with any test framework, here is an example without using any test framework at all:
To enable http2 protocol, simply append an options to
requestorrequest.agent:Here’s an example with mocha, note how you can pass
donestraight to any of the.expect()calls:You can use
authmethod to pass HTTP username and password in the same way as in the superagent:One thing to note with the above statement is that superagent now sends any HTTP error (anything other than a 2XX response code) to the callback as the first argument if you do not add a status code expect (i.e.
.expect(302)).If you are using the
.end()method.expect()assertions that fail will not throw - they will return the assertion as an error to the.end()callback. In order to fail the test case, you will need to rethrow or passerrtodone(), as follows:You can also use promises:
Or async/await syntax:
Expectations are run in the order of definition. This characteristic can be used to modify the response body or headers before executing an assertion.
Anything you can do with superagent, you can do with supertest - for example multipart file uploads!
Passing the app or url each time is not necessary, if you’re testing the same host you may simply re-assign the request variable with the initialization app or url, a new
Testis created perrequest.VERB()call.Here’s an example with mocha that shows how to persist a request and its cookies:
There is another example that is introduced by the file agency.js
Here is an example where 2 cookies are set on the request.
API
You may use any superagent methods, including
.write(),.pipe()etc and perform assertions in the.end()callback for lower-level needs..expect(status[, fn])
Assert response
statuscode..expect(status, body[, fn])
Assert response
statuscode andbody..expect(body[, fn])
Assert response
bodytext with a string, regular expression, or parsed body object..expect(field, value[, fn])
Assert header
fieldvaluewith a string or regular expression..expect(function(res) {})
Pass a custom assertion function. It’ll be given the response object to check. If the check fails, throw an error.
.end(fn)
Perform the request and invoke
fn(err, res).Notes
Inspired by api-easy minus vows coupling.
License
MIT
Contributors
Made with contributors-img.