Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse Supertest tests on a remote URL

Tags:

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.

Is there a way to supply request with a remote URL or proxy to a remote URL?

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();         }); 
like image 531
Shane Stillwell Avatar asked Dec 06 '12 22:12

Shane Stillwell


People also ask

How to pass the HTTP server to the Supertest?

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 .

How do I use Supertest to test APIs?

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.

How to test HTTP endpoints in Express with Supertest?

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.

How do I use supertest with an external site?

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.


1 Answers

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); }); 
like image 185
zemirco Avatar answered Sep 21 '22 06:09

zemirco