سؤال

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();
        });
هل كانت مفيدة؟

المحلول 2

You already mentioned it, since you are targeting remote URL just replace the app with your remote server URL

request('http://yourserverhere.com')
.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();
});

نصائح أخرى

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);
});

Other answers din't work for me.

They use .end(function(err, res) { which is a problem for any async http calls, need to use .then instead:

A sample working code is below:

File: \test\rest.test.js

let request = require("supertest");
var assert = require("assert");

describe("Run tests", () => {
  request = request("http://localhost:3001");        // line must be here, change URL to yours

  it("get", async () => {

    request
      .get("/")                                      // update path to yours
      .expect(200)
      .then((response) => {                          // must be .then, not .end
        assert(response.body.data !== undefined);
      })
      .catch((err) => {
        assert(err === undefined);
      });

  });

});

File: \package.json

"devDependencies": {
  "supertest": "^6.1.3",
  "mocha": "^8.3.0",
},
"scripts": {
  "test": "mocha"
}

Install & run

npm i
npm test
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top