Sunday, 9 April 2017

can't mock a 3d party request with Nock

I'm trying to mock a third-party request to google with nock inside a mocha test for node.js/express/superagent application

unfortunately, it doesn't mock at all :( and prints out a real request in a console :(

what am i missing

my express application code:

var express = require('express');
var google = require('./controllers/google');
var app = express();

app.get('/google', google.go);

my google.js with a function:

var request = require('superagent');

exports.go = function(req, res) {
    request
        .get('http://google.com/')
        .then(function onResult(response) {
          console.log('my unmocked funcion')
            res.json({
                status: "ok"
            });
        }, function onError(err) {
            res.json({
                status: "error"
            })
        });
};

finally my test:

var agent = require('superagent');
var request = require('supertest');
var nock = require('nock');
var chai = require('chai');
var expect = chai.expect();


request = request('http://localhost:3000');


describe('GET /google', function() {
    var google = nock('http://google.com')
                .get('/')
                .reply(200, "this response is mocked");

    it('should be mockeed!', function() {
        return request.get('/google')
            .expect(200)
            .then( function (res) {
            console.log(res)})
    });
});


via tania

No comments:

Post a Comment