I need to mock client side HTTP requests. I'm using
isomorphic-fetch
mocha
nock
nock
fetch('/foo') //hostname: http://localhost:8080
.then(res => res.json())
.then(data => console.log(data))
.catch(e => console.log(e))
nock('/')
.get('/foo')
.reply(200, {data: "hello"})
nock
Found a workaround for this. Have a _ajax-setup.js
in your test folder
import $ from 'jquery'
$(document).ajaxSend((event, jqxhr, settings) => {
settings.url = 'http://0.0.0.0' + settings.url;
})
The thing to note is that this file has to run First and Only Once. Having it as a file runs only once and _
before it makes it alphabetically first.
Later you can test your code with
nock('http://0.0.0.0')
.get('/foo')
.reply(200, {data: "hello"})