What is proper way to mock requests sent by node Webshot during a test using nock.js?
I tried following code to capture mocked response of http://example.com/foo.html as foo.png
but the mock doesn't seem to work.
const nock = require("nock");
const webshot = require("webshot");
describe("mock webshot request", function(){
this.timeout(20000);
beforeEach(function(){
nock("http://example.com").persist().get("/foo.html").reply(200, "<h1>Foo</h1>");
});
afterEach(function () {
nock.cleanAll();
});
it("captures mocked response", function(done){
webshot("http://example.com/foo.html", "foo.png",function(err) {
nock.isDone();
if(!err) done();
});
});
});
Edit:
The solution was to pass mocked response body to Webshot rather than url:
webshot("<h1>Foo</h1>", ...
Nock
expects the http request to happen in the same process.
Note: node-webshot
is a wrapper for PhantonJS which run in another process.
In your case Nock
is setup in one process, but the http request happens in another process. So you cannot mock http request done by node-webshot
like the way your are currently doing.
What you need is support for mocking http request built into node-webshot
i.e you will have to add this feature to node-webshot
if it doesn't have it.