I'm using MochaJS and SuperTest to test my API during development and absolutely LOVE it.
However, I would like to also turn these same tests to remotely tests my staging server before pushing the code out to production.
Here is a sample of a test I use
request(app) .get('/api/photo/' + photo._id) .set(apiKeyName, apiKey) .end(function(err, res) { if (err) throw err; if (res.body._id !== photo._id) throw Error('No _id found'); done(); });
Then we need to pass the http.Server to the request () method of supertest. To do that let’s include our express app as follows. //apiTest.jsconst request = require ('supertest');const app = require ('../app'); //reference to you app.js file The let’s write the first API test to test the http://localhost:3000/users endpoint .
Here’s how I use it to test APIs. You can configure supertest in a couple ways. If you’re testing an external site, you can configure supertest with the site’s base url: If you’re testing an Express app, you can pass the app to supertest, and let it worry about setup/teardown of your site.
Well you can use SuperTest which is can be used to test HTTP endpoints. Before starting SuperTest let’s create a simple API using node + express. In express let’s use the the route given to us by them in order to learn SuperTest library. This article is not focusing on crating a rest API using express framework.
If you’re testing an external site, you can configure supertest with the site’s base url: If you’re testing an Express app, you can pass the app to supertest, and let it worry about setup/teardown of your site.
I'm not sure if you can do it with supertest. You can definitely do it with superagent. Supertest is built on superagent. An example would be:
var request = require('superagent'); var should = require('should'); var agent = request.agent(); var host = 'http://www.yourdomain.com' describe('GET /', function() { it('should render the index page', function(done) { agent .get(host + '/') .end(function(err, res) { should.not.exist(err); res.should.have.status(200); done(); }) }) })
So you cannot directly use your existing tests. But they are very similiar. And if you add
var app = require('../app.js');
to the top of your tests you easily switch between testing your local app and the deployment on a remote server by changing the host
variable
var host = 'http://localhost:3000';
Edit:
Just found an example for supertest in the docs#example
request = request.bind(request, 'http://localhost:5555'); // add your url here request.get('/').expect(200, function(err){ console.log(err); }); request.get('/').expect('heya', function(err){ console.log(err); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With