question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nocked calls to nested HTTP calls not working as expected

See original GitHub issue

Here’s a sample app:

var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var request = require('request');

var app = express();
var PORT = 1234;
app.set('port', PORT);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.get('/test', function (req, res) {
    request.get('http://www.google.com', function (err, response, body) {
        res.send(body);
    });
});

app.listen(PORT, function () {
    console.log('App started on port ' + PORT + '!');
});

Here’s the Mocha test for it:

var mocker = require('nock');
var assert = require('chai').assert;
var request = require('request');

describe('node-nock', function () {
    var testOutput = 'Hooray!';
    mocker('http://www.google.com')
        .get('/')
        .reply(200, testOutput);

    it('should mock the request to google.com', function (done) {
        request('http://localhost:1234/test', function (err, response, body) {
            assert.equal(testOutput, body);
            done();
        });
    });
});

Running the test above fails the assertion even though the call to www.google.com has been nocked.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
maheshjagcommented, Sep 20, 2018

@vitorarins @matanh-tzmedical Many thanks for your responses. No, I wasn’t requiring the app in my test, and yes, I was running the app separately.

Yes, I’m happy for this issue to be closed.

0reactions
lock[bot]commented, Oct 4, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6 Nested HTTP Calls Nested Call Never Executes
If I break up the two calls and execute them individually they both work. However, when combining them in series the second call...
Read more >
nock - npm
HTTP server mocking and expectations library for Node.js. Nock can be used to test modules that perform HTTP requests in isolation.
Read more >
The Best Smart Smoke Alarm | Reviews by Wirecutter
Smart smoke alarms are pricey, but unlike traditional ones can alert you to a problem even when you aren't home. The Google Nest...
Read more >
Nesl: A Nested Data-Parallel Language (Version 3.1)
compiler, on a local workstation while executing interactive calls to Nesl programs on ... solves the problem of nding the k th smallest...
Read more >
Never use nested IFs again! : r/excel - Reddit
Not the IFERROR call. Another place IF constructs can't be replaced is array formulas. Maybe no one should use array formulas either, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found